SPOJ QTREE Query on a tree 树链剖分+线段树
题目链接:http://www.spoj.com/problems/QTREE/en/
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
题意:给你一颗数,求u到v的路径中最大的边权大小;
思路:线段树单点更新区间查询;
树链剖分板子;
最后把边权往下落,去掉lca那个点的权值;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x) cout<<"bug"<<x<<endl;
const int N=2e5+,M=1e6+,inf=1e9+;
const ll INF=1e18+,mod=; ///数组大小
struct edge{
int v,next;
}edge[N<<];
int head[N<<],edg,id,n;
/// 树链剖分 int fa[N],dep[N],son[N],siz[N]; // fa父亲,dep深度,son重儿子,siz以该点为子树的节点个数
int a[N],ran[N],top[N],tid[N]; // tid表示边的标号,top通过重边可以到达最上面的点,ran表示标记tid
int u[N],v[N],w[N];
void init()
{
memset(son,-,sizeof(son));
memset(head,-,sizeof(head));
edg=;
id=;
}
void add(int u,int v)
{
edg++;
edge[edg].v=v;
edge[edg].next=head[u];
head[u]=edg;
}
void dfs1(int u,int fath,int deep)
{
fa[u]=fath;
siz[u]=;
dep[u]=deep;
for(int i=head[u];i!=-;i=edge[i].next)
{
int v=edge[i].v;
if(v==fath)continue;
dfs1(v,u,deep+);
siz[u]+=siz[v];
if(son[u]==-||siz[v]>siz[son[u]])
son[u]=v;
}
}
void dfs2(int u,int tp)
{
tid[u]=++id;
top[u]=tp;
ran[tid[u]]=u;
if(son[u]==-)return;
dfs2(son[u],tp);
for(int i=head[u];i!=-;i=edge[i].next)
{
int v=edge[i].v;
if(v==fa[u])continue;
if(v!=son[u])
dfs2(v,v);
}
} /// 线段树
int sum[N<<];
void pushup(int pos)
{
sum[pos]=max(sum[pos<<],sum[pos<<|]);
}
void build(int l,int r,int pos)
{
if(l==r)
{
sum[pos]=a[ran[l]];
return;
}
int mid=(l+r)>>;
build(l,mid,pos<<);
build(mid+,r,pos<<|);
pushup(pos);
}
void update(int p,int c,int l,int r,int pos)
{
if(l==r)
{
sum[pos]=c;
return;
}
int mid=(l+r)>>;
if(p<=mid)update(p,c,l,mid,pos<<);
if(p>mid) update(p,c,mid+,r,pos<<|);
pushup(pos);
}
int query(int L,int R,int l,int r,int pos)
{
if(L<=l&&r<=R)return sum[pos];
int mid=(l+r)>>;
int ans=;
if(L<=mid)ans=max(ans,query(L,R,l,mid,pos<<));
if(R>mid) ans=max(ans,query(L,R,mid+,r,pos<<|));
return ans;
}
int up(int l,int r)
{
int ans=;
while(top[l]!=top[r])
{
if(dep[top[l]]<dep[top[r]])swap(l,r);
ans=max(ans,query(tid[top[l]],tid[l],,n,));
l=fa[top[l]];
}
if(dep[l]==dep[r])return ans;
if(dep[l]<dep[r])swap(l,r);
//cout<<tid[r]<<" "<<tid[l]<<" "<<endl;
ans=max(ans,query(tid[son[r]],tid[l],,n,));
return ans;
}
char s[];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
init();
for(int i=;i<n;i++)
{
scanf("%d%d%d",&u[i],&v[i],&w[i]);
add(u[i],v[i]);
add(v[i],u[i]);
}
dfs1(,-,);
dfs2(,);
for(int i=;i<n;i++)
{
if(fa[u[i]]==v[i])a[u[i]]=w[i];
else a[v[i]]=w[i];
}
build(,n,);
while()
{
scanf("%s",s);
if(s[]=='D')break;
int a,b;
scanf("%d%d",&a,&b);
if(s[]=='C')
{
if(fa[u[a]]==v[a])update(tid[u[a]],b,,n,);
else update(tid[v[a]],b,,n,);
}
else
{
printf("%d\n",up(a,b));
}
}
}
return ;
}
SPOJ QTREE Query on a tree 树链剖分+线段树的更多相关文章
- Spoj Query on a tree SPOJ - QTREE(树链剖分+线段树)
You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, ...
- 【POJ3237】Tree(树链剖分+线段树)
Description You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edg ...
- Aizu 2450 Do use segment tree 树链剖分+线段树
Do use segment tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.bnuoj.com/v3/problem_show ...
- POJ3237 Tree 树链剖分 线段树
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ3237 题意概括 Description 给你由N个结点组成的树.树的节点被编号为1到N,边被编号为1 ...
- 【CF725G】Messages on a Tree 树链剖分+线段树
[CF725G]Messages on a Tree 题意:给你一棵n+1个节点的树,0号节点是树根,在编号为1到n的节点上各有一只跳蚤,0号节点是跳蚤国王.现在一些跳蚤要给跳蚤国王发信息.具体的信息 ...
- Water Tree CodeForces 343D 树链剖分+线段树
Water Tree CodeForces 343D 树链剖分+线段树 题意 给定一棵n个n-1条边的树,起初所有节点权值为0. 然后m个操作, 1 x:把x为根的子树的点的权值修改为1: 2 x:把 ...
- 【BZOJ-2325】道馆之战 树链剖分 + 线段树
2325: [ZJOI2011]道馆之战 Time Limit: 40 Sec Memory Limit: 256 MBSubmit: 1153 Solved: 421[Submit][Statu ...
- POJ3237 (树链剖分+线段树)
Problem Tree (POJ3237) 题目大意 给定一颗树,有边权. 要求支持三种操作: 操作一:更改某条边的权值. 操作二:将某条路径上的边权取反. 操作三:询问某条路径上的最大权值. 解题 ...
- BZOJ.4034 [HAOI2015]树上操作 ( 点权树链剖分 线段树 )
BZOJ.4034 [HAOI2015]树上操作 ( 点权树链剖分 线段树 ) 题意分析 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 ...
- Aragorn's Story 树链剖分+线段树 && 树链剖分+树状数组
Aragorn's Story 来源:http://www.fjutacm.com/Problem.jsp?pid=2710来源:http://acm.hdu.edu.cn/showproblem.p ...
随机推荐
- 金九银十跳槽季,程序员面试点解析之Java专场
前言 近年来Java工程师这个岗位炙手可热,市场需求大,学习Java的人也越来越多,所以IT企业与求职者的选择都比较多,那么IT企业在面试时都会提哪些问题呢.下面为大家分享 Java高级工程师面试阿里 ...
- GROUP BY 和 ORDER BY 同时使用问题
GROUP BY 和 ORDER BY一起使用时,ORDER BY要在GROUP BY的后面.
- javaweb项目中errorPage的问题
我们的请求找不到时,会跳到错误页面,tomcat提供了一个错误页面,但是不太好.分析:tomcat自带错误页面不好的原因:有一下两点: 1.不好看: 2.不能为seo做出贡献.思考:如何解决以上问题? ...
- 求N!的二进制表示中最低位1的位置。(编程之美)
要求的是N!的二进制表示中最低位1的位置.给定一个整数N,求N!二进制表示的最低位1在第几位?例如:给定N = 3,N!= 6,那么N!的二进制表示(1 010)的最低位1在第二位. 为了得到更好的解 ...
- P3366 【模板】最小生成树(boruvka/sollin)
P3366 [模板]最小生成树 boruvka/sollin 复杂度$O(mlogn)$ 简要说明一下过程 引入一个数组$link[i]$表示连通块$i$下一步可更新的最短的边的编号 1.每次枚举所有 ...
- Vue父子组件生命周期
转载自:https://blog.csdn.net/a8725585/article/details/79092505 vue父子组件钩子函数触发顺序 beforeMount后mounted前构造子组 ...
- 解决c1xx fatal error C1083 Cannot open source file
在项目开发过程中,遇到一个问题,一个工程B导入另外一个工程A的生产代码,出现这个错误,最后查阅资料发现是文件路径太深,导致文件路径字符超过了217字符. 写了一个测试Demo来验证: 一.新建Win3 ...
- 【python35.1--EasyGui界面】
一.什么是EasyGUI EasyGUI是python中一个非常简单的GUI编程模块,不同于其他的GUI生成器,它不是事件驱动的,相反,所有的GUI交互都是通过简地函数调用就可以实现(意思是:函数调用 ...
- 【python016--序列】
一.列表,元组和字符串的共同点 --都可以通过索引得到每一个元素 --默认索引值总是从0开始 --可以通过分片的方法得到一个范围内的元素的集合 --有很多共同的操作符(重复操作符,拼接操作符.成员关系 ...
- django基础 -- 6. 多表操作
一.多表的创建 from django.db import models # Create your models here. class Author(models.Model): id = mod ...