NOIP simulation game blacksmith Carpenter explanations

【Problem Description】

Mark Douglas is an excellent forging division. Inconsistent with his excellent forging level, he was very poor, so many knives are good material because of a lack of lack of funds can not build.

Mark put his ability to forge all n kinds of forging a knife and built a tree, except for the first one than the kind of knife can directly create, other knives can only be processed from a certain kind of knife. Specifically, the first i kind of knife only from the FA i processed from kinds of knives, the cost it takes to w i , but the first FA i kind of knife is not the first i get kind of knife inverse process. Mark define a value for the knife used his knife to create the existing cost. Although he was poor, but the vision is very high, the value is below a certain value of his knife disdain. There are q times asked Always ask when Mark hands of first u minimum value kinds of knives and his fancy for k , the value of all the knife he could fancy and how much.

[Input Format]

Enter the file name Forging .in .

The first line a positive integer the n- .

Next, n- -1 lines of two positive integers FA I  W I

The next line a positive integer Q .

Next q lines of two positive integers UK .

[Output format]

Output file name Forging the .out .

Output q lines, each line represents a set of answers to inquiries.

 

Input:

3

1 2

1 3

2

1 3

1 2

Output:

3

5

【data range】

For 1 to 4 number of a test point ( 20 is%) : 1 <= n-, Q <= 1000 .

For 1-8 Number Test points ( 40%) 1 <= n-, Q <= 100000,1 <= K <= 50.

For 9 to 10 Number Test points ( 10% ): one strand of the tree form.

For 9 to 14 Number of test points ( 30% ): In addition to a remaining degree all points outside the dot does not exceed 2 .

For all test points (100%). 1 <= n-, Q <= 100000,1 <= K <= 1e9,1 <= Wi <= 1000 .

 

This question first, we can see it as President of the trees bare title, it can also be seen as segment tree merge templates;

However, here is the maintenance of a tree with a cross-sectional block;

We started this tree will be split tree chain, note that we do not use to maintain the tree line, so long as the pretreatment portion of the tree section to write out just fine;

We know that the cross-sectional nature of the tree (DFS sequence may of course): a collection of all the root node and its child nodes are formed in a contiguous section;

Then for each inquiry, each block having a half position, the large number in each position are accumulated into this answer;

Note one detail: when half of the answer is this interval of the extreme right, be sure to meet the special judge at this point whether the conditions;

A O (nlogn + q * sqrt (n) * logn) time complexity when;

Can be had without off-line processing, not discrete, then a large range of data may be done;

#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#define inc(i,a,b) for(register int i=a;i<=b;i++)
using namespace std;
int head[200010],cnt;
class littlestar{
    public:
        int to;
        int nxt;
        int w;
        void add(int u,int v,int gg){
            to=v;
            nxt=head[u];
            head[u]=cnt;
            w=gg;
        }
}star[2000010];
int n;
long long val[100010];
int f[100010],top[100010],seg[100010],dep[100010],size[100010],rev[100010],son[100010];
long long b[100010];
void dfs1(int u,int fa)
{
    dep[u]=dep[fa]+1;
    f[u]=fa;
    size[u]=1;
    for(int i=head[u];i;i=star[i].nxt){
        int v=star[i].to;
        if(v==fa) continue;
        dfs1(v,u);
        size[u]+=size[v];
        if(size[v]>size[son[u]]){
            son[u]=v;
        }
    }
}
void dfs2(int u,int fa)
{
    if(son[u]){
        seg[son[u]]=++seg[0];
        rev[seg[0]]=son[u];
        top[son[u]]=top[u];
        dfs2(son[u],u);
    }
    for(int i=head[u];i;i=star[i].nxt){
        int v=star[i].to;
        if(v==fa) continue;
        if(!top[v]){
            seg[v]=++seg[0];
            rev[seg[0]]=v;
            top[v]=v;
            dfs2(v,u);
        }
    }
}
void build()
{
    seg[0]=seg[1]=rev[1]=top[1]=1;
    dfs1(1,0);
    dfs2(1,0);
    inc(i,1,n){
        b[i]=val[rev[i]];
    }
}
void dfs(int u,int fa)
{
    for(register int i=head[u];i;i=star[i].nxt){
        int v=star[i].to;
        if(v==fa) continue;
        val[v]=val[u]+star[i].w;
        dfs(v,u);
    }
}
int num;
int belong[100010],l[100010],r[100010],block;
long long gg[100010];
long long sum[100010];
void build2()
{
    block=sqrt(n);
    num=n/block;
    if(n%block) ++num;  
    inc(i,1,n){
        gg[i]=b[i];
        belong[i]=((i-1)/block)+1;
    }
    inc(i,1,num){
        l[i]=block*(i-1)+1;
        r[i]=block*i;
    }
    r[num]=n;
    inc(i,1,num){
        sort(gg+l[i],gg+1+r[i]);
    }
    inc(i,1,n) sum[i]=sum[i-1]+gg[i];
}
long long ans;
inline void query(register int x,register int y,long long goal)
{
    if(belong[x]==belong[y]){
        for(int j=x;j<=y;j++){
            if(b[j]-b[x]>=goal) ans+=(b[j]-b[x]);
        }
        return;
    }
    for(register int i=x;i<=r[belong[x]];i++){
        if(b[i]-b[x]>=goal) ans+=(b[i]-b[x]);
    }
    for(register int i=l[belong[y]];i<=y;i++){
        if(b[i]-b[x]>=goal) ans+=(b[i]-b[x]);
    }
    for(register int i=belong[x]+1;i<=belong[y]-1;i++){
        register int L=l[i],R=r[i],mid;
        while(L<R){
            mid=(L+R)/2;
            if(gg[mid]-b[x]>=goal){
                R=mid;
            }
            else{
                L=mid+1;
            }
        }
        if(L==r[i]){
            if(gg[L]-b[x]>=goal){
                ans+=gg[L]-b[x];
            }
            continue;
        }
        ans+=(sum[r[i]]-sum[L-1]-b[x]*(r[i]-L+1));
    }
}
template<class nT>
inline void read(nT& x)
{
    char c;
    while(c=getchar(),!isdigit(c));
    x=c^48;
    while(c=getchar(),isdigit(c)) x=x*10+c-48;
}
int main()
{
    read(n);
    inc(i,2,n){
        int u,w; scanf("%d%d",&u,&w);
        star[++cnt].add(u,i,w);
    }
    dfs(1,0);
    build();
    build2();
    int q;read(q);
    inc(i,1,q){
        ans=0;
        long long x,goal;
        read(x); read(goal);
        query(seg[x],seg[x]+size[x]-1,goal);
        printf("%lld\n",ans);
    }
}
/*
3
1 2
1 3
2
1 3
1 2
 
6
1 2
1 2
3 3
3 4
2 5
2
3 2
 
7
3 1
1 2
3 1
4 2
4 1
4 2
5
 
*/

 

Guess you like

Origin www.cnblogs.com/kamimxr/p/11773125.html