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?

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

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

原文地址:https://www.cnblogs.com/qldabiaoge/p/9415345.html

时间: 2024-07-30 12:48:32

Apple Tree POJ - 3321 dfs序列构造树状数组(好题)的相关文章

POJ 3321 DFS序+线段树

单点修改树中某个节点,查询子树的性质.DFS序 子树序列一定在父节点的DFS序列之内,所以可以用线段树维护. 1: /* 2: DFS序 +线段树 3: */ 4:   5: #include <cstdio> 6: #include <cstring> 7: #include <cctype> 8: #include <algorithm> 9: #include <vector> 10: #include <iostream> 1

POJ 2299 Ultra-QuickSort (离散化+树状数组)

题目链接:POJ 2299 Ultra-QuickSort 求一串序列相邻连个元素交换多少后,是一串上升的序列. 思路:求该串序列的逆序数,数据比较大,要离散化. AC代码: #include<stdio.h> #include<string.h> #include<set> #include<map> #include<algorithm> #define ll __int64 using namespace std; const ll max

POJ 2828 poj 2828 Buy Tickets 【树状数组,已知前n项和为K,返回n值】

题目链接:http://poj.org/problem?id=2828 在一个队列中,一个人想要插队,告诉你每个新来的人会插在i个人后面,求出最后的队列. 如果我们用模拟的话,那么时间复杂度肯定是超了:想想,如果我们逆序,那么最后来的人的位置一定是固定的,这样的话,我们将问题转化成逆序扫描给出数据,插在i个人后面这个数据就变成了在这个人前面需要留出多少个空位.如此我们只需要用树状数组记录前n项总共有多少个空位,每扫描一个数据,就找出能使得他前面正好有i个空位. 这题用树状数组或者线段树都可以,今

POJ 2464 Brownie Points II 树状数组+扫描线

题意奇葩的一笔,本质上就是一个复杂统计,智商低下的我想不出来只好去搜了题解 #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <set> #include <vector> #include <string> #include <queue> #include <deque> #inclu

【POJ 1195】 Mobile phones (树状数组)

[POJ 1195] Mobile phones (树状数组) Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 16761   Accepted: 7713 Description Suppose that the fourth generation mobile phone base stations in the Tampere area operate as follows. The ar

【不可能的任务1/200】bzoj1103树状数组水题

(卧槽,居然规定了修改的两点直接相连,亏我想半天) 非常水的题,用dfs序(而且不用重复,应该是直接规模为n的dfs序)+树状数组可以轻松水 收获:树状数组一遍A(没啥好骄傲的,那么简单的东西) 1 #include <cstdio> 2 #include <iostream> 3 using namespace std; 4 int N=0,n,m,p,q,a[300000],l[300000],pos[300000],end[300000],son[300000],bro[30

HDU1166 敌兵布阵 树状数组水题

中文题目,很简单的题目,区间求和,当然对于线段树来说也很水,为了练习一下树状数组,多做做水题吧,加深理解,并且打好基础,我算是被没打好基础给吓坏了,宁可多花几个小时 刷刷水题扎实点, 很裸的题目 操作也很裸,了解树状数组的肯定能做 #include<iostream> #include<cstdio> #include<list> #include<algorithm> #include<cstring> #include<string&g

POJ 2763 Housewife Wind(DFS序+LCA+树状数组)

Housewife Wind Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 11419   Accepted: 3140 Description After their royal wedding, Jiajia and Wind hid away in XX Village, to enjoy their ordinary happy life. People in XX Village lived in beauti

HDU 5156 - Harry and Christmas tree (dfs序+离线树状数组)

http://acm.hdu.edu.cn/showproblem.php?pid=5156 BC#25的C题. 题意是:给出一颗大小为n的树,以1为根,然后给出m次染色,每次将节点u加上一种颜色(一个节点可以有多个颜色). 最后查询树上每个节点对应子树上包含的不同颜色数量. 当时这场比赛没有做,回来看一下题目,没看标解就试着敲了一遍,于是解题思路从一开始就走上了不归路. 标解是O(n+m)的方法,主要思路是将问题转为:一次染色表示将u到根节点的路径都染上这种颜色. 但这样做需要去重,因为如果u