spoj 375 树链剖分 模板

QTREE - Query on a tree

 

You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, 3...N-1.

We will ask you to perfrom some instructions of the following form:

  • CHANGE i ti : change the cost of the i-th edge to ti
    or
  • QUERY a b : ask for the maximum edge cost on the path from node a to node b

Input

The first line of input contains an integer t, the number of test cases (t <= 20). t test cases follow.

For each test case:

  • In the first line there is an integer N (N <= 10000),
  • In the next N-1 lines, the i-th line describes the i-th edge: a line with three integers a b c denotes an edge between a, b of cost c (c <= 1000000),
  • The next lines contain instructions "CHANGE i ti" or "QUERY a b",
  • The end of each test case is signified by the string "DONE".

There is one blank line between successive tests.

Output

For each "QUERY" operation, write one integer representing its result.

Example

Input:
1

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

Output:
1
3

 题意:

一棵树有修改边权值操作和询问两个节点之间的最大边权值操作

代码:

代码:

//每个点和他父节点的边构成一个线段树上的点。所以线段树的点实际从2开始
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN=100000;
int id[MAXN+9],fa[MAXN+9],max_val[MAXN*4+9],head[MAXN+9],son[MAXN+9],top[MAXN+9],lev[MAXN+9],size[MAXN+9];
//id:对应到线段树上的点编号,son:重儿子,top:重链的头,lev:深度,size:子树大小
int tot,cnt,val[MAXN+9]; //cnt:线段树节点数
struct Edge
{
    int u,v,w,next;
}edge[MAXN*2+9];
void init()
{
    for(int i=0;i<=MAXN;i++) fa[i]=top[i]=i;
    memset(size,0,sizeof(size));
    memset(head,-1,sizeof(head));
    memset(val,0,sizeof(val));
    tot=cnt=0;
}
void add(int x,int y,int z)
{
    edge[tot].u=x;edge[tot].v=y;edge[tot].w=z;
    edge[tot].next=head[x];
    head[x]=tot++;
    edge[tot].u=y;edge[tot].v=x;edge[tot].w=z;
    edge[tot].next=head[y];
    head[y]=tot++;
}
void dfs1(int x,int d)
{
    lev[x]=d;
    son[x]=0;
    size[x]=1;
    for(int i=head[x];i!=-1;i=edge[i].next){
        int y=edge[i].v;
        if(y==fa[x]) continue;
        fa[y]=x;
        dfs1(y,d+1);
        size[x]+=size[y];
        if(size[son[x]]<size[y]) son[x]=y;
    }
}
void dfs2(int x,int tp)
{
    top[x]=tp;
    id[x]=++cnt;
    if(son[x]) dfs2(son[x],tp);
    for(int i=head[x];i!=-1;i=edge[i].next){
        int y=edge[i].v;
        if(y==fa[x]||y==son[x]) continue;
        dfs2(y,y);
    }
}
void pushup(int rt) { max_val[rt]=max(max_val[rt<<1],max_val[rt<<1|1]); }
void build(int l,int r,int rt)
{
    if(l==r) { max_val[rt]=val[l];return; }
    int mid=(l+r)>>1;
    build(l,mid,rt<<1);
    build(mid+1,r,rt<<1|1);
    pushup(rt);
}
void update(int id,int c,int l,int r,int rt)
{
    if(l==r){
        max_val[rt]=c;
        return;
    }
    int mid=(l+r)>>1;
    if(id<=mid) update(id,c,l,mid,rt<<1);
    else update(id,c,mid+1,r,rt<<1|1);
    pushup(rt);
}
int query(int ql,int qr,int l,int r,int rt)
{
    if(ql<=l&&qr>=r) return max_val[rt];
    int mid=(l+r)>>1,ans=0;
    if(ql<=mid) ans=max(ans,query(ql,qr,l,mid,rt<<1));
    if(qr>mid) ans=max(ans,query(ql,qr,mid+1,r,rt<<1|1));
    return ans;
}
int solve(int l,int r)
{
    int ltp=top[l],rtp=top[r],ans=0;
    while(ltp!=rtp){
        if(lev[rtp]<lev[ltp]){
            swap(ltp,rtp);
            swap(l,r);
        }
        ans=max(ans,query(id[rtp],id[r],1,cnt,1));
        r=fa[rtp];
        rtp=top[r];
    }
    if(lev[r]>lev[l]) swap(r,l);
    if(l!=r) ans=max(ans,query(id[son[r]],id[l],1,cnt,1));
    return ans;
}
int main()
{
    //freopen("in.txt","r",stdin);
    int t,n;
    scanf("%d",&t);
    while(t--){
        init();
        scanf("%d",&n);
        for(int i=1;i<n;i++){
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            add(x,y,z);
        }
        dfs1(1,1);
        dfs2(1,1);
        for(int i=0;i<tot;i+=2){
            if(lev[edge[i].u]>lev[edge[i].v]) swap(edge[i].u,edge[i].v);
            val[id[edge[i].v]]=edge[i].w;
        }
        build(1,cnt,1);
        char ch[20];
        while(scanf("%s",ch)&&ch[0]!='D'){
            int x,y;
            scanf("%d%d",&x,&y);
            if(ch[0]=='C') update(id[edge[x*2-2].v],y,1,cnt,1);
            else printf("%d\n",solve(x,y));
        }
    }
    return 0;
}

 

 

转载于:https://www.cnblogs.com/--ZHIYUAN/p/7689101.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本方法。编译原理不仅是计算机科学理论的重要组成部分,也是实现高效、可靠的计算机程序设计的关键。本文将对编译原理的基本概念、发展历程、主要内容和实际应用进行详细介绍编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本方法。编译原理不仅是计算机科学理论的重要组成部分,也是实现高效、可靠的计算机程序设计的关键。本文将对编译原理的基本概念、发展历程、主要内容和实际应用进行详细介绍编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本方法。编译原理不仅是计算机科学理论的重要组成部分,也是实现高效、可靠的计算机程序设计的关键。本文将对编译原理的基本概念、发展历程、主要内容和实际应用进行详细介绍编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本方法。编译原理不仅是计算机科学理论的重要组成部分,也是实现高效、可靠的计算机程序设计的关键。本文将对编译原理的基本概念、发展历程、主要内容和实际应用进行详细介绍编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值