[hdu4010]: Query on The Trees

  大概是有史以来调LCT调得最惨的一次了。。因为删边那里判断是否合法时少了个条件。。调了整个晚上>_<。。。。

  被模版题教做人了QAQ。。

  1 #include<cstdio>
  2 #include<iostream>
  3 #include<cstring>
  4 using namespace std;
  5 const int maxn=300233;
  6 struct zs{
  7     int too,pre;
  8 }e[maxn<<1];int tot,last[maxn],dl[maxn];
  9 int ch[maxn][2],fa[maxn],mxv[maxn],v[maxn],add[maxn],st[maxn],top;
 10 bool rev[maxn];
 11 int i,j,k,n,m,x,y,w;
 12 
 13 int ra;char rx;
 14 inline int read(){
 15     rx=getchar(),ra=0;
 16     while(rx<'0'||rx>'9')rx=getchar();
 17     while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,rx=getchar();return ra;
 18 }
 19 
 20 inline void insert(int a,int b){
 21     e[++tot].too=b,e[tot].pre=last[a],last[a]=tot;
 22     e[++tot].too=a,e[tot].pre=last[b],last[b]=tot;
 23 }
 24 
 25 inline int max(int a,int b){return a>b?a:b;}
 26 inline bool isrt(int x){
 27     return ch[fa[x]][0]!=x&&ch[fa[x]][1]!=x;
 28 }
 29 inline void pushdown(int x){
 30     int l=ch[x][0],r=ch[x][1];
 31     if(rev[x]){
 32         swap(ch[x][0],ch[x][1]),
 33         rev[l]^=1,rev[r]^=1,rev[x]=0;
 34     }
 35     if(add[x]){
 36         if(l)add[l]+=add[x],mxv[l]+=add[x],v[l]+=add[x];
 37         if(r)add[r]+=add[x],mxv[r]+=add[x],v[r]+=add[x];
 38         add[x]=0;
 39     }
 40 }
 41 inline void upd(int x){
 42     mxv[x]=max(mxv[ch[x][0]],mxv[ch[x][1]]);
 43     if(mxv[x]<v[x])mxv[x]=v[x];
 44 }
 45 inline void rotate(int x){
 46     int f=fa[x],gfa=fa[f],l=ch[f][1]==x,r=l^1;
 47     if(!isrt(f))ch[gfa][ch[gfa][1]==f]=x;
 48     fa[ch[f][l]=ch[x][r]]=f,fa[fa[ch[x][r]=f]=x]=gfa,
 49     upd(f);
 50 }
 51 inline void splay(int x){
 52     int f=x,gfa;
 53     for(st[top=1]=f;!isrt(f);)st[++top]=(f=fa[f]);
 54     while(top)pushdown(st[top--]);
 55     while(!isrt(x)){
 56         f=fa[x],gfa=fa[f];
 57         if(!isrt(f))
 58             rotate(((ch[f][1]==x)^(ch[gfa][1]==f))?x:f);
 59         rotate(x);
 60     }
 61     upd(x);
 62 }
 63 inline void access(int x){
 64     for(int rc=0;x;rc=x,x=fa[x])
 65         splay(x),ch[x][1]=rc,upd(x);
 66 }
 67 inline void makert(int x){
 68     access(x),splay(x),rev[x]^=1;
 69 }
 70 inline void link(int x,int y){
 71     makert(x),fa[x]=y;
 72 }
 73 inline void cut(int x,int y){
 74     makert(x),access(y),splay(y),fa[ch[y][0]]=0,ch[y][0]=0,upd(y);
 75 }
 76 inline int getfa(int x){
 77     for(access(x),splay(x);ch[x][0];x=ch[x][0]);
 78     return x;
 79 }
 80 
 81 int main(){
 82     mxv[0]=-200023333;bool first=1;
 83     while(scanf("%d",&n)==1){
 84         if(!first)
 85             for(i=0;i<=n;i++)rev[i]=add[i]=ch[i][0]=ch[i][1]=last[i]=0;
 86         else first=0;
 87         tot=0;
 88         
 89         for(i=1;i<n;i++)
 90             x=read(),y=read(),insert(x,y);
 91         for(i=1;i<=n;i++)v[i]=mxv[i]=read();
 92         int l=0,r=1,now;dl[1]=1;fa[1]=0;
 93         
 94         while(l<r){
 95             now=dl[++l];
 96             for(i=last[now];i;i=e[i].pre)if(e[i].too!=fa[now])
 97                 fa[dl[++r]=e[i].too]=now;
 98         }
 99         
100         m=read();char id;
101         while(m--){
102             for(id=getchar();id<'0'||id>'9';id=getchar());
103             if(id=='3')w=read();
104             x=read(),y=read();bool sm=(getfa(x)==getfa(y));
105             if(id=='1')
106                 if(sm)puts("-1");
107                 else link(x,y);
108             if(id=='2')
109                 if(!sm||x==y)puts("-1");
110                 else cut(x,y);
111             if(id=='3')
112                 if(!sm)puts("-1");
113                 else makert(x),access(y),splay(y),add[y]+=w,mxv[y]+=w,v[y]+=w;
114             if(id=='4')
115                 if(!sm)puts("-1");
116                 else makert(x),access(y),splay(y),printf("%d
",mxv[y]);
117         }
118         puts("");
119     }
120     return 0;
121 }
View Code
原文地址:https://www.cnblogs.com/czllgzmzl/p/5243103.html