Apple Tree POJ - 3321 dfs序列构造树状数组(好题)

弱龄寄事外,委怀在琴书。这篇文章主要讲述Apple Tree POJ - 3321 dfs序列构造树状数组(好题)相关的知识,希望能为你提供帮助。
There is an apple tree outside of kaka‘s house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very much, so he has been carefully nurturing the big apple tree.
The tree has  N  forks which are connected by branches. Kaka numbers the forks by 1 to  N  and the root is always numbered by 1. Apples will grow on the forks and two apple won‘t grow on the same fork. kaka wants to know how many apples are there in a sub-tree, for his study of the produce ability of the apple tree.
The trouble is that a new apple may grow on an empty fork some time and kaka may pick an apple from the tree for his dessert. Can you help kaka?
 

Apple Tree POJ - 3321 dfs序列构造树状数组(好题)

文章图片
 
Input
The first line contains an integer  N  (N  ≤ 100,000) , which is the number of the forks in the tree.
The following  N  - 1 lines each contain two integers  u  and  v, which means fork  u  and fork  vare connected by a branch.
The next line contains an integer  M  (M  ≤ 100,000).
The following  M  lines each contain a message which is either
"C  x" which means the existence of the apple on fork  x  has been changed. i.e. if there is an apple on the fork, then Kaka pick it; otherwise a new apple has grown on the empty fork.
or
"Q  x" which means an inquiry for the number of apples in the sub-tree above the fork  x, including the apple (if exists) on the fork x
Note the tree is full of apples at the beginning
Output
For every inquiry, output the correspond answer per line.Sample Input
3 1 2 1 3 3 Q 1 C 2 Q 1

Sample Output
3 2

这题关键就是用dfs序列 构建树状数组
这个是多叉树,所以你需要注意L,R


1 #include < cstdio> 2 #include < cstring> 3 #include < queue> 4 #include < cmath> 5 #include < algorithm> 6 #include < set> 7 #include < iostream> 8 #include < map> 9 #include < stack> 10 #include < string> 11 #include < vector> 12 #define pi acos(-1.0) 13 #define eps 1e-6 14 #define fi first 15 #define se second 16 #define lson l,m,rt< < 1 17 #define rson m+1,r,rt< < 1|1 18 #define bugprintf("****** ") 19 #define mem(a,b)memset(a,b,sizeof(a)) 20 #define fuck(x)cout< < "["< < x< < "]"< < endl 21 #define f(a)a*a 22 #define sf(n)scanf("%d", & n) 23 #define sff(a,b)scanf("%d %d", & a, & b) 24 #define sfff(a,b,c) scanf("%d %d %d", & a, & b, & c) 25 #define pfprintf 26 #define FRE(i,a,b)for(i = a; i < = b; i++) 27 #define FREE(i,a,b) for(i = a; i > = b; i--) 28 #define FRL(i,a,b)for(i = a; i < b; i++) 29 #define FRLL(i,a,b) for(i = a; i > b; i--) 30 #define FIN freopen("DATA.txt","r",stdin) 31 #define lowbit(x)x& -x 32 #pragma comment (linker,"/STACK:102400000,102400000") 33 34 using namespace std; 35 const int maxn = 1e5 + 10; 36 typedef long long LL; 37 int n, m, lefeid[maxn], rightid[maxn], c[maxn]; 38 int tot, dfscnt, head[maxn], tree[maxn]; 39 struct node { 40int v, nxt; 41 } edge[maxn < < 2]; 42 void init() { 43tot = 0; 44mem(head, -1); 45 } 46 void add(int u, int v) { 47edge[tot].v = v; 48edge[tot].nxt = head[u]; 49head[u] = tot++; 50 } 51 void update(int x, int d) { 52while(x < = n) { 53c[x] += d; 54x += lowbit(x); 55} 56 } 57 int sum(int x) { 58int ret = 0; 59while(x > 0) { 60ret += c[x]; 61x -= lowbit(x); 62} 63return ret; 64 } 65 void dfs(int u, int fa) { 66lefeid[u] = dfscnt + 1; 67for (int i = head[u]; ~i ; i = edge[i].nxt ) { 68int v = edge[i].v; 69if (v != fa) dfs(v, u); 70} 71rightid[u] = ++dfscnt; 72 } 73 int main() { 74while(~scanf("%d", & n)) { 75init(); 76for (int i = 1; i < = n; i++) { 77tree[i] = 1; 78update(i, 1); 79} 80for (int i = 1 ; i < n ; i++) { 81int u, v; 82scanf("%d%d", & u, & v); 83add(u, v); 84add(v, u); 85} 86dfscnt = 0; 87dfs(1, -1); 88scanf("%d", & m); 89while(m--) { 90char op[10]; 91int x, l, r; 92scanf("%s%d", op, & x) ; 93l = lefeid[x], r = rightid[x]; 94if (op[0] == ‘C‘) { 95if (tree[r]) { 96tree[r] = 0; 97update(r, -1); 98} else { 99tree[r] = 1; 100update(r, 1); 101} 102} else printf("%d ", sum(r) - sum(l - 1)); 103} 104} 105return 0; 106 }

【Apple Tree POJ - 3321 dfs序列构造树状数组(好题)】 













    推荐阅读