Topic links
Topic: A tree, dynamic plus edge, asking the number of paths passing through an edge
LCT: Maintaining subtree information
My Harvest: 2333
#include <bits/stdc++.h> using namespace std;
#define LS c[x][0] #define RS c[x][1] const int n=1e5+10;
int n,q;
int c[n][2],fa[n],size[n],sum[n],stk[n];
BOOL Rev[n]; bool F (int x) {return c[fa[x]][1]==x;} bool ISR (int x) {return c[fa[x]][0]!=x&&c[fa[x]][1]!=x;} void Update (int x)
{sum[x]=sum[ls]+sum[rs]+size[x]+1;} void reverse (int x) {if (!x) return, swap (LS,RS), rev[x]^=1; void pushdown (int x) {if (!rev[x]) return; reverse (LS); reverse
(rs); rev[x]=0;}
void rotate (int x) {int y = fa[x], z = fa[y], k =f (x); if (!isr (y)) c[z][f (y)] = x;
FA[X] = Z; C[Y][K] = c[x][!k];
Fa[c[y][k]] = y; C[x][!k] = y; Fa[y] = x;
Update (y);
} void maintain (int x) {int top=0;stk[++top]=x; for (;!
ISR (x); x=fa[x]) stk[++top]=fa[x];
while (top) pushdown (stk[top--]);
} void Splay (int x) {maintain (x); for (;!
ISR (x); rotate (x)) if (!isr (fa[x)) rotate (f (x) ==f (fa[x])? fa[x]:x);
Update (x); } void Access (int x) {for (int y=0;x;y=x,x=fa[x]) splay (x), size[x]+=sum[c[x][1]]-sum[y],c[x][1]=y,update (x);} void mkrt (int x) {access (x); splay (x); reverse (x);} void link (int x,int y) {mkrt (x); Mkrt (y); fa[x]=y;size[y]+=sum[x];
Update (y);}
void cut (int x,int y) {mkrt (x); Access (y); splay (y);}
void work () {char opt[5];int x, y;
while (q--) {scanf ("%s%d%d", opt,&x,&y);
if (opt[0]== ' A ') link (x, y);
else cut (x, y), printf ("%lld\n", 1ll*sum[x]* (Sum[y]-sum[x]));
}} void Init () {scanf ("%d%d", &n,&q);}
int main () {init ();
Work ();
return 0; }