Topic Links:
https://vjudge.net/problem/POJ-3321
Main topic:
The main idea is to give you a tree, there is an apple at the beginning of each node, there are two actions: modify (that is, modify a node, modify this node of the apple from there to nothing, or from scratch) and query (query a node how many apples on his subtree).
Problem Solving Ideas:
Each node of the tree is numbered according to the ordinal traversal, so that a period of time can be processed, so that the interval is the sub-tree of point I, the sum only requires interval and can.
Black is the original number, Blue is the number that is obtained by the first order traversal
The point of the Super Pit:
Use VECTOR<INT>G[MAXN here] will time out
With vector<vector<int> > G (MAXN) it's over.
1#include <cstdio>2#include <cstring>3#include <iostream>4#include <algorithm>5#include <string>6#include <cmath>7#include <Set>8#include <queue>9#include <map>Ten#include <stack> One#include <vector> A#include <list> -#include <deque> -#include <sstream> the#include <cctype> - #defineREP (i, n) for (int i = 0; I < (n); i++) - #defineFor (I, S, T) for (int i = (s); I < (t); i++) - #defineMEM (A, X) memset (A, X, sizeof (a)); + using namespacestd; -typedefLong Longll; +typedef unsignedLong Longull; A Const intMAXN = 1e5 +Ten; at Const DoubleEPS = 1e-Ten; - Const intINF =1<< -; - Const intdir[4][2] = {1,0,0,1,0,-1,-1,0}; - Const DoublePI =3.1415926535898; - intT, N, m, cases, tot; - intTREE[MAXN], LEF[MAXN], RIG[MAXN], S[MAXN];///s array records whether the node has a tree invector<vector<int> >A (MAXN); - voidDfsintx) to ///DFS gives the tree node numbers, respectively, to the left node and the right node, + ///The left node is itself, the right node is the maximum number of reachable nodes, - ///The sub-tree of the query node x can then be queried directly for the interval of [le[x], rig[x]], and the { *LEF[X] =tot; $ for(inti =0; I < a[x].size (); i++)Panax Notoginseng { -tot++; the DFS (A[x][i]); + } ARIG[X] =tot; the } + intLowbit (intx) - { $ returnx& (-x); $ } - intSumintx) - { the intRET =0; - while(X >0)Wuyi { theRET + =Tree[x]; -X-=lowbit (x); Wu } - returnret; About } $ intAddintXintd) - { - while(x <=N) - { ATREE[X] + =D; +X + =lowbit (x); the } - } $ intMain () the { the intx, y; the Charc[5]; the while(SCANF ("%d", &n)! =EOF) - { inMEM (LEF,0); theMEM (Rig,0); theMEM (S,0); AboutMEM (Tree,0); the for(inti =0; i < MAXN; i++) a[i].clear (); the for(inti =1; I < n; i++) the { +scanf"%d%d", &x, &y); - a[x].push_back (y); the }Bayitot =1; theDfs1); the for(inti =1; I <= N; i++) - { -S[i] =1; theAdd (I,1); the } thescanf"%d", &m); the while(m--) - { thescanf"%s%d", C, &x); the if(c[0] =='Q') the {94cout << sum (rig[x])-sum (lef[x]-1) <<Endl; the } the Else if(c[0] =='C') the {98 if(S[x]) Add (lef[x],-1); About ElseAdd (Lef[x],1); -S[X] =!s[x];///Update this point if there is an Apple101 }102 }103 }104 return 0; the}
POJ-3321 Apple Tree---+dfs sequence