18.10.8 POJ 3321 Apple Tree

描述

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 Nforks which are connected by branches. Kaka numbers the forks by 1 to Nand 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?

输入

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 v are 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
"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
"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

输出

For every inquiry, output the correspond answer per line.

样例输入

3
1 2
1 3
3
Q 1
C 2
Q 1

样例输出

3
2
 1 #include <iostream>
 2 #include <string.h>
 3 #include <algorithm>
 4 #include <stack>
 5 #include <string>
 6 #include <math.h>
 7 #include <queue>
 8 #include <stdio.h>
 9 #include <string.h>
10 #include <vector>
11 
12 using namespace std;
13 const int maxn = 100005;
14 vector<vector<int>> app(maxn);
15 int c[maxn],start[maxn],End[maxn],tree[maxn];
16 int n,time0=1;
17 
18 void dfs(int x) {
19     start[x] = time0;
20     int size = app[x].size();
21     for (int i = 0; i < size; i++)
22     {
23         time0++;
24         dfs(app[x][i]);
25     }
26     End[x] = time0;
27 }
28 
29 int lowbit(int x) {
30     return x & (-x);
31 }
32 
33 void change(int x,int d) {
34     while (x <= n) {
35         c[x] += d;
36         x += lowbit(x);
37     }
38 }
39 
40 int getsum(int x) {
41     int sum = 0;
42     while (x > 0) {
43         sum += c[x];
44         x -= lowbit(x);
45     }
46     return sum;
47 }
48 
49 void init() {
50     scanf("%d", &n);
51     for (int i = 1; i <= n - 1; i++) {
52         int u, v;
53         scanf("%d%d", &u, &v);
54         app[u].push_back(v);
55     }
56     dfs(1);
57     for (int i = 1; i <= n; i++)
58     {
59         tree[i] = 1;
60         change(i, 1);
61     }
62     int cishu;
63     scanf("%d", &cishu);
64     while (cishu--) {
65         char ch;
66         cin >> ch;
67         if (ch == 'C') {
68             int x;
69             scanf("%d", &x);
70             if (tree[x]) {
71                 change(start[x], -1);
72                 tree[x] -= 1;
73             }
74             else {
75                 change(start[x], 1);
76                 tree[x] += 1;
77             }
78         }
79         else if (ch == 'Q') {
80             int x;
81             scanf("%d", &x);
82             printf("%d\n", getsum(End[x]) - getsum(start[x] - 1));
83         }
84     }
85 }
86 
87 int main()
88 {
89     init();
90     return 0;
91 }
View Code

因为把left[x]写成x错了很久

显然这题还有栈解法,但我懒得写了,直接dfs写起来比较快(

树状数组,利用dfs将各枝排序

这里是老师给的神奇做法:记下dfs时途经这个结点时的时间点(有两个-进入时和出去时),分别记录为start数组和end数组

将start数组的元素作为c数组的下标。

由于父亲结点的时间区间覆盖了子节点的时间区间,要求某个父节点的子树和,就是求所有start时间在父节点的时间区间内的结点的值总和

posted @ 2018-10-08 18:02  TobicYAL  阅读(264)  评论(0编辑  收藏  举报