SPOJ 375 QTREE系列-Query on a tree (树链剖分)

题目地址:SPOJ 375

树链剖分第一发!

果然是个貌似很高级的数据结构,其实就是把树的边从树形结构转化成了线性结构,从而可以用线段树或树状数组之类的数据结构进行快速维护。从而将时间缩到n*log(2*n).

这题用的线段树维护的。

代码如下:

#include <iostream>
#include <string.h>
#include <math.h>
#include <queue>
#include <algorithm>
#include <stdlib.h>
#include <map>
#include <set>
#include <stdio.h>
using namespace std;
#define LL long long
#define pi acos(-1.0)
//#pragma comment(linker, "/STACK:1024000000")
const int mod=1e9+7;
const int INF=0x3f3f3f3f;
const double eqs=1e-3;
const int MAXN=10000+10;
#define root 1, n, 1
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1
int Max[MAXN<<2], head[MAXN], cnt, n;
int siz[MAXN], dep[MAXN], w[MAXN], top[MAXN], son[MAXN], fa[MAXN], tot;
struct node
{
        int u, v, w, next;
}edge[MAXN<<1];
void add(int u, int v, int w)
{
        edge[cnt].u=u;
        edge[cnt].v=v;
        edge[cnt].w=w;
        edge[cnt].next=head[u];
        head[u]=cnt++;
}
void init()
{
        memset(head,-1,sizeof(head));
        cnt=0;
        memset(dep,0,sizeof(dep));
        memset(son,0,sizeof(son));
        memset(Max,0,sizeof(Max));
        tot=0;
}
void dfs1(int u, int p)
{
        siz[u]=1;
        for(int i=head[u];i!=-1;i=edge[i].next){
                int v=edge[i].v;
                if(v==p) continue ;
                dep[v]=dep[u]+1;
                dfs1(v,u);
                fa[v]=u;
                if(siz[v]>siz[son[u]]) son[u]=v;
                siz[u]+=siz[v];
        }
}
void dfs2(int u, int tp)
{
        w[u]=++tot; top[u]=tp;
        if(son[u]) dfs2(son[u],top[u]);
        for(int i=head[u];i!=-1;i=edge[i].next){
                int v=edge[i].v;
                if(v!=son[u]&&v!=fa[u])
                        dfs2(v,v);
        }
}
struct Line_Tree
{
        void PushUp(int rt)
        {
                Max[rt]=max(Max[rt<<1],Max[rt<<1|1]);
        }
        void Update(int p, int x, int l, int r, int rt)
        {
                if(l==r){
                        Max[rt]=x;
                        return ;
                }
                int mid=l+r>>1;
                if(p<=mid) Update(p,x,lson);
                else Update(p,x,rson);
                PushUp(rt);
        }
        int Query(int ll, int rr, int l, int r, int rt)
        {
                if(ll<=l&&rr>=r){
                        return Max[rt];
                }
                int mid=l+r>>1, ans=0;
                if(ll<=mid) ans=max(Query(ll,rr,lson),ans);
                if(rr>mid) ans=max(ans,Query(ll,rr,rson));
                return ans;
        }
}lt;
int Query(int u, int v)
{
        int f1=top[u], f2=top[v], ans=0;
        while(f1!=f2){
                if(dep[f1]<dep[f2]){
                        swap(u,v);
                        swap(f1,f2);
                }
                ans=max(ans,lt.Query(w[f1],w[u],root));
                u=fa[f1];f1=top[u];
        }
        if(u==v) return ans;
        if(dep[u]<dep[v]) swap(u,v);
        return max(ans,lt.Query(w[son[v]],w[u],root));
}
int main()
{
        int m, q, i, u, v, T, x, c;
        char s[10];
        scanf("%d",&T);
        while(T--){
                scanf("%d",&n);
                init();
                for(i=1;i<n;i++){
                        scanf("%d%d%d",&u,&v,&c);
                        add(u,v,c);
                        add(v,u,c);
                }
                dfs1(1,-1);
                dfs2(1,1);
                for(i=0;i<cnt;i+=2){
                        u=edge[i].u;
                        v=edge[i].v;
                        if(dep[u]<dep[v]) swap(u,v);
                        lt.Update(w[u],edge[i].w,root);
                }
                while(scanf("%s",s)!=EOF&&s[0]!=‘D‘){
                        if(s[0]==‘C‘){
                                scanf("%d%d",&x,&c);
                                u=edge[x-1<<1].u;
                                v=edge[x-1<<1].v;
                                if(dep[u]<dep[v]) swap(u,v);
                                lt.Update(w[u],c,root);
                        }
                        else{
                                scanf("%d%d",&u,&v);
                                printf("%d\n",Query(u,v));
                        }
                }
        }
        return 0;
}
时间: 2024-10-12 13:09:09

SPOJ 375 QTREE系列-Query on a tree (树链剖分)的相关文章

SPOJ - QTREE 375 Query on a tree 树链剖分+线段树

操作1:修改第k条边权. 操作2:询问两点间最大边权. 树链剖分,然后线段树维护最大值 #include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> #include<set> #include<map> #include<queue> #include<vector> #inclu

SPOJ375 Query on a tree 树链剖分

SPOJ375  Query on a tree   树链剖分 no tags 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

spoj 375 QTREE - Query on a tree 树链剖分

题目链接 给一棵树, 每条边有权值, 两种操作, 一种是将一条边的权值改变, 一种是询问u到v路径上最大的边的权值. 树链剖分模板. #include <iostream> #include <vector> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <map> #include <set&g

SPOJ QTREE Query on a tree ——树链剖分 线段树

[题目分析] 垃圾vjudge又挂了. 树链剖分裸题. 垃圾spoj,交了好几次,基本没改动却过了. [代码](自带常数,是别人的2倍左右) #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define maxn 20005 int T,n,fr[maxn],h[maxn],to[maxn],ne[maxn]

spoj 375 Query on a tree (树链剖分)

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 Q

spoj Query on a tree(树链剖分模板题)

375. Query on a tree Problem code: QTREE 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

SPOJ 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 tior QUERY a b : ask fo

SPOJ 913 QTREE系列- Query on a tree II (倍增LCA)

题目地址:QTREE2 - Query on a tree II LCA学了离线与在线转RMQ方法后就去做这道题,于是想了好长时间也没想到怎么做.看了题解都是用的倍增LCA..于是又去学了下倍增法求LCA,这才发现用倍增法做简直是水题...因为求路径的第k个点可以转化成求第k个父节点,然而倍增法的原理就是根据的父节点,于是这题就很容易解决了.. 求距离很好求.关键是求路径第k个点,显然这个点要么是u的第k个父节点,要么是v的第k个父节点,于是乎,先求一次LCA,判断是u还是v的,然后用倍增法找到

SPOJ QTREE Query on a tree --树链剖分

题意:给一棵树,每次更新某条边或者查询u->v路径上的边权最大值. 解法:做过上一题,这题就没太大问题了,以终点的标号作为边的标号,因为dfs只能给点分配位置,而一棵树每条树边的终点只有一个. 询问的时候,在从u找到v的过程中顺便查询到此为止的最大值即可. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath&