poj 3237 Tree 树链剖分
来源:互联网 发布:淘宝网禁止出售的药品 编辑:程序博客网 时间:2024/05/16 07:06
对于区间取反的操作,记录区间最大值max_num,最小值min_num,这个区间取反就是max_num = -max_num;min_num = - min_num; swap(max_num,min_num)就可以,就可以传递lazy值了。
#include <map>#include <set>#include <queue>#include <cmath>#include <cstdio>#include <cstring>#include <cstdlib>#include <iostream>#include <algorithm>using namespace std;const int MAXN = 100010;struct Edge { int to ; int id; int value; Edge * next;}E[MAXN*2],*EE;struct Gragh { Edge * first;}G[MAXN];struct Tree { int x,y; int max_num,min_num; int lazy;}t[MAXN<<2];int pre[MAXN],son[MAXN],siz[MAXN],dep[MAXN],top[MAXN],pos[MAXN],Rank[MAXN];int N,Q,S;int num[MAXN];int idx[MAXN];int tree_idx = 0;int tot ;void Init() { EE = E; dep[1] = 1; pre[1] = 0; tree_idx = 0; memset(G,0,sizeof(G)); memset(son,0,sizeof(son));}void addedge(int u,int v,int value,int id) { EE->to = v ; EE->next = G[u].first ; EE->value = value ; EE->id = id ; G[u].first = EE++; EE->to = u ; EE->next = G[v].first ; EE->value = value ; EE->id = id ; G[v].first = EE++;}void dfs1(int u) { siz[u] = 1; son[u] = 0; for(Edge * p = G[u].first ; p ; p = p -> next) { if(p->to != pre[u]) { pre[p->to] = u; dep[p->to] = dep[u] + 1; dfs1(p->to); siz[u] += siz[p->to]; if(siz[p->to] > siz[son[u]]) son[u] = p -> to; } }}void dfs2(int u,int ancestor) { top[u] = ancestor; tree_idx ++; pos[u] = tree_idx; if(son[u] != 0) { dfs2(son[u],ancestor); } for(Edge * p = G[u].first ; p ; p = p -> next) { if(p->to != pre[u] && p->to != son[u]) { dfs2(p->to,p->to); num[pos[p->to]] = p->value; idx[p->id] = pos[p->to]; } else if(p->to == son[u]) { num[pos[p->to]] = p->value; idx[p->id] = pos[p->to]; } }} void Push_Up(int rt) { t[rt].max_num = max(t[rt<<1].max_num,t[rt<<1|1].max_num); t[rt].min_num = min(t[rt<<1].min_num,t[rt<<1|1].min_num);}void Push_Down(int rt) { if(t[rt].lazy) { t[rt<<1].lazy ^= 1; t[rt<<1|1].lazy ^= 1; t[rt<<1].max_num = -t[rt<<1].max_num; t[rt<<1].min_num = -t[rt<<1].min_num; swap(t[rt<<1].max_num,t[rt<<1].min_num); t[rt<<1|1].max_num = -t[rt<<1|1].max_num; t[rt<<1|1].min_num = -t[rt<<1|1].min_num; swap(t[rt<<1|1].max_num,t[rt<<1|1].min_num); t[rt].lazy = 0; }}void Build(int x,int y,int rt) { t[rt].x = x ; t[rt].y = y; t[rt].lazy = 0; if(x == y) { t[rt].max_num = num[x]; t[rt].min_num = num[x]; return ; } int mid = (x + y) >> 1; Build(x,mid,rt<<1); Build(mid+1,y,rt<<1|1); Push_Up(rt);}void Update(int rt,int position,int value) { if(t[rt].x == t[rt].y) { t[rt].max_num = value; t[rt].min_num = value; return ; } int mid = (t[rt].x + t[rt].y) >> 1; Push_Down(rt); if(mid >= position) { Update(rt<<1,position,value); } else { Update(rt<<1|1,position,value); } Push_Up(rt);}void Ne_Update(int rt,int left,int right,int lazy) { if(t[rt].x >= left && t[rt].y <= right) { t[rt].lazy ^= 1; t[rt].max_num = -t[rt].max_num; t[rt].min_num = -t[rt].min_num; swap(t[rt].max_num,t[rt].min_num); return; } Push_Down(rt); int mid = (t[rt].x + t[rt].y) >> 1; if(mid >= left) { Ne_Update(rt<<1,left,right,lazy); } if(mid < right) { Ne_Update(rt<<1|1,left,right,lazy); } Push_Up(rt);}int ANS = 0;void Query(int rt,int left,int right) { if(left <= t[rt].x && right >= t[rt].y) { ANS = max(t[rt].max_num,ANS); return ; } int ans = 0; int mid = (t[rt].x + t[rt].y) >> 1; Push_Down(rt); if(mid >= left) { Query(rt<<1,left,right); } if(mid < right){ Query(rt<<1|1,left,right); } Push_Up(rt);}void Change(int x,int y,int lazy) { //printf("lazy : %d\n",lazy); while(top[x] != top[y]) { if(dep[top[x]] < dep[top[y]]) swap(x,y); Ne_Update(1,pos[top[x]],pos[x],lazy); x = pre[top[x]]; } if(x == y) return ; if(dep[x] > dep[y]) swap(x,y); Ne_Update(1,pos[x]+1,pos[y],lazy);}void Query_Tree(int x,int y) { int ans = 0; while(top[x] != top[y]) { if(dep[top[x]] < dep[top[y]]) swap(x,y); Query(1,pos[top[x]],pos[x]); x = pre[top[x]]; } if(x == y) return ; if(dep[x] > dep[y]) swap(x,y); Query(1,pos[x]+1,pos[y]);}// void print_tree(int rt) {// printf("x : %d y : %d lazy : %d\n",t[rt].x,t[rt].y,t[rt].lazy);// if(t[rt].x == t[rt].y) {// printf("%d ",t[rt].value);// return ;// }// print_tree(rt<<1);// print_tree(rt<<1|1);// }int ReadInt() { char c = getchar(); while(c > '9' || c < '0') c = getchar(); int ret = 0; while(c <= '9' && c >= '0') { ret = ret * 10 + c - '0'; c = getchar(); } return ret;}void input() { int u,v,value; for(int i = 1 ; i <= N - 1 ; i++) { scanf("%d %d %d",&u,&v,&value); addedge(u,v,value,i); } dfs1(1); dfs2(1,1); // for(int i = 1 ; i <= N ; i++) { // Rank[pos[i]] = num[i]; // } Build(1,N,1); char op[10]; int x,y; while(scanf("%s",op)) { if(op[0] == 'D') break; scanf("%d %d",&x,&y); if(op[0] == 'C') { Update(1,idx[x],y); } else if(op[0] == 'N') { Change(x,y,1); } else { ANS = -2147483648; Query_Tree(x,y); printf("%d\n",ANS); } }}int main(void) { freopen("a.in","r",stdin); int T; scanf("%d",&T); while(T--) { scanf("%d",&N); Init(); input(); //solve(); } return 0;}
0 0
- POJ 3237 Tree 树链剖分
- 【POJ】3237 Tree 树链剖分
- POJ 3237 Tree 树链剖分
- 【树链剖分】 POJ 3237 Tree
- poj 3237 Tree(树链剖分)
- POJ 3237 Tree (树链剖分)
- POJ 3237 Tree 树链剖分
- POJ 3237 Tree 树链剖分
- poj 3237 Tree 树链剖分
- poj 3237 Tree 树链剖分
- POJ 3237 - Tree(树链剖分)
- poj 3237 Tree 树链剖分
- POJ 3237Tree 树链剖分
- POJ 3237 Tree 树链剖分
- POJ-3237Tree(树链剖分)
- poj 3237 Tree(树链剖分)
- POJ 3237 Tree (树链剖分)
- POJ 3237 Tree(树链剖分模板)
- 黑马程序员——C语言日志——指针、地址和变量值
- Hibernate 配置文件
- 【static&extern】static、extern分析总结
- 9.21-9.27学习随笔
- python模拟登录网易邮箱
- poj 3237 Tree 树链剖分
- 文件描述符与文件描述符标志
- 黑马程序员—Java编程基础+常量与变量+类型的转换+进制的转换+运算符+scanner的使用+程序流程结构控制+循环结构
- c# panel背景图片切换闪烁的解决办法
- iOS界面编程-UIPickerView
- HDU 2612 Find a way(两次bfs)
- 关于ARM启动流程的分析(NandFlash Or NorFlash)
- 《数据结构》实验一
- web.xml文件的作用