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[300000],h[300000];
 5 void add(int x,int y)
 6 {
 7     while(x<=N)
 8     {
 9         a[x]+=y;
10         x+=x&-x;
11     }
12 }
13 int get(int x)
14 {
15     int ans=0;
16     while(x)
17     {
18         ans+=a[x];
19         x-=x&-x;
20     }
21     return ans;
22 }
23 void build(int now)
24 {
25     l[++N]=now;pos[now]=N;
26     for(int i=son[now];i;i=bro[i])
27         h[i]=h[now]+1,build(i);
28     end[now]=N+1;
29 }
30 int main()
31 {
32     scanf("%d",&n);
33     for(int i=1;i<n;i++)
34     {
35         scanf("%d%d",&p,&q);
36         if(p>q) swap(p,q);
37         bro[q]=son[p];son[p]=q;
38     }
39     build(1);
40     scanf("%d",&m);
41     for(int i=1;i<=n+m-1;i++)
42     {
43         char ch=getchar();
44         while(ch!='A' && ch!='W')
45             ch=getchar();
46         if(ch=='A')
47         {
48             scanf("%d%d",&p,&q);
49             if(p>q) swap(p,q);
50             add(pos[q],1),add(end[q],-1);
51         }
52         if(ch=='W')
53         {
54             scanf("%d",&p);
55             printf("%d\n",h[p]-get(pos[p]));
56         }
57     }
58     return 0;
59 } 

 

posted @ 2016-08-18 20:57  汪立超  阅读(441)  评论(0编辑  收藏  举报