1. 程式人生 > >bzoj2157: 旅遊

bzoj2157: 旅遊

pda sdi def val include query == ret acc

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),輸出答案。

Sample Input

3
0 1 1
1 2 2
8
SUM 0 2
MAX 0 2
N 0 1
SUM 0 2
MIN 0 2
C 1 3
SUM 0 2
MAX 0 2

Sample Output

3
2
1
-1
5
3

HINT

一共有10 個數據,對於第i (1 <= i <= 10) 個數據, N = M = i * 2000。

題解:   LCT或樹剖模板題··· code:
 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cmath>
 4
#include<cstring> 5 #include<algorithm> 6 using namespace std; 7 char ch; bool ok; 8 void read(int &x){ 9 for (ok=0,ch=getchar();!isdigit(ch);ch=getchar()) if (ch==-) ok=1; 10 for (x=0;isdigit(ch);x=x*10+ch-0,ch=getchar()); 11 if (ok) x=-x; 12 } 13 const int maxn=200005; 14 const int inf=0x3f3f3f3f; 15 int n,a,b,c,q; 16 char op[10]; 17 struct LCT{ 18 #define ls son[x][0] 19 #define rs son[x][1] 20 int fa[maxn],son[maxn][2],neg[maxn],rev[maxn],val[maxn],sum[maxn],res[maxn][2];//0:min 1:max 21 void init(){res[0][0]=inf,res[0][1]=-inf;} 22 int which(int x){return son[fa[x]][1]==x;} 23 bool isroot(int x){return son[fa[x]][0]!=x&&son[fa[x]][1]!=x;} 24 void addtag_rev(int x){if (x) rev[x]^=1,swap(ls,rs);} 25 void addtag_neg(int x){ 26 if (x){ 27 neg[x]^=1,val[x]=-val[x],sum[x]=-sum[x]; 28 swap(res[x][0],res[x][1]),res[x][0]=-res[x][0],res[x][1]=-res[x][1]; 29 } 30 } 31 void pushdown(int x){ 32 if (rev[x]) addtag_rev(ls),addtag_rev(rs),rev[x]=0; 33 if (neg[x]) addtag_neg(ls),addtag_neg(rs),neg[x]=0; 34 } 35 void relax(int x){ 36 if (!isroot(x)) relax(fa[x]); 37 pushdown(x); 38 } 39 void updata(int x){ 40 sum[x]=sum[ls]+val[x]+sum[rs]; 41 res[x][0]=min(res[ls][0],res[rs][0]); 42 res[x][1]=max(res[ls][1],res[rs][1]); 43 if (x>n) res[x][0]=min(res[x][0],val[x]),res[x][1]=max(res[x][1],val[x]); 44 } 45 void rotate(int x){ 46 int y=fa[x],z=fa[y],d=which(x),dd=which(y); 47 fa[son[x][d^1]]=y,son[y][d]=son[x][d^1],fa[x]=fa[y]; 48 if (!isroot(y)) son[z][dd]=x; 49 son[x][d^1]=y,fa[y]=x,updata(y); 50 } 51 void splay(int x){ 52 relax(x); 53 while (!isroot(x)){ 54 if (isroot(fa[x])) rotate(x); 55 else if (which(fa[x])==which(x)) rotate(fa[x]),rotate(x); 56 else rotate(x),rotate(x); 57 } 58 updata(x); 59 } 60 void access(int x){for (int p=0;x;x=fa[x]) splay(x),son[x][1]=p,p=x;} 61 void make_root(int x){access(x),splay(x),addtag_rev(x);} 62 void init(int id,int v){int x=n+id; val[x]=sum[x]=res[x][0]=res[x][1]=v;} 63 void connect(int u,int v,int id,int w){make_root(u),fa[u]=n+id,fa[n+id]=v,init(id,w);} 64 int query_sum(int u,int v){make_root(u),access(v),splay(v);return sum[v];} 65 int query_min(int u,int v){make_root(u),access(v),splay(v);return res[v][0];} 66 int query_max(int u,int v){make_root(u),access(v),splay(v);return res[v][1];} 67 void change(int x,int v){splay(x),val[x]=v,updata(x);} 68 void _neg(int u,int v){make_root(u),access(v),splay(v),addtag_neg(v);} 69 }T; 70 int main(){ 71 read(n),T.init(); 72 for (int i=1;i<n;i++) read(a),read(b),read(c),T.connect(++a,++b,i,c); 73 for (read(q);q;q--){ 74 scanf("%s",op+1),read(a),read(b); 75 if (op[1]==C) T.change(n+a,b); 76 else if (op[1]==N) T._neg(++a,++b); 77 else if (op[1]==S) printf("%d\n",T.query_sum(++a,++b)); 78 else if (op[2]==I) printf("%d\n",T.query_min(++a,++b)); 79 else if (op[2]==A) printf("%d\n",T.query_max(++a,++b)); 80 } 81 return 0; 82 }

bzoj2157: 旅遊