POJ3237 Tree【树链剖分】

来源:互联网 发布:mysql不能删除数据 编辑:程序博客网 时间:2024/05/16 19:28

题意:给定一棵树,有3种操作,1.修改某条边,2.给两个点间路径上的边取相反数,3.求两个点间路径上的边权的最大值


思路:树链剖分,求区间最大值,又能取反,那么线段树维护一个最大值和最小值,取反的时候,一段区间的最大值取反 赋给 最小值,最小值取反 赋给 最大值。同时,需要一个laze标记。


#include<stdio.h>#include<iostream>#include<string.h>#include<string>#include<stdlib.h>#include<math.h>#include<vector>#include<list>#include<map>#include<stack>#include<queue>#include<algorithm>#include<numeric>#include<functional>using namespace std;typedef long long ll;typedef pair<int,int> pii;#define lson(x) 2*x#define rson(x) 2*x+1const int maxn = 1e5+5;struct Edge{int to,next;}edge[maxn*2];struct ed{int x,y,cost;}e[maxn];int head[maxn],tot,cnt;int root,size[maxn],dep[maxn],fa[maxn],son[maxn];int top[maxn],id[maxn],val[maxn];/*size -> 以x为根节点子树的大小   dep -> 深度 fa   -> 父节点                  son -> 重儿子 top  -> x所在重链的顶端节点     id -> 这个点在线段树中的位置 */struct data{int l,r,max,min,laz;}node[maxn*4];void init(){memset(head,-1,sizeof head);tot = 0;cnt = 0;}void add(int x,int y){edge[tot].to = y;edge[tot].next = head[x];head[x] = tot++;}int dfs1(int x,int pre,int d){size[x] = 1;fa[x] = pre;dep[x] = d;son[x] = 0;for(int i = head[x]; i != -1; i = edge[i].next){int y = edge[i].to;if(y != pre){size[x] += dfs1(y,x,d+1);if(size[y] > size[son[x]])son[x] = y;}}return size[x];}void dfs2(int x,int pre){id[x] = ++cnt;top[x] = pre;if(son[x]) dfs2(son[x],pre); //重链 for(int i = head[x]; i != -1; i = edge[i].next){int y = edge[i].to;if(y != fa[x] && y != son[x])dfs2(y,y); //轻链}}void pushup(int cnt){node[cnt].max = max(node[lson(cnt)].max,node[rson(cnt)].max);node[cnt].min = min(node[lson(cnt)].min,node[rson(cnt)].min);}void build(int x,int y, int cnt){node[cnt].l = x;node[cnt].r = y;node[cnt].laz = 0;if(x == y){node[cnt].max = node[cnt].min = val[x];return;}int mid = (x+y) / 2;build(x,mid,lson(cnt));build(mid+1,y,rson(cnt));pushup(cnt);}void down(int cnt){if(node[cnt].laz == 0)return;node[lson(cnt)].laz ^= 1;node[rson(cnt)].laz ^= 1;int tp = -node[lson(cnt)].max;node[lson(cnt)].max = -node[lson(cnt)].min;node[lson(cnt)].min = tp;tp = -node[rson(cnt)].max;node[rson(cnt)].max = -node[rson(cnt)].min;node[rson(cnt)].min = tp;node[cnt].laz = 0;}void up(int x,int y,int cnt,int val){if(x == node[cnt].l && y == node[cnt].r){node[cnt].laz = 0;node[cnt].max = node[cnt].min = val;return;}down(cnt);int fa = 2*cnt;if(x <= node[fa].r){if(y <= node[fa].r)up(x,y,fa,val);elseup(x,node[fa].r,fa,val);}fa++;if(y >= node[fa].l){if(x >= node[fa].l)up(x,y,fa,val);elseup(node[fa].l,y,fa,val);}pushup(cnt);}void neg(int x,int y,int cnt){if(x == node[cnt].l && y == node[cnt].r){node[cnt].laz ^= 1;int tp = -node[cnt].max;node[cnt].max = -node[cnt].min;node[cnt].min = tp;return;}down(cnt);int fa = 2*cnt;if(x <= node[fa].r){if(y <= node[fa].r)neg(x,y,fa);elseneg(x,node[fa].r,fa);}fa++;if(y >= node[fa].l){if(x >= node[fa].l)neg(x,y,fa);elseneg(node[fa].l,y,fa);}pushup(cnt);}int fid(int x,int y,int cnt){down(cnt);int res = -0x3f3f3f3f;if(x == node[cnt].l && y == node[cnt].r){return node[cnt].max;}int fa = 2*cnt;if(x <= node[fa].r){if(y <= node[fa].r)res = max(res,fid(x,y,fa));elseres = max(res,fid(x,node[fa].r,fa));}fa++;if(y >= node[fa].l){if(x >= node[fa].l)res = max(res,fid(x,y,fa));elseres = max(res,fid(node[fa].l,y,fa));}return res;}int qurey(int u,int v){int ans = -0x3f3f3f3f;while(top[u] != top[v]){if(dep[top[u]] < dep[top[v]])swap(u,v);ans = max(ans,fid(id[top[u]],id[u],1));u = top[u];u = fa[u];}if(u == v) return ans;if(dep[u] > dep[v])swap(u,v);ans = max(ans,fid(id[son[u]],id[v],1));return ans;//边权,存的都是点的父边,最上面的点用son[u] }void ne(int u,int v){while(top[u] != top[v]){if(dep[top[u]] < dep[top[v]])swap(u,v);neg(id[top[u]],id[u],1);u = top[u];u = fa[u];}if(u == v) return;if(dep[u] > dep[v])swap(u,v);neg(id[son[u]],id[v],1);}int main(){int T,n;scanf("%d",&T);while(T--){scanf("%d",&n);init();for(int i = 1; i < n; i++){scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].cost);add(e[i].x,e[i].y);add(e[i].y,e[i].x);}root = (n+1) / 2;dfs1(root,root,1);dfs2(root,root);for(int i = 1; i < n; i++) //边权 {if(dep[ e[i].x ] < dep[ e[i].y ])swap(e[i].x,e[i].y);val[ id[e[i].x] ] = e[i].cost;}build(1,cnt,1);char op[15];while(scanf("%s",op)!=EOF && op[0] != 'D'){if(op[0]=='C'){int k,b;scanf("%d%d",&k,&b);up(id[e[k].x],id[e[k].x],1,b);}else if(op[0] == 'N'){int a,b;scanf("%d%d",&a,&b);ne(a,b);}else{int a,b;scanf("%d%d",&a,&b);printf("%d\n",qurey(a,b));}}}}


原创粉丝点击