Apple Tree POJ - 3321 dfs序列構造樹狀數組(好題)

來源:互聯網
上載者:User

標籤:center   using   can   OWIN   mem   space   nss   pac   nod   

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?

 

 

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

Output

For every inquiry, output the correspond answer per line.

Sample Input

31 21 33Q 1C 2Q 1

Sample Output

32

這題關鍵就是用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 bug         printf("******\n") 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 pf          printf 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 { 40     int v, nxt; 41 } edge[maxn << 2]; 42 void init() { 43     tot = 0; 44     mem(head, -1); 45 } 46 void add(int u, int v) { 47     edge[tot].v = v; 48     edge[tot].nxt = head[u]; 49     head[u] = tot++; 50 } 51 void update(int x, int d) { 52     while(x <= n) { 53         c[x] += d; 54         x += lowbit(x); 55     } 56 } 57 int sum(int x) { 58     int ret = 0; 59     while(x > 0) { 60         ret += c[x]; 61         x -= lowbit(x); 62     } 63     return ret; 64 } 65 void dfs(int u, int fa) { 66     lefeid[u] = dfscnt + 1; 67     for (int i = head[u]; ~i ; i = edge[i].nxt ) { 68         int v = edge[i].v; 69         if (v != fa) dfs(v, u); 70     } 71     rightid[u] = ++dfscnt; 72 } 73 int main() { 74     while(~scanf("%d", &n)) { 75         init(); 76         for (int i = 1; i <= n; i++) { 77             tree[i] = 1; 78             update(i, 1); 79         } 80         for (int i = 1 ; i < n ; i++) { 81             int u, v; 82             scanf("%d%d", &u, &v); 83             add(u, v); 84             add(v, u); 85         } 86         dfscnt = 0; 87         dfs(1, -1); 88         scanf("%d", &m); 89         while(m--) { 90             char op[10]; 91             int x, l, r; 92             scanf("%s%d", op, &x) ; 93             l = lefeid[x], r = rightid[x]; 94             if (op[0] == ‘C‘) { 95                 if (tree[r]) { 96                     tree[r] = 0; 97                     update(r, -1); 98                 } else { 99                     tree[r] = 1;100                     update(r, 1);101                 }102             } else printf("%d\n", sum(r) - sum(l - 1));103         }104     }105     return 0;106 }

 

Apple Tree POJ - 3321 dfs序列構造樹狀數組(好題)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.