poj 3321

Description

There is an apple tree outside of kaka'shouse. Every autumn, a lot of apples will grow in the tree. Kaka likes applevery much, so he has been carefully nurturing the big apple tree.

The tree has N forks which areconnected by branches. Kaka numbers the forks by 1 to N and the rootis always numbered by 1. Apples will grow on the forks and two apple won't growon 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 onan empty fork some time and kaka may pick an apple from the tree for hisdessert. Can you help kaka?

Input

The first line contains aninteger N (N ≤ 100,000) , which is the number of the forks inthe tree.
The following N - 1 lines each contain twointegers u and v, which means fork u andfork 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 onfork x has been changed. i.e. if there is an apple on the fork, thenKaka 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 thesub-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 correspondanswer per line.

Sample Input

3

1 2

1 3

3

Q 1

C 2

Q 1

Sample Output

3

2

 

先建树,用前序遍历一遍,就可以了。

 

a: [1, 9]

b: [2, 6]

i: [3, 3]

f: [7, 7]

h: [8, 9]

就变成了改变值和求区间和了,用树状数组即可。

const 

 maxn=100000; 

 

var 

  e,n,dep,root:longint; 

  num,l,r,last,v,a:array[1..maxn]of longint; 

 side:array[1..maxn] of record 

    x,y,next:longint; 

  end; 

 

procedure add(x,y:longint); 

begin 

  inc(e); 

 side[e].x:=x; side[e].y:=y; side[e].next:=last[x]; last[x]:=e; 

end; 

 

procedure init; 

var 

  i,x,y:longint; 

begin 

  readln(n); 

  fillchar(v,sizeof(v),0); 

  fori:=1 to n-1 do 

  begin 

    readln(x,y); 

    add(x,y); 

    v[y]:=1; 

  end; 

  fori:=1 to n do 

    ifv[i]=0 then 

    begin 

     root:=i; 

     break; 

    end; 

end; 

 

function max(x,y:longint):longint; 

begin 

  ifx>y then exit(x) 

        else exit(y); 

end; 

 

function min(x,y:longint):longint; 

begin 

  ifx<y then exit(x) 

        else exit(y); 

end; 

 

function lowbit(x:longint):longint; 

begin 

 lowbit:=x and -x; 

end; 

 

procedure dfs(x:longint); 

var 

  i:longint; 

begin 

  inc(dep); 

  num[x]:=dep; 

  l[x]:=dep; 

  r[x]:=dep; 

  i:=last[x]; 

  whilei>0 do 

    withside[i] do 

    begin 

     dfs(y); 

     l[x]:=min(l[x],l[y]); 

     r[x]:=max(r[x],r[y]); 

     i:=next; 

    end; 

end; 

 

procedure updata(x,delta:longint); 

var 

  i:longint; 

begin 

  i:=x; 

  whilei<=n do 

  begin 

    a[i]:=a[i]+delta; 

    i:=i+lowbit(i); 

  end; 

end; 

 

function sum(x:longint):longint; 

var 

  i:longint; 

begin 

  sum:=0; 

  i:=x; 

  whilei>0 do 

  begin 

   sum:=sum+a[i]; 

    i:=i-lowbit(i); 

  end; 

end; 

 

procedure main; 

var 

  m,i,x,delta:longint; 

  c:char; 

begin 

  dfs(root); 

  fori:=1 to n do 

  begin 

    a[i]:=lowbit(i); 

    v[i]:=1; 

  end; 

  readln(m); 

  fori:=1 to m do 

  begin 

    read(c); 

    ifc='C' 

     then begin 

            readln(x); 

            if v[x]=1 

               then delta:=-1 

               else delta:=1; 

            v[x]:=1-v[x]; 

            updata(num[x],delta); 

          end 

     else begin 

            readln(x); 

            writeln(sum(r[x])-sum(l[x]-1)); 

          end; 

  end; 

end; 

 

begin 

 init; 

 main; 

end. 

 


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值