2157: 旅游

Description

Ray 乐忠于旅游,这次他来到了T 城。T 城是一个水上城市,一共有 N 个景点,有些景点之间会用一座桥连接。为了方便游客到达每个景点但又为了节约成本,T 城的任意两个景点之间有且只有一条路径。换句话说, T 城中只有N − 1 座桥。Ray 发现,有些桥上可以看到美丽的景色,让人心情愉悦,但有些桥狭窄泥泞,令人烦躁。于是,他给每座桥定义一个愉悦度w,也就是说,Ray 经过这座桥会增加w 的愉悦度,这或许是正的也可能是负的。有时,Ray 看待同一座桥的心情也会发生改变。现在,Ray 想让你帮他计算从u 景点到v 景点能获得的总愉悦度。有时,他还想知道某段路上最美丽的桥所提供的最大愉悦度,或是某段路上最糟糕的一座桥提供的最低愉悦度。

Input

输入的第一行包含一个整数N,表示T 城中的景点个数。景点编号为 0...N − 1。接下来N − 1 行,每行三个整数u、v 和w,表示有一条u 到v,使 Ray 愉悦度增加w 的桥。桥的编号为1...N − 1。|w| <= 1000。输入的第N + 1 行包含一个整数M,表示Ray 的操作数目。接下来有M 行,每行描述了一个操作,操作有如下五种形式: C i w,表示Ray 对于经过第i 座桥的愉悦度变成了w。 N u v,表示Ray 对于经过景点u 到v 的路径上的每一座桥的愉悦度都变成原来的相反数。 SUM u v,表示询问从景点u 到v 所获得的总愉悦度。 MAX u v,表示询问从景点u 到v 的路径上的所有桥中某一座桥所提供的最大愉悦度。 MIN u v,表示询问从景点u 到v 的路径上的所有桥中某一座桥所提供的最小愉悦度。测试数据保证,任意时刻,Ray 对于经过每一座桥的愉悦度的绝对值小于等于1000。

Output

对于每一个询问(操作S、MAX 和MIN),输出答案。

 

LCT....把边拆成点,因为这道题是一个树,所以每个点为它到它的父亲的那条边的值,然后就好了。。。

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

 

posted @ 2016-07-11 16:44  HTWX  阅读(110)  评论(0编辑  收藏  举报