知道了并查集写的问题后,我也明白了为什么之前这道题TLE的原因;
有这道题的合并操作不难想到用并查集维护;
由于并查集易于向上查询而不易于向下查询
所以对于询问方块x下面有多少个方块,我们可以转化为立方柱的规模-x上方的方块数-1
所以我们可以再维护这两个域即可
var fa,s,up:array[..] of longint;
k1,k2,m,n,x,y,i:longint;
ch:char; function getf(x:longint):longint;
var f:longint;
begin
if fa[x]<>x then
begin
f:=getf(fa[x]);
up[x]:=up[x]+up[fa[x]];
fa[x]:=f;
end;
exit(fa[x]);
end; function getu(x:longint):longint;
begin
k2:=;
while fa[x]<>x do
begin
k2:=k2+up[x];
x:=fa[x];
end;
exit(x);
end; begin
readln(m);
for i:= to do
begin
fa[i]:=i;
s[i]:=;
up[i]:=;
end;
for i:= to m do
begin
read(ch);
if ch='M' then
begin
readln(x,y);
k1:=getf(x);
k2:=getf(y);
fa[k2]:=k1;
up[k2]:=s[k1];
s[k1]:=s[k1]+s[k2];
end
else begin
readln(x);
k1:=getu(x);
writeln(s[k1]-k2-);
end;
end;
end.