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 }

时间: 2024-12-19 10:02:02

bzoj2157: 旅游的相关文章

bzoj2157旅游

题目: 2157: 旅游 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 569  Solved: 310[Submit][Status][Discuss] Description Ray 乐忠于旅游,这次他来到了T 城.T 城是一个水上城市,一共有 N 个景点,有些景点之间会用一座桥连接.为了方便游客到达每个景点但又为了节约成本,T 城的任意两个景点之间有且只有一条路径.换句话说, T 城中只有N − 1 座桥.Ray 发现,有些桥上可以看到美丽

[bzoj2157]旅游 (lct)

这个应该也算裸的模板题吧..主要是边权的问题,对于每条边u->v,我们可以新建一个节点代替他,把边的信息弄到新的点上,就变成u->x->v了... 当然了这样的话要防止u和v这些没用的点影响到实际的结果....这个可以初始化的时候解决 话说如果写链剖的话也可以用这样的姿势,就不用像以前那样特判lca了.. 上传的时候少更新了一个值结果调了半天= = 1 #include<cstdio> 2 #include<iostream> 3 #include<math

【树链剖分】【线段树】bzoj2157 旅游

#include<cstdio> #include<algorithm> using namespace std; #define INF 2147483647 #define N 20001 #define lson rt<<1,l,m #define rson rt<<1|1,m+1,r int v[N<<1],first[N],next[N<<1],en,bw[N<<1],dw[N<<1]; void A

BZOJ2157: 旅游 树链剖分 线段树

http://www.lydsy.com/JudgeOnline/problem.php?id=2157 在对树中数据进行改动的时候需要很多pushdown(具体操作见代码),不然会wa,大概原因和线段树区间修改需要很多pushup是一样的. 这个轻重链的方法特别好用,虽然第一次写树链剖分但是容易理解又有优秀复杂度的结构让人情不自禁orz. (后来发现很久以前学lca的时候就学了树链剖分只不过忘了,mdzz) 因为忘了去掉测试代码的freopen,re了4次(虽然有三次就算不re也wa),发现一

【BZOJ2157】旅游 LCT

模板T,SB的DMoon..其实样例也是中国好样例...一开始不会复制,yangyang:找到“sample input”按住shift,按page down.... 1 #include <iostream> 2 #include <cstdio> 3 #define inf 0x7fffffff 4 #define N 20010 5 #define M 20010 6 using namespace std; 7 int n,m; 8 struct node *null; 9

【bzoj2157】【旅游】

2157: 旅游 Time Limit: 10 Sec Memory Limit: 259 MB Submit: 419 Solved: 248 [Submit][Status][Discuss] Description Ray 乐忠于旅游,这次他来到了T 城.T 城是一个水上城市,一共有 N 个景点,有些景点之间会用一座桥连接.为了方便游客到达每个景点但又为了节约成本,T 城的任意两个景点之间有且只有一条路径.换句话说, T 城中只有N ? 1 座桥.Ray 发现,有些桥上可以看到美丽的景色,

【bzoj2157】旅游

题目描述 Ray 乐忠于旅游,这次他来到了T 城.T 城是一个水上城市,一共有 N 个景点,有些景点之间会用一座桥连接.为了方便游客到达每个景点但又为了节约成本,T 城的任意两个景点之间有且只有一条路径.换句话说, T 城中只有N ? 1 座桥. Ray 发现,有些桥上可以看到美丽的景色,让人心情愉悦,但有些桥狭窄泥泞,令人烦躁.于是,他给每座桥定义一个愉悦度w,也就是说,Ray 经过这座桥会增加w 的愉悦度,这或许是正的也可能是负的.有时,Ray 看待同一座桥的心情也会发生改变. 现在,Ray

【BZOJ2157】旅游 裸树链剖分

#include <stdio.h> int main() { puts("转载请注明出处[vmurder]谢谢"); puts("网址:blog.csdn.net/vmurder/article/details/43965845"); } 重写大发好!!!!! ****什么题解都没有,水题一道, 挂了就去调,调不过就去重写. 代码: #include <cstdio> #include <cstring> #include &l

广州去厦门旅游攻略--(转自 #散文吧网站#)

广州去厦门旅游攻略 发布时间:2016-12-11 17:30 厦门由厦门岛.离岛鼓浪屿.内陆九龙江南岸海沧半岛.集美半岛.翔安区以及同安等组成,陆地面积1699.39Km2,海域面积300多平方公里.厦门的主体--厦门岛南北长13.7公里,东西宽12.5公里,面积约为128.14Km2.是厦门的主要岛屿,也是厦门第一大岛屿.厦门岛是厦门经济特区的发祥地,岛上有厦门的商业和政治中心.各国殖民者最初居住的地方鼓浪屿就在厦门岛西南部.今天小编带给大家的是广州去厦门旅游攻略, 希望对大家有帮助. 厦门