zcmimi's blog
查看原题

点击跳转

lct缩点

LG 2542 [AHOI2005]航线规划有点像

缩点的时候顺便和并所有点的信息

#include<bits/stdc++.h>
#define il __inline__ __attribute__ ((always_inline))
il int min(int x,int y){return x<y?x:y;}
il int max(int x,int y){return x>y?x:y;}
namespace IN{const int str=1<<20;static char in_buf[str],*in_s,*in_t;bool __=0;inline char gc(){return (in_s==in_t)&&(in_t=(in_s=in_buf)+fread(in_buf,1,str,stdin)),in_s==in_t?EOF:*in_s++;}template<typename T>inline void in(T &x){if(__)return;char c=gc();bool f=0;while(c!=EOF&&(c<'0'||c>'9'))f^=(c=='-'),c=gc();if(c==EOF){__=1;return;}x=0;while(c!=EOF&&'0'<=c&&c<='9')x=x*10+c-48,c=gc();if(c==EOF)__=1;if(f)x=-x;}template<typename T,typename ... arr>inline void in(T &x,arr & ... y){in(x),in(y...);}}using namespace IN;
namespace OUT{const char ln='\n';const int str=1<<20;static char out_buf[str],*out_s=out_buf,*out_t=out_buf+str;inline void flush(){fwrite(out_buf,1,out_s-out_buf,stdout);out_s=out_buf;}inline void pt(char c){(out_s==out_t)?(fwrite(out_s=out_buf,1,str,stdout),*out_s++=c):(*out_s++=c);}inline void out(char c){pt(c);}template<typename T>inline void out(T x){if(!x){pt('0');return;}if(x<0)pt('-'),x=-x;char a[50],t=0;while(x)a[t++]=x%10,x/= 10;while(t--)pt(a[t]+'0');}template<typename T,typename ... arr>inline void out(T x,arr & ... y){out(x),out(y...);}}using namespace OUT;
const int N=150011,inf=2122219134;
int n,q,s[N],f[N],c[2][N],v[N],bl[N],a[N];
bool rev[N];
int BL(int x){return bl[x]==x?x:(bl[x]=BL(bl[x]));}
#define ls c[0][x]
#define rs c[1][x]
il void pu(int x){s[x]=s[ls]+s[rs]+v[x];}
il void pr(int x){rev[x]^=1,ls^=rs,rs^=ls,ls^=rs;}
il void pd(int x){
    if(rev[x]){
        if(ls)pr(ls);
        if(rs)pr(rs);
        rev[x]=0;
    }
}
il int g(int x){return c[1][f[x]]==x;}
il int nrt(int x){return c[0][f[x]]==x||c[1][f[x]]==x;}
il void rotate(int x){
    int y=f[x],z=f[y],l=g(x),r=!l,w=c[r][x];
    if(nrt(y))c[g(y)][z]=x;
    c[r][x]=y,c[l][y]=w;
    if(w)f[w]=y;
    f[x]=z,f[y]=x;
    pu(y),pu(x);
}
void pda(int x){if(nrt(x))pda(f[x]);pd(x);}
il void splay(int x){
    for(pda(x);nrt(x);rotate(x))if(nrt(f[x]))
        rotate(g(x)^g(f[x])?x:f[x]);
}
il void access(int x){
    for(int y=0;x;x=f[y=x]=BL(f[x]))
        splay(x),rs=y,pu(x);
}
il void mrt(int x){access(x),splay(x),pr(x);}
il int frt(int x){
    for(access(x),splay(x);ls;pd(x),x=ls);
    splay(x);return x;
}
int tmp;
void get(int x,int y){
    if(x)tmp+=v[x],bl[x]=y,get(ls,x),get(rs,x);
}
int main(){
    in(n,q);
    for(int i=1;i<=n;++i)in(a[i]),s[i]=v[i]=a[i],bl[i]=i;
    int opt,x,y;
    while(q--){
        in(opt,x,y);
        if(opt==1){
            x=BL(x),y=BL(y);
            if(x==y)continue;
            mrt(x);
            if(frt(y)!=x)f[x]=y;
            else tmp=0,get(rs,x),rs=0,v[x]+=tmp,pu(x);
        }
        if(opt==2){
            mrt(BL(x));
            v[BL(x)]+=y-a[x];
            s[BL(x)]+=y-a[x];
            a[x]=y;
        }
        if(opt==3){
            x=BL(x),y=BL(y);
            mrt(x);
            if(frt(y)!=x)out(-1,ln);
            else out(s[x],ln);
        }
    }
    flush();
}
BZ 2959 长跑
comment评论
Search
search