POJ 3237 树链剖分+线段树

思路:树链剖分+线段树

纯自己瞎想的 想到哪儿就写哪儿,,, 所以调了一下午+一晚上…..【尴尬】

#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 22222
using namespace std;
char ch[11];
int cnt=0,rec[N],top[N],fa[N],deep[N];
int cases,n,root,size[N],son[N],jyx,jyy;
int first[N],next[N],v[N],w[N],tot;
struct Edge{int from,to,weight;}edge[N];
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);
        }
    }
}

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值