POJ 3237 树链剖分 题解

Tree
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 9443 Accepted: 2480

Description

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 v Change the weight of the ith edge to v
NEGATE a b Negate the weight of every edge on the path from a to b
QUERY a b Find 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 a, b 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

Source

POJ Monthly–2007.06.03, Lei, Tao

【解题报告】
敲了一百多行学校的老年机蓝屏了,生无可恋的找了个程序
等我再理解一下。。。

代码如下:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define N 100010

struct Edge
{
    int to,next;
}edge[N*2];
int head[N],tot;
int top[N];//top[v]表示v所在的重链的顶端节点
int fa[N]; //父亲节点
int deep[N];//深度
int num[N];//num[v]表示以v为根的子树的节点数
int p[N];//p[v]表示v与其父亲节点的连边在线段树中的位置
int fp[N];//和p数组相反
int son[N];//重儿子
int pos;

void addedge(int u,int v)
{
    edge[tot].to=v;
    edge[tot].next=head[u];
    head[u]=tot++;
}
void dfs1(int u,int pre,int d) //第一遍dfs求出fa,deep,num,son
{
    deep[u]=d;
    fa[u]=pre;
    num[u]=1;
    for(int i=head[u];i!=-1;i=edge[i].next)
    { 
        int v=edge[i].to;
        if(v!=pre)
        {
            dfs1(v,u,d+1);
            num[u]+=num[v];
            if(son[u]==-1||num[v]>num[son[u]])
            son[u]=v;
        }
    }
}
void dfs2(int u,int sp) //第二遍dfs求出top和p
{
    top[u]=sp;
    p[u]=pos++;
    fp[p[u]]=u;
    if(son[u]==-1) return;
    dfs2(son[u],sp);
    for(int i=head[u] ; i != -1; i = edge[i].next)
    {
        int v=edge[i].to;
        if(v!=son[u] && v != fa[u])
        dfs2(v,v);
    }
}

struct Node
{
    int l,r;
    int Max;
    int Min;
    int ne;
}segTree[N*3];
void build(int i,int l,int r)
{
    segTree[i].l=l;
    segTree[i].r=r;
    segTree[i].Max=0;
    segTree[i].Min=0;
    segTree[i].ne=0;
    if(l==r)return;
    int mid=(l+r)>>1;
    build(i<<1,l,mid);
    build((i<<1)|1,mid+1,r);
}
void push_up(int i)
{
    segTree[i].Max=max(segTree[i<<1].Max,segTree[(i<<1)|1].Max);
    segTree[i].Min=min(segTree[i<<1].Min,segTree[(i<<1)|1].Min);
}
void push_down(int i)
{
    if(segTree[i].l==segTree[i].r) return;
    if(segTree[i].ne)
    {
        segTree[i<<1].Max=-segTree[i<<1].Max;
        segTree[i<<1].Min=-segTree[i<<1].Min;
        swap(segTree[i<<1].Min,segTree[i<<1].Max);
        segTree[(i<<1)|1].Max=-segTree[(i<<1)|1].Max;
        segTree[(i<<1)|1].Min=-segTree[(i<<1)|1].Min;
        swap(segTree[(i<<1)|1].Max,segTree[(i<<1)|1].Min);
        segTree[i<<1].ne^=1;
        segTree[(i<<1)|1].ne^=1;
        segTree[i].ne=0;
    }
}
void update(int i,int k,int val) // 更新线段树的第k个值为val
{
    if(segTree[i].l==k&&segTree[i].r==k)
    {
        segTree[i].Max=val;
        segTree[i].Min=val;
        segTree[i].ne=0;
        return;
    }
    push_down(i);
    int mid=(segTree[i].l + segTree[i].r)/2;
    if(k<=mid)update(i<<1,k,val);
    else update((i<<1)|1,k,val);
    push_up(i);
}
void ne_update(int i,int l,int r) // 更新线段树的区间[l,r]取反
{
    if(segTree[i].l==l&&segTree[i].r==r)
    {
        segTree[i].Max=-segTree[i].Max;
        segTree[i].Min=-segTree[i].Min;
        swap(segTree[i].Max,segTree[i].Min);
        segTree[i].ne^=1;
        return;
    }
    push_down(i);
    int mid=(segTree[i].l + segTree[i].r)/2;
    if(r<=mid)ne_update(i<<1,l,r);
    else if(l>mid) ne_update((i<<1)|1,l,r);
    else
    {
        ne_update(i<<1,l,mid);
        ne_update((i<<1)|1,mid+1,r);
    }
    push_up(i);
}
int query(int i,int l,int r)  //查询线段树中[l,r] 的最大值
{
    if(segTree[i].l==l&&segTree[i].r==r)
    return segTree[i].Max;
    push_down(i);
    int mid=(segTree[i].l + segTree[i].r)/2;
    if(r<=mid) return query(i<<1,l,r);
    else if(l>mid) return query((i<<1)|1,l,r);
    else return max(query(i<<1,l,mid),query((i<<1)|1,mid+1,r));
    push_up(i);
}
int findmax(int u,int v)//查询u->v边的最大值
{
    int f1=top[u],f2=top[v];
    int tmp=-100000000;
    while(f1!=f2)
    {
        if(deep[f1]<deep[f2])
        {
            swap(f1,f2);
            swap(u,v);
        }
        tmp=max(tmp,query(1,p[f1],p[u]));
        u=fa[f1]; f1 = top[u];
    }
    if(u==v)return tmp;
    if(deep[u]>deep[v]) swap(u,v);
    return max(tmp,query(1,p[son[u]],p[v]));
}
void Negate(int u,int v)//把u-v路径上的边的值都设置为val
{
    int f1=top[u],f2=top[v];
    while(f1!=f2)
    {
        if(deep[f1]<deep[f2])
        {
            swap(f1,f2);
            swap(u,v);
        }
        ne_update(1,p[f1],p[u]);
        u=fa[f1];f1=top[u];
    }
    if(u == v)return;
    if(deep[u]>deep[v]) swap(u,v);
    return ne_update(1,p[son[u]],p[v]);
}
int e[N][3];
int main()
{
    int T;
    int n;
    scanf("%d",&T);
    while(T--)
    {
        tot=0;
        memset(head,-1,sizeof(head));
        pos=0;
        memset(son,-1,sizeof(son));
        scanf("%d",&n);
        for(int i=0;i<n-1;i++)
        {
            scanf("%d%d%d",&e[i][0],&e[i][1],&e[i][2]);
            addedge(e[i][0],e[i][1]);
            addedge(e[i][1],e[i][0]);
        }
        dfs1(1,0,0);
        dfs2(1,1);
        build(1,0,pos-1);
        for(int i=0;i<n-1;i++)
        {
            if(deep[e[i][0]] > deep[e[i][1]])
                swap(e[i][0],e[i][1]);
            update(1,p[e[i][1]],e[i][2]);
        }
        char op[10];
        int u,v;
        while(scanf("%s",op)==1)
        {
            if(op[0] == 'D')break;
            scanf("%d%d",&u,&v);
            if(op[0] == 'Q')
            printf("%d\n",findmax(u,v));//查询u->v路径上边权的最大值
            else if(op[0] == 'C')
            update(1,p[e[u-1][1]],v);//改变第u条边的值为v
            else Negate(u,v);
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值