POJ 3237 树链剖分+线段树
2016-08-31 23:27
239 查看
思路:树链剖分+线段树
纯自己瞎想的 想到哪儿就写哪儿,,, 所以调了一下午+一晚上…..【尴尬】
纯自己瞎想的 想到哪儿就写哪儿,,, 所以调了一下午+一晚上…..【尴尬】
#include <cstdio> #include <cstring> #include <algorithm> #define N 22222 using namespace std; char ch[11]; int cnt=0,rec ,top ,fa ,deep ; int cases,n,root,size ,son ,jyx,jyy; int first ,next ,v ,w ,tot; struct Edge{int from,to,weight;}edge ; struct Tree{int maxx,minn,mark;void init(){maxx=minn=0;mark=0;}}tree[N*4]; void add(int x,int y,int z){w[tot]=z,v[tot]=y,next[tot]=first[x],first[x]=tot++;} void dfs(int x){ size[x]=1;son[x]=0; for(int i=first[x];~i;i=next[i]) if(v[i]!=fa[x]){ fa[v[i]]=x,deep[v[i]]=deep[x]+1; dfs(v[i]); size[x]+=size[v[i]]; if(size[v[i]]>size[son[x]])son[x]=v[i]; } } void build(int x,int tp){ top[x]=tp;rec[x]=++cnt; if(son[x])build(son[x],tp); for(int i=first[x];~i;i=next[i]) if(v[i]!=fa[x]&&v[i]!=son[x]) build(v[i],v[i]); } void push_down(int lson,int rson){ swap(tree[lson].maxx,tree[lson].minn); tree[lson].maxx*=-1,tree[lson].minn*=-1; swap(tree[rson].maxx,tree[rson].minn); tree[rson].maxx*=-1,tree[rson].minn*=-1; tree[lson].mark^=1;tree[rson].mark^=1; } void push_up(int pos){ tree[pos].maxx=max(tree[pos<<1].maxx,tree[pos<<1|1].maxx); tree[pos].minn=min(tree[pos<<1].minn,tree[pos<<1|1].minn); } void insert(int l,int r,int pos,int location,int weight){ if(l==r){tree[pos].maxx=tree[pos].minn=weight;tree[pos].mark=0;return;} int mid=(l+r)>>1,lson=pos<<1,rson=pos<<1|1; if(tree[pos].mark)tree[pos].mark=0,push_down(lson,rson); if(mid>=location)insert(l,mid,lson,location,weight); else insert(mid+1,r,rson,location,weight); push_up(pos); } void update(int l,int r,int pos,int Left,int Right){ if(l>=Left&&r<=Right){ swap(tree[pos].maxx,tree[pos].minn); tree[pos].maxx*=-1;tree[pos].minn*=-1; tree[pos].mark^=1; return; } int mid=(l+r)>>1,lson=pos<<1,rson=pos<<1|1; if(tree[pos].mark)tree[pos].mark=0,push_down(lson,rson); if(mid<Left)update(mid+1,r,rson,Left,Right); else if(mid>=Right)update(l,mid,lson,Left,Right); else update(l,mid,lson,Left,Right),update(mid+1,r,rson,Left,Right); push_up(pos); } int query(int l,int r,int pos,int Left,int Right){ if(l>=Left&&r<=Right)return tree[pos].maxx; int mid=(l+r)>>1,lson=pos<<1,rson=pos<<1|1; if(tree[pos].mark)tree[pos].mark=0,push_down(lson,rson); if(mid>=Right)return query(l,mid,lson,Left,Right); else if(mid<Left)return query(mid+1,r,rson,Left,Right); else return max(query(l,mid,lson,Left,Right),query(mid+1,r,rson,Left,Right)); } int find(int x,int y){ int fx=top[x],fy=top[y],temp=-0x3fffffff; while(fx!=fy){ if(deep[fx]<deep[fy])swap(fx,fy),swap(x,y); temp=max(temp,query(1,cnt,1,rec[fx],rec[x])); x=fa[fx],fx=top[x]; } if(x==y)return temp; if(deep[x]>deep[y])swap(x,y); return max(temp,query(1,cnt,1,rec[son[x]],rec[y])); } void negate(int x,int y){ int fx=top[x],fy=top[y]; while(fx!=fy){ if(deep[fx]<deep[fy])swap(fx,fy),swap(x,y); update(1,cnt,1,rec[fx],rec[x]); x=fa[fx],fx=top[x]; } if(x==y)return; if(deep[x]>deep[y])swap(x,y); update(1,cnt,1,rec[son[x]],rec[y]); } int main(){ scanf("%d",&cases); while(cases--){ memset(first,-1,sizeof(first)); memset(size,0,sizeof(size)); memset(fa,0,sizeof(fa)); scanf("%d",&n); tot=cnt=deep[root]=fa[root]=0; for(int i=1;i<n;i++){ scanf("%d%d%d",&edge[i].from,&edge[i].to,&edge[i].weight); add(edge[i].from,edge[i].to,edge[i].weight); add(edge[i].to,edge[i].from,edge[i].weight); } root=(n+1)>>1; dfs(root);build(root,root); for(int i=1;i<n;i++){ if(deep[edge[i].from]>deep[edge[i].to])swap(edge[i].from,edge[i].to); insert(1,cnt,1,rec[edge[i].to],edge[i].weight); } while(scanf("%s",ch)&&ch[0]!='D'){ scanf("%d%d",&jyx,&jyy); if(ch[0]=='Q')printf("%d\n",find(jyx,jyy)); else if(ch[0]=='C')insert(1,cnt,1,rec[edge[jyx].to],jyy); else negate(jyx,jyy); } } }
相关文章推荐
- poj 3237 Tree(树链剖分,点权,线段树)
- POJ 3237 Tree (树链剖分 路径剖分 线段树的lazy标记)
- POJ 3237 Tree 树链剖分 线段树
- POJ 3237 Tree(树链剖分 线段树区间标记)
- poj 3237 Tree 树链剖分 线段树
- poj 3237 Tree(树链剖分,线段树)
- POJ 3237 Tree(树链剖分-线段树点更新-区间更新-区间最值查询-入边)
- |poj 3237|树链剖分|线段树|Tree
- POJ 3237 树链剖分+线段树
- POJ 3237 Tree 树链剖分
- POJ 3237 - Tree(树链剖分)
- 【SPOJ 375】 【POJ 3237】 375. Query on a tree 树链剖分
- POJ 3237 Tree(树链剖分模板)
- POJ 3237 Tree 树链剖分
- POJ 3237 Tree (树链剖分)
- POJ 3237 Tree 树链剖分
- |poj 2763|LCA, 树状数组|或者|树链剖分, 线段树|Housewife Wind
- 树链剖分理解&&poj 3237
- POJ 3237 Tree (树链剖分 路径更新)
- POJ 2763 Housewife Wind(树链剖分+线段树点更新-区间查询+入边)