题解:

裸链剖/LCT

刚开始想把边转到点上,结果各种蛋疼,后来发现lct的话,好像在边上不难处理。。。

我个逗比忘了把v[x]取反了。。。

代码:

  1 #include<cstdio>
  2 #include<cstdlib>
  3 #include<cmath>
  4 #include<cstring>
  5 #include<algorithm>
  6 #include<iostream>
  7 #include<vector>
  8 #include<map>
  9 #include<set>
 10 #include<queue>
 11 #include<string>
 12 #define inf 2000000000
 13 #define maxn 400000+5
 14 #define maxm 100000+5
 15 #define eps 1e-10
 16 #define ll long long
 17 #define pa pair<int,int>
 18 #define for0(i,n) for(int i=0;i<=(n);i++)
 19 #define for1(i,n) for(int i=1;i<=(n);i++)
 20 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
 21 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
 22 #define for4(i,x) for(int i=head[x],y=e[i].go;i;i=e[i].next,y=e[i].go)
 23 #define mod 1000000007
 24 using namespace std;
 25 inline int read()
 26 {
 27     int x=0,f=1;char ch=getchar();
 28     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
 29     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
 30     return x*f;
 31 }
 32 int n,m,tot,tmp,ans,top,id[maxn],head[maxn],sta[maxn],v[maxn],sum[maxn],mi[maxn],mx[maxn],c[maxn][2],fa[maxn];
 33 bool tag[maxn];
 34 struct edge{int go,next,w;}e[maxn];
 35 inline bool isroot(int x)
 36 {
 37     return c[fa[x]][0]!=x&&c[fa[x]][1]!=x;
 38 }
 39 inline void pushup(int x)
 40 {
 41     int l=c[x][0],r=c[x][1];
 42     sum[x]=sum[l]+sum[r]+v[x];
 43     mi[x]=min(v[x],min(mi[l],mi[r]));
 44     mx[x]=max(v[x],max(mx[l],mx[r]));
 45 }
 46 inline void update(int x)
 47 {
 48     tag[x]^=1;
 49     v[x]=-v[x];
 50     sum[x]=-sum[x];
 51     int t=mi[x];
 52     mi[x]=-mx[x];
 53     mx[x]=-t;
 54 }
 55 inline void pushdown(int x)
 56 {
 57     int l=c[x][0],r=c[x][1];
 58     if(tag[x])
 59     {
 60         update(l);
 61         update(r);
 62         tag[x]=0;
 63     }
 64 }
 65 inline void rotate(int x)
 66 {
 67     int y=fa[x],z=fa[y],l=c[y][1]==x,r=l^1;
 68     if(!isroot(y))c[z][c[z][1]==y]=x;
 69     fa[x]=z;fa[y]=x;fa[c[x][r]]=y;
 70     c[y][l]=c[x][r];c[x][r]=y;
 71     pushup(y);pushup(x);
 72 }
 73 inline void splay(int x)
 74 {
 75     sta[++top]=x;
 76     for(int y=x;!isroot(y);y=fa[y])sta[++top]=fa[y];
 77     while(top)pushdown(sta[top--]);
 78     while(!isroot(x))
 79     {
 80         int y=fa[x],z=fa[y];
 81         if(!isroot(y))
 82         {
 83             if(c[z][1]==y^c[y][1]==x)rotate(x);
 84             else rotate(y);
 85         }
 86         rotate(x);
 87     }
 88 }
 89 inline void access(int x)
 90 {
 91     for(int y=0;x;x=fa[x])
 92     {
 93       splay(x);c[x][1]=y;pushup(x);y=x;
 94     }
 95 }
 96 inline void ask(int x,int y)
 97 {
 98     access(y);
 99     for(y=0;x;x=fa[x])
100     {
101         splay(x);
102         if(!fa[x])
103         {
104             if(tmp==1)update(c[x][1]),update(y);
105             if(tmp==2)ans=sum[c[x][1]]+sum[y];
106             if(tmp==3)ans=max(mx[c[x][1]],mx[y]);
107             if(tmp==4)ans=min(mi[c[x][1]],mi[y]);
108         }
109         c[x][1]=y;pushup(x);y=x;
110     }
111 }
112 inline void add(int x,int y,int w)
113 {
114     e[++tot]=(edge){y,head[x],w};head[x]=tot;
115     e[++tot]=(edge){x,head[y],w};head[y]=tot;
116 }
117 inline void dfs(int x)
118 {
119     for4(i,x)if(y!=fa[x])
120     {
121         id[i>>1]=y;fa[y]=x;
122         sum[y]=mi[y]=mx[y]=v[y]=e[i].w;
123         dfs(y);
124     }
125 }
126 int main()
127 {
128     freopen("input.txt","r",stdin);
129     freopen("output.txt","w",stdout);
130     n=read();
131     tot=1;
132     for1(i,n-1)
133     {
134         int x=read()+1,y=read()+1,w=read();
135         add(x,y,w);
136     }
137     dfs(1);
138     mi[0]=inf;mx[0]=-inf;
139     m=read();char ch[10];
140     while(m--)
141     {
142         scanf("%s",ch);int x=read()+1,y=read()+1;
143         if(ch[0]=='C')
144         {
145             x=id[x-1];y--;
146             splay(x);v[x]=y;pushup(x);
147         }else
148         {
149             if(ch[0]=='N')tmp=1;
150             else if(ch[0]=='S')tmp=2;
151             else if(ch[1]=='A')tmp=3;
152             else tmp=4;
153             ans=inf;
154             ask(x,y);
155             if(ans!=inf)printf("%d\n",ans);
156         }
157     }
158     return 0;
159 }
View Code

相关文章: