bzoj2157: 旅游
又是lct裸题(据说暴力能过?)
写着略蛋疼
1 #include<iostream> 2 #include<cstring> 3 #include<cstdlib> 4 #include<cstdio> 5 #include<algorithm> 6 7 using namespace std; 8 9 const int Maxn=200010; 10 11 int ch[Maxn][2],w[Maxn],sum[Maxn],mn[Maxn],mx[Maxn],p[Maxn]; 12 bool flip[Maxn],rev[Maxn]; 13 14 bool isroot(int x) { 15 return ch[p[x]][0]!=x && ch[p[x]][1]!=x; 16 } 17 18 #define l ch[x][0] 19 #define r ch[x][1] 20 int n; 21 22 void update(int x) { 23 if(!x) return; 24 sum[x] = w[x] + sum[l] + sum[r]; 25 mn[x] = min(mn[l],mn[r]); 26 mx[x] = max(mx[l],mx[r]); 27 if(x>n) mn[x]=min(mn[x],w[x]), 28 mx[x]=max(mx[x],w[x]); 29 } 30 31 void rever(int x) { 32 sum[x]*=-1; 33 w[x]*=-1; 34 swap(mn[x],mx[x]); 35 mn[x]*=-1; 36 mx[x]*=-1; 37 rev[x]^=1; 38 } 39 40 void down(int x) { 41 if(!x) return; 42 if(flip[x]) { 43 swap(l,r); 44 flip[l]^=1; 45 flip[r]^=1; 46 flip[x]^=1; 47 } 48 if(rev[x]) { 49 rev[x] = 0; 50 if(l) rever(l); 51 if(r) rever(r); 52 } 53 } 54 55 56 #undef l 57 #undef r 58 59 void rotate(int x) { 60 int y=p[x],z=p[y]; 61 if(!isroot(y)) ch[z][ch[z][1]==y] = x; 62 int l=ch[y][1]==x,r=l^1; 63 p[x]=z; 64 p[y]=x; 65 p[ch[x][r]]=y; 66 67 ch[y][l]=ch[x][r]; 68 ch[x][r]=y; 69 70 update(y); 71 update(x); 72 } 73 74 void splay(int x) { 75 static int stk[Maxn],top; 76 stk[top=1]=x; 77 for(int t=x;!isroot(t);t=p[t]) stk[++top]=p[t]; 78 while(top) down(stk[top--]); 79 80 for(;!isroot(x);) { 81 int y=p[x],z=p[y]; 82 if(!isroot(y)) { 83 if( (ch[y][0]==x)^(ch[z][0]==y) )rotate(x); 84 rotate(y); 85 } 86 rotate(x); 87 } 88 } 89 90 void access(int x) { 91 for(int t=0;x;x=p[t=x]) { 92 splay(x); 93 ch[x][1]=t; 94 update(x); 95 } 96 } 97 98 int getroot(int x) { 99 for(access(x),splay(x);ch[x][0];x=ch[x][0]); 100 return x; 101 } 102 103 void newroot(int x) { 104 access(x); 105 splay(x); 106 flip[x]^=1; 107 } 108 109 void Link(int x,int y) { 110 newroot(x); 111 p[x]=y; 112 } 113 114 int m; 115 const int INF=int(1e9); 116 void init() { 117 scanf("%d",&n); 118 int u,v,W; 119 for(int i=0;i<=n;i++){ 120 mn[i]=INF,mx[i]=-INF; 121 } 122 for(int i=1;i<n;i++) { 123 scanf("%d%d%d",&u,&v,&W);u++,v++; 124 w[i+n]=W; 125 Link(u,i+n),Link(v,i+n); 126 } 127 } 128 129 void work() { 130 char opt[100]; 131 int x,y; 132 for(scanf("%d",&m);m--;) { 133 scanf("%s%d%d",opt,&x,&y); 134 if(opt[0]=='C') { 135 access(x+n); 136 splay(x+n); 137 w[x+n]=y; 138 update(x+n); 139 }else if(opt[0]=='N') { 140 x++,y++; 141 newroot(x); 142 access(y); 143 splay(y); 144 rever(y); 145 }else if(opt[0]=='S') { 146 x++,y++; 147 newroot(x); 148 access(y); 149 splay(y); 150 printf("%d\n",sum[y]); 151 }else if(opt[0]=='M'&&opt[1]=='A') { 152 x++,y++; 153 newroot(x); 154 access(y); 155 splay(y); 156 printf("%d\n",mx[y]); 157 }else /*if(opt[0]==e)*/ { 158 x++,y++; 159 newroot(x); 160 access(y); 161 splay(y); 162 printf("%d\n",mn[y]); 163 } 164 } 165 } 166 167 int main() { 168 #ifdef DEBUG 169 freopen("in.txt","r",stdin); 170 freopen("out.txt","w",stdout); 171 #endif 172 173 init(); 174 work(); 175 176 return 0; 177 }
原文出处http://www.cnblogs.com/showson/