Magic Tree

Harry Potter a new school of magic: You can change the number of fruit trees. Full of joy, he found a huge fruit trees, to test his new law
surgery. A total of N nodes fruit tree, where 0 is the root node, each node's father referred to as FA u [u], ensure fa [u] <u. Initially, the fruit tree
fruit trees have been cleared away by Dumbledore magic, so that each node of fruit trees do not fruit (ie, 0 fruit) Unfortunately, Harry
's magic school get bit, only to the number of fruit on the tree node for a path of unity to increase a certain amount. That is, Harry magic can be
thus described: Add uvd, shows a number of fruit all the nodes on the path between nodes u and v are plus d. Next, in order to facilitate inspection Harr
whether y magic success, you need to tell him something about fruit trees in the process of release of magic in: Query u, represents the current fruit trees, to the point u
sub-tree rooted in a total of how many fruit?
Input
first line an integer N (1≤N≤100000), indicates the total number of fruit node, nodes 0,1, ..., N-1 numbers, 0 necessarily represent the root node.
Then N-1 lines, each two integers a, b (0≤a Next is an integer Q (1≤Q≤100000), Q represents a total operation time.
Followed by Q rows, each row is two of one:
1. a UVD, u represents the number of fruit to all nodes on the path to v plus D; (0 ≦ u, v≤N-1,0 <D <100000)
2. Q u, It represents the total number of fruit interrogation subtree rooted in u, u attention including itself. (. 1-0≤u≤N)
1≤N≤100000,1≤Q≤100000
the output
output answer to the query.
the Sample the Input
4
0 1
1 2
2 3
4
A 1 3 1
Q 0
Q 1
Q 2
Sample Output
3
3
2

 

// note of dfs order to know the scope of the subtree 
// value for some range, operating the lazy 
#include <stdio.h> 
#include <the iostream> 
#include <string.h> 
#include <algorithm> 
# the include <stdlib.h> 
#include <math.h> 
#include <Vector> 
the using namespace STD; 
 
typedef Long Long LL; 
const int N = 100001; 
 
int E [N 2 *], NXT [2 * N], head [ N]; 
int DEP [N], FA [N], SIZ [N], W [N], Top [N], Son [N], End [N]; 
 
struct A { 
    LL SUM, Tag; 
} T [ . 4 * N]; 
 
int n-, C = 0, Edge = 0; 
void the Add (int X, Y int) 
{ 
    E [Edge ++] = Y; 
    NXT [Edge] = head [X]; 
    head [X] = Edge; 
} 
 
void DFS1 (int X, F int, int D) 
{// find size, dep, son, fa
    DEP [X] = D; FA [X] = F; SIZ [X] =. 1; 
    int I, TP = 0; 
    for (I = head [X]; I; I = NXT [I]) 
        IF (E [ I] = FA [X])! 
        { 
            DFS1 (E [I], X, D +. 1); 
            SIZ [X] + = SIZ [E [I]]; 
            IF (SIZ [E [I]]> TP) SIZ TP = {[E [I]]; Son [X] = E [I];} 
        } 
} 
 
void DFS2 (X int, int TP) 
{// find dfs sequence, the top of the chain of nodes 
    w [x] = + + C; 
	Top [X] = TP; 
    IF (Son [X]) 
	DFS2 (Son [X], Top [X]); 
    for (int I = head [X]; I; I = NXT [I]) 
        IF (!! son E [I] = [X] && E [I] = FA [X]) 
            DFS2 (E [I], E [I]); 
    End [X] = C; // X where subtree maximum sequence dfs 
} 
 
void build (int X, L int, int R & lt) 
{// build segment tree 
    t [x] .tag = t [ x] .sum = 0;
    IF (R & lt == L) return;
    pushdown(x,l,r); 
    int >> M. 1 = L + R & lt;
    Build (<<. 1 X, L, M); 
    Build (X <<. 1 |. 1,. 1 + M, R & lt); 
} 
 
void pushdown (int X, L int, int R & lt) 
{// the line segment tree downstream marker 
    if (T [X] .tag) 
    { 
        T [X <<. 1] + = .tag T [X] .tag; 
        T [X <<. 1 |. 1] + = .tag T [X] .tag; 
        int = M + L >>. 1 R & lt; 
        T [X <<. 1] + .sum = (. 1 + M-L) * T [X] .tag; 
        T [X <<. 1 |. 1] + = .sum (rM of) * T [X] .tag; 
        T [X] .tag = 0; 
    } 
} 
 
void update (X int, int L, R & lt int, int QL, QR int, int V) 
{// update the line segment tree [ql, qr] node weights plus V 
    IF (QL <= L && QR> = R & lt) 
     { 
        T [X] .sum + = (R & lt-L +. 1) * V; 
        T [X] .tag + = V; 
        return; 
     } 
    int M = L + R & lt >>. 1 ;
    if(ql<=M) 
	   update(x<<1,l,M,ql,qr,v);
    if(qr>M) 
	   update(x<<1|1,M+1,r,ql,qr,v);
    t[x].sum=t[x<<1].sum+t[x<<1|1].sum;
}
 
ll query(int x,int l,int r,int ql,int qr)
{//在线段树中查询[ql,qr]的权值和 
    if(ql<=l&&qr>=r) return t[x].sum;
    pushdown(x,l,r);
    int M=l+r>>1;ll sum=0;
    if(ql<=M) sum+=query(x<<1,l,M,ql,qr);
    if(qr>M) sum+=query(x<<1|1,M+1,r,ql,qr);
    return sum;
}
 
void upd(int x,int y,int z)
{//修改操作,见课件 
    int f1=top[x],f2=top[y],tmp;
    while(f1!=f2)
    {
        if(dep[f1]<dep[f2])
        {
            tmp=f1;f1=f2;f2=tmp;
            tmp=x;x=y;y=tmp;
 
    scanf("%d",&m);
    for(i=1;i<=m;i++)
    { 
        } 
        the else 
        { 
            Scanf ( "% D", & x); // x is statistical in the subtree rooted weights 
			x ++; 
            the printf ( "% LLD \ n-", Query (1,1, n-, W [ X], End [X])); 
        } 
    } 
    return 0; 
}

  

Guess you like

Origin www.cnblogs.com/cutemush/p/11858018.html