POJ - 3237 Tree (树链剖分+线段树单点更新,区间更新,区间最值)

Tree

You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edges are numbered 1 through N − 1. Each edge is associated with a weight. Then you are to execute a series of instructions on the tree. The instructions can be one of the following forms:

CHANGE i vChange the weight of the ith edge to v 
NEGATE a bNegate the weight of every edge on the path from a to b 
QUERY a bFind the maximum weight of edges on the path from a to b 

Input

The input contains multiple test cases. The first line of input contains an integer t (t ≤ 20), the number of test cases. Then follow the test cases.

Each test case is preceded by an empty line. The first nonempty line of its contains N (N ≤ 10,000). The next N − 1 lines each contains three integers ab and c, describing an edge connecting nodes a and b with weight c. The edges are numbered in the order they appear in the input. Below them are the instructions, each sticking to the specification above. A lines with the word “DONE” ends the test case.

Output

For each “QUERY” instruction, output the result on a separate line.

Sample Input

1

3
1 2 1
2 3 2
QUERY 1 2
CHANGE 1 3
QUERY 1 2
DONE

Sample Output

1
3

题目链接:http://poj.org/problem?id=3237

题目大意:这道题是Query on a tree的升级版,其余的都那道题一样,就是在其基础上加了一个操作:NEGATE u v

把u到v的最短路径上的所有边权都取相反数

思路:维护最大值和最小值,当取相反数时,最大值就变成了最小值,最小值就变成了最大值

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define inf 0x3f3f3f
const int N=10005;
int first[N],n,w[N],tot,cnt,maxx[N<<2],minn[N<<2],lazy[N<<2];
int d[N],fa[N],siz[N],son[N],top[N],id[N],rk[N];
struct node
{
    int u,v,w,nex;
}e[N<<1];
void adde(int u,int v,int w)
{
    e[tot].u=u,e[tot].v=v;
    e[tot].w=w;
    e[tot].nex=first[u];
    first[u]=tot++;
}
void init()
{
    memset(first,-1,sizeof(first));
    memset(son,0,sizeof(son));
    tot=cnt=0;
}
void dfs1(int u,int pre,int depth) //处理 d,fa,siz,son 数组
{
    d[u]=depth;
    fa[u]=pre;
    siz[u]=1;
    for(int i=first[u];~i;i=e[i].nex)
    {
        int v=e[i].v;
        if(v==pre) continue;
        dfs1(v,u,depth+1);
        siz[u]+=siz[v];
        if(siz[v]>siz[son[u]])
            son[u]=v;
    }
}
void dfs2(int u,int t) //处理 top id rk wt 数组
{
    top[u]=t;
    id[u]=++cnt;
    rk[cnt]=u;
    if(!son[u]) return;
    dfs2(son[u],t);
    for(int i=first[u];~i;i=e[i].nex)
    {
        int v=e[i].v;
        if(v!=son[u]&&v!=fa[u])
            dfs2(v,v);
    }
}
void pushup(int rt)
{
    maxx[rt]=max(maxx[rt<<1],maxx[rt<<1|1]);
    minn[rt]=min(minn[rt<<1],minn[rt<<1|1]);
}
void build(int l,int r,int rt)
{
    lazy[rt]=0;
    if(l==r)
    {
        maxx[rt]=minn[rt]=w[l];
        return;
    }
    int mid=(l+r)>>1;
    build(lson);
    build(rson);
    pushup(rt);
}
void pushdown(int rt)
{
    if(lazy[rt])
    {
        maxx[rt<<1]*=-1;
        minn[rt<<1]*=-1;
        swap(minn[rt<<1],maxx[rt<<1]);
        maxx[rt<<1|1]*=-1;
        minn[rt<<1|1]*=-1;
        swap(minn[rt<<1|1],maxx[rt<<1|1]);
        lazy[rt<<1]^=1;
        lazy[rt<<1|1]^=1;
        lazy[rt]=0;
    }
}
void update(int k,int c,int l,int r,int rt)
{
    if(l==r)
    {
        maxx[rt]=c;
        minn[rt]=c;
        return;
    }
    pushdown(rt);
    int mid=(l+r)>>1;
    if(k<=mid) update(k,c,lson);
    else update(k,c,rson);
    pushup(rt);
}
void update1(int L,int R,int l,int r,int rt)
{
    if(L<=l&&r<=R)
    {
        maxx[rt]*=-1;
        minn[rt]*=-1;
        swap(minn[rt],maxx[rt]);
        lazy[rt]^=1;
        return;
    }
    pushdown(rt);
    int mid=(l+r)>>1;
    if(L<=mid) update1(L,R,lson);
    if(R>mid) update1(L,R,rson);
    pushup(rt);
}
int query(int L,int R,int l,int r,int rt)
{
    if(L<=l&&r<=R)
        return maxx[rt];
    pushdown(rt);
    int mid=(l+r)>>1,ans=-inf;
    if(L<=mid) ans=max(ans,query(L,R,lson));
    if(R>mid) ans=max(ans,query(L,R,rson));
    return ans;
}
void uprange(int x,int y)
{
    while(top[x]!=top[y])
    {
        if(d[top[x]]<d[top[y]]) swap(x,y);
        update1(id[top[x]],id[x],1,n,1);
        x=fa[top[x]];
    }
    if(d[x]>d[y]) swap(x,y);
    update1(id[son[x]],id[y],1,n,1);
}
int quemax(int x,int y)
{
    int ans=-inf;
    while(top[x]!=top[y])
    {
        if(d[top[x]]<d[top[y]]) swap(x,y);
        ans=max(ans,query(id[top[x]],id[x],1,n,1));
        x=fa[top[x]];
    }
    if(d[x]>d[y]) swap(x,y);
    ans=max(ans,query(id[son[x]],id[y],1,n,1));
    return ans;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int u,v,k;
        scanf("%d",&n);
        init();
        for(int i=1;i<n;i++)
        {
            scanf("%d%d%d",&u,&v,&k);
            adde(u,v,k);
            adde(v,u,k);
        }
        dfs1(1,0,1);
        dfs2(1,1);
        for(int i=0;i<tot;i+=2)
        {
            if(d[e[i].u]<d[e[i].v]) swap(e[i].u,e[i].v);
            w[id[e[i].u]] = e[i].w;
        }
        build(1,n,1);
        char s[10];
        while(~scanf("%s",&s))
        {
            if(s[0]=='D') break;
            else if(s[0]=='C')
            {
                scanf("%d%d",&u,&k);
                update(id[e[u*2-2].u],k,1,n,1);
            }
            else if(s[0]=='Q')
            {
                scanf("%d%d",&u,&v);
                int ans=quemax(u,v);
                printf("%d\n",ans);
            }
            else if(s[0]=='N')
            {
                scanf("%d%d",&u,&v);
                uprange(u,v);
            }
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值