4530: [Bjoi2014] Great fusion

Source: Internet
Author: User

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; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.