码迷,mamicode.com
首页 > 编程语言 > 详细

POJ 3321 树状数组(+dfs+重新建树)

时间:2016-10-07 13:43:37      阅读:303      评论:0      收藏:0      [点我收藏+]

标签:

Apple Tree
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 27092   Accepted: 8033

Description

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

Source

POJ Monthly--2007.08.05, Huang, Jinsong
题意:
一棵苹果树,每个节点最多结一个苹果,最初每个节点都有一个苹果,非二叉树,有两种操作,Q X,表示X节点的子树共有几个苹果,C X,表示X节点摘掉一个苹果或长出一个苹果(有就摘,没有就长)
技术分享
代码:
  1 /*
  2 挺好的一道题,本题要重新建立树状数组,用dfs从树根开始搜,每搜到一个点他的左值记录该点的新序号,他的
  3 右值记录改点的子树中的最大深度,这样每个点就变成了树状数组中的C数组参考上图,然后套树状数组区间求和的模板就行了。
  4 注意本题如果用vector会很费时,vector<int>a[MAX]会超时,vector<vector<int> > a(MAX) 985ms过了(后面这个什么鬼!)。
  6 不如自己建树用时会更少。
  7 */
  8 #include<iostream>
  9 #include<cstdio>
 10 #include<vector>
 11 #include<cstring>
 12 using namespace std;
 13 const int MAX=100005;
 14 int c[MAX];
 15 int lef[MAX],rig[MAX];
 16 int n,m;
 17 int rot;
 18 int head[MAX];
 19 int lne;
 20 //vector<int>a[MAX];
 21 //vector<vector<int> > a(MAX);
 22 struct node
 23 {
 24     int to,next;
 25 }a[MAX];
 26 void tadd(int u,int v)
 27 {
 28     a[lne].to=v;
 29     a[lne].next=head[u];
 30     head[u]=lne++;
 31 }
 32 int lowbit(int x)
 33 {
 34     return x&(-x);
 35 }
 36 void add(int id,int val)
 37 {
 38     while(id<=n)
 39     {
 40         c[id]+=val;
 41         id+=lowbit(id);
 42     }
 43 }
 44 int sum(int id)
 45 {
 46     int sum=0;
 47     while(id>0)
 48     {
 49         sum+=c[id];
 50         id-=lowbit(id);
 51     }
 52     return sum;
 53 }
 54 void dfs(int x)
 55 {
 56     lef[x]=rot;
 57     //for(int i=0;i<a[x].size();i++)
 58     for(int i=head[x];i!=-1;i=a[i].next)
 59     {
 60         rot++;
 61         //dfs(a[x][i]);
 62         dfs(a[i].to);
 63     }
 64     rig[x]=rot;
 65 }
 66 int main()
 67 {
 68     int x,y;
 69     char ch[5];
 70     int flag[MAX];
 71     scanf("%d",&n);
 72     for(int i=1;i<=n;i++)
 73     {
 74         flag[i]=1;
 75         add(i,1);
 76     }
 77     lne=0;
 78     memset(head,-1,sizeof(head));
 79     for(int i=1;i<n;i++)
 80     {
 81         scanf("%d%d",&x,&y);
 82         //a[x].push_back(y);
 83         tadd(x,y);
 84     }
 85     rot=1;
 86     dfs(1);
 87     scanf("%d",&m);
 88     while(m--)
 89     {
 90         scanf("%s%d",ch,&x);
 91         if(ch[0]==C)
 92         {
 93             if(flag[x])
 94             add(lef[x],-1);
 95             else add(lef[x],1);
 96             flag[x]=!flag[x];
 97         }
 98         else
 99         {
100             printf("%d\n",sum(rig[x])-sum(lef[x]-1));
101         }
102     }
103     return 0;
104 }

 

POJ 3321 树状数组(+dfs+重新建树)

标签:

原文地址:http://www.cnblogs.com/--ZHIYUAN/p/5935636.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!