1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
| type graph=object tail:array[0..40010] of longint; b:array[0..200010] of record id,next:longint; end; ep:longint; procedure init(n:longint); function Create(x:longint):longint; procedure add(x,y:longint); end; function graph.Create(x:longint):longint; begin inc(ep); with b[ep] do begin id:=x; next:=-1; end; exit(ep); end; procedure graph.init(n:longint); var i:longint; begin ep:=0; for i:=1 to n do tail[i]:=Create(0); end; procedure graph.add(x,y:longint); begin b[tail[x]].next:=Create(y); tail[x]:=ep; end; var g:graph; p:array[0..40010,0..20] of longint; depth,lg2:array[0..40010] of longint; procedure da(cur,dep,pre:longint); var now,i:longint; begin depth[cur]:=dep; p[cur,0]:=pre; for i:=1 to lg2[dep] do p[cur,i]:=p[p[cur,i-1],i-1]; now:=cur; with g do while b[now].next<>-1 do begin now:=b[now].next; if b[now].id=pre then continue; da(b[now].id,dep+1,cur); end; end; function movedep(x,y:longint):longint; begin while y>0 do begin x:=p[x,lg2[y and -y]]; dec(y,y and -y); end; exit(x); end; function lca(x,y:longint):longint; var i:longint; begin if depth[x]>depth[y] then x:=movedep(x,depth[x]-depth[y]) else y:=movedep(y,depth[y]-depth[x]); for i:=20 downto 0 do if p[x,i]<>p[y,i] then begin x:=p[x,i]; y:=p[y,i]; end; if x=y then exit(x) else exit(p[x,0]); end; type pointer=^node; node=record left,right,mid,count:longint; lc,rc:pointer; end; var ld,rd,d,belong,tpre,xp,wp:array[0..40010] of longint; troot:array[0..40010] of pointer; all,ccc:longint; function build(bg,ed:longint):pointer; begin new(build); with build^ do begin left:=bg; right:=ed; mid:=(bg+ed)>>1; count:=ed-bg+1; lc:=nil; rc:=nil; if bg=ed then exit; lc:=build(bg,mid); rc:=build(mid+1,ed); end; end; procedure serere(root:pointer; pl,dt:longint); begin with root^ do begin inc(count,dt); if left=right then exit; if pl<=mid then serere(lc,pl,dt) else serere(rc,pl,dt); end; end; function query(root:pointer; bg,ed:longint):longint; begin with root^ do begin if (bg<=left)and(right<=ed) then exit(count); if ed<=mid then exit(query(lc,bg,ed)); if mid<bg then exit(query(rc,bg,ed)); exit(query(lc,bg,ed)+query(rc,bg,ed)); end; end; function query(x:longint):longint; begin exit(query(troot[x],ld[x],rd[x])); end; procedure dfs(cur,pre,dep:longint); var now:longint; begin inc(all); ld[cur]:=all; d[all]:=cur; inc(ccc,dep); now:=cur; with g do while b[now].next<>-1 do begin now:=b[now].next; if b[now].id=pre then continue; dfs(b[now].id,cur,dep+1); end; rd[cur]:=all; end; procedure buildup(root,_bel,z:longint); var i:longint; temp:pointer; begin all:=0; ccc:=0; dfs(root,-1,0); temp:=build(1,all); for i:=1 to all do begin troot[d[i]]:=temp; belong[d[i]]:=_bel; tpre[d[i]]:=z; end; end; procedure solve(n,where,st,cw,ed,zz,dd:longint); var z,dif,x,nw,dis,s1,s2:longint; begin z:=lca(ed,st); dis:=depth[ed]+depth[st]-depth[z]*2+1; wp[where]:=wp[where]+dis*zz+dd; s1:=query(st); while st<>z do begin st:=p[st,0]; s2:=query(st); if s1>s2 then x:=n-s2*2 else x:=s1*2-n; s1:=s2; nw:=wp[where]+x-zz; if nw>wp[where] then exit; wp[where]:=nw; xp[where]:=st; end; dif:=depth[ed]-depth[z]; while dif>0 do begin dec(dif); st:=movedep(ed,dif); s2:=query(st); if s1>s2 then x:=n-s2*2 else x:=s1*2-n; s1:=s2; nw:=wp[where]+x-zz; if nw>wp[where] then exit; wp[where]:=nw; xp[where]:=st; end; end; var _q:array[0..100010] of record ch:char; x,y:longint; end; procedure lemon(); var n,Q,i,j,x,y,zz,ceng,final,temp,tmp,_x:longint; ch:char; begin readln(n,Q); g.init(n); for i:=1 to Q do begin repeat read(ch); until ch in ['A','Q']; _q[i].ch:=ch; if ch='A' then begin readln(_q[i].x,_q[i].y); g.add(_q[i].x,_q[i].y); g.add(_q[i].y,_q[i].x); end; end; lg2[1]:=0; for i:=2 to 40000 do lg2[i]:=lg2[i>>1]+1; da(1,0,-1); g.init(n); final:=0; for i:=1 to n do buildup(i,i,-1); for i:=1 to n do begin xp[i]:=i; wp[i]:=0; end; for i:=1 to Q do if _q[i].ch='A' then begin x:=_q[i].x; y:=_q[i].y; if query(belong[x])<query(belong[y]) then begin temp:=x; x:=y; y:=temp; end; final:=final-wp[belong[x]]-wp[belong[y]]; buildup(y,belong[x],x); zz:=query(y); solve(query(belong[x]),belong[x],xp[belong[x]],wp[belong[x]],x,zz,ccc); serere(troot[x],ld[x],zz); ceng:=0; _x:=x; while troot[x]<>troot[belong[x]] do begin serere(troot[tpre[x]],ld[tpre[x]],zz); x:=tpre[x]; inc(ceng); end; x:=_x; g.add(x,y); g.add(y,x); final:=final+wp[belong[x]]; if ceng>100 then buildup(belong[x],belong[x],-1); end else writeln(final); end; begin lemon(); end.
|