首页 > 学院 > 开发设计 > 正文

[BZOJ1180][CROATIAN2009][LCT]OTOCI

2019-11-14 09:17:36
字体:
来源:转载
供稿:网友

LCT裸题吧….就是没有cut而已

#include <cstdio>#define N 50010int n,m;int v[N],f[N],ch[N][2],rev[N],fa[N],t[N],sta[N],tp;char op[20];inline char C(){ static char buf[100000],*p1=buf,*p2=buf; if(p1==p2){ p2=(p1=buf)+fread(buf,1,100000,stdin); if(p1==p2) return EOF; } return *p1++;}inline void reaD(int &x){ char Ch=C();x=0; for(;Ch>'9'||Ch<'0';Ch=C()); for(;Ch>='0'&&Ch<='9';x=x*10+Ch-'0',Ch=C());}inline void reaD(char *x){ char Ch=C();int len=0; for(;Ch>'z'||Ch<'a';Ch=C()); for(;Ch>='a'&&Ch<='z';x[++len]=Ch,Ch=C());}int fifa(int x){return x==fa[x]?x:fa[x]=fifa(fa[x]);}inline void swap(int &x,int &y){int z=x;x=y;y=z;}inline int isr(int x){return ch[f[x]][0]!=x&&ch[f[x]][1]!=x;}inline int isl(int x){return ch[f[x]][1]==x;}inline void pushdown(int x){ if(!(x&&rev[x])) return; swap(ch[x][0],ch[x][1]); if(ch[x][0]) rev[ch[x][0]]^=1; if(ch[x][1]) rev[ch[x][1]]^=1; rev[x]=0;}inline void upd(int x){ t[x]=v[x]; if(ch[x][0]) t[x]+=t[ch[x][0]]; if(ch[x][1]) t[x]+=t[ch[x][1]];}inline void rot(int x){ int y=f[x],z=f[y],lor=isl(x); if(!isr(y)) ch[z][ch[z][1]==y]=x; f[x]=z;ch[y][lor]=ch[x][lor^1]; if(ch[y][lor]) f[ch[y][lor]]=y; ch[x][lor^1]=y;f[y]=x; upd(y);upd(x);}inline void splay(int x){ sta[tp=1]=x; for(int i=x;!isr(i);i=f[i]) sta[++tp]=f[i]; for(;tp;tp--) pushdown(sta[tp]); for(;!isr(x);rot(x))if(!isr(f[x])) if(isl(x)^isl(f[x])) rot(x); else rot(f[x]);}inline void access(int x){int t=0;for(;x;t=x,x=f[x])splay(x),ch[x][1]=t,upd(x);}inline void reverse(int x){access(x);splay(x);rev[x]^=1;}inline int query(int x,int y){reverse(x);access(y);splay(y);return t[y];}inline void link(int x,int y){reverse(x);f[x]=y;splay(x);}inline void change(int x,int y){reverse(x);v[x]=y;upd(x);}int w[20],wt;inline void Pt(int x){ if(!x){putchar(48);putchar('/n');return;} while(x)w[++wt]=x%10,x/=10; for(;wt;wt--)putchar(48+w[wt]);putchar('/n');}int main(){ reaD(n); for(int i=1;i<=n;i++) reaD(v[i]),fa[i]=i; reaD(m); for(int i=1,x,y;i<=m;i++){ reaD(op);reaD(x);reaD(y); if(op[1]=='e') if(fifa(x)==fifa(y)) Pt(query(x,y)); else puts("impossible"); else if(op[1]=='b') if(fifa(x)==fifa(y)) puts("no"); else puts("yes"),link(x,y),fa[fifa(x)]=fifa(y); else change(x,y); }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表