题意:有一颗树,1号点为根,保证编号小的点深度较小,初始状态每条边都没有被标记,要求实现两个操作在线:

A:将连接x,y的边标记

W:查询从1到x的路径上有多少条边未被标记

n<=2*10^5

思路:本题的特殊性质:

1.一次只标记一条边且没有重边

2.直接求1到x的路径,不用LCA

记录i点在DFS序中第一次与最后一次出现的时间b[i]与c[i]

可以发现若(x,y)(x<y)边被标记只对区间(b[y],c[y])有1的贡献

等价于前缀和s[b[y]]++ s[c[y]+1]--

至于s[c[y]+1]--还是s[c[y]]--并不重要,因为询问的肯定是s[a[i]]之类的,而DFS序上不会出现重复

单点修改,树状数组前缀和实现

 var head,vet,next,a,b,c,t,flag:array[..]of longint;
n,m,tot,i,x,y,time,j,k:longint;
ch:string; procedure add(a,b:longint);
begin
inc(tot);
next[tot]:=head[a];
vet[tot]:=b;
head[a]:=tot;
end; procedure dfs(u:longint);
var e,v:longint;
begin
flag[u]:=; inc(time); a[time]:=u; b[u]:=time;
e:=head[u];
while e<> do
begin
v:=vet[e];
if flag[v]= then dfs(v);
e:=next[e];
end;
inc(time); a[time]:=u; c[u]:=time;
end; function lowbit(x:longint):longint;
begin
exit(x and (-x));
end; function sum(x:longint):longint;
begin
sum:=;
while x> do
begin
sum:=sum+t[x];
x:=x-lowbit(x);
end;
end; procedure update(x,y:longint);
begin
while x<=n<< do
begin
t[x]:=t[x]+y;
x:=x+lowbit(x);
end;
end; begin
assign(input,'bzoj1103.in'); reset(input);
assign(output,'bzoj1103.out'); rewrite(output);
readln(n);
for i:= to n- do
begin
readln(x,y);
add(x,y);
add(y,x);
end;
dfs();
for i:= to n do
begin
update(b[i],);
update(c[i],-);
end;
readln(m);
for i:= to n+m- do
begin
readln(ch); x:=; y:=;
if ch[]='W' then
begin
for j:= to length(ch) do x:=x*+ord(ch[j])-ord('');
writeln(sum(b[x])-);
end;
if ch[]='A' then
begin
for j:= to length(ch) do
begin
if ch[j]=' ' then break;
x:=x*+ord(ch[j])-ord('');
end;
k:=j;
for j:=k+ to length(ch) do y:=y*+ord(ch[j])-ord('');
update(b[y],-);
update(c[y],);
end;
end;
close(input);
close(output);
end.
05-11 18:38