树剖模板,注意把边化为点后要查到y的儿子。

 #include<bits/stdc++.h>
using namespace std;
const int N=;
int f[N],bel[N],w[N],id[N],pos[N],d[N],size[N],son[N],head[N];
int n,idx,cnt,num,T,rt;
struct node
{
int l,r,s,mx;
void clean()
{
l=r=s=mx=-1e9;
}
}t[N<<];
struct edge
{
int to,nex,w,x;
}e[N<<],a[N];
void add(int x,int y,int w)
{
e[++cnt].to=y;e[cnt].nex=head[x];head[x]=cnt;e[cnt].x=x;
}
void dfs1(int x,int fa)
{
size[x]=;
for(int i=head[x];i;i=e[i].nex)
{
int y=e[i].to;
if(y==fa)continue;
f[y]=x;d[y]=d[x]+;
dfs1(y,x);
if(size[y]>size[son[x]])son[x]=y;
size[x]+=size[y];
}
return;
}
void dfs2(int x,int chain)
{
bel[x]=chain;pos[x]=++idx;id[idx]=x;
if(son[x])dfs2(son[x],chain);
for(int i=head[x];i;i=e[i].nex)
{
int y=e[i].to;
if(y==f[x]||y==son[x])continue;
dfs2(y,y);
}
}
void build(int &x,int l,int r)
{
x=++num;
if(l==r){
t[x].s=t[x].mx=w[l];return;
}
int mid=l+r>>;
build(t[x].l,l,mid);build(t[x].r,mid+,r);
t[x].mx=max(t[t[x].l].mx,t[t[x].r].mx);
return;
}
int query(int x,int l,int r,int L,int R)
{
if(l==L&&r==R)return t[x].mx;
int mid=l+r>>;
if(R<=mid)return query(t[x].l,l,mid,L,R);
else if(L>mid)return query(t[x].r,mid+,r,L,R);
else return max(query(t[x].l,l,mid,L,mid),query(t[x].r,mid+,r,mid+,R));
}
void change(int x,int l,int r,int pos,int ww)
{
if(l==r){t[x].s=t[x].mx=ww;return;}
int mid=l+r>>;
if(pos<=mid)change(t[x].l,l,mid,pos,ww);
else change(t[x].r,mid+,r,pos,ww);
t[x].mx=max(t[t[x].l].mx,t[t[x].r].mx);
}
int querytree(int x,int y)
{
int ans=-1e9;
while(bel[x]!=bel[y])
{
if(d[bel[x]]<d[bel[y]])swap(x,y);
ans=max(ans,query(rt,,n,pos[bel[x]],pos[x]));
x=f[bel[x]];
}
if(d[x]<d[y])swap(x,y);
if(d[son[y]]<=d[x])
ans=max(ans,query(rt,,n,pos[son[y]],pos[x]));
return ans;
}
void init()
{
memset(f,,sizeof(f));
memset(d,,sizeof(d));
memset(size,,sizeof(size));
memset(son,,sizeof(son));
memset(bel,,sizeof(bel));
memset(pos,,sizeof(pos));
memset(head,,sizeof(head));
memset(id,,sizeof(id));
memset(w,,sizeof(w));cnt=idx=num=;rt=;
for(int i=;i<=num;++i)t[i].clean();
}
int main()
{
scanf("%d",&T);
while(T--)
{
init();int x,y,ww;
scanf("%d",&n);
for(int i=;i<n;++i)
{
scanf("%d%d%d",&x,&y,&ww);
add(x,y,ww);add(y,x,ww);
a[i].x=x;a[i].to=y;a[i].w=ww;
}
dfs1(,);dfs2(,);
build(rt,,n);char s[];
for(int i=;i<n;i++)
{
x=d[a[i].x]>d[a[i].to]?a[i].x:a[i].to;
change(rt,,n,pos[x],a[i].w);
}
change(rt,,n,pos[],-1e9);
while()
{
scanf("%s",s);
if(s[]=='D')break;
if(s[]=='Q')
{
scanf("%d%d",&x,&y);
printf("%d\n",querytree(x,y));
}
else
{
scanf("%d%d",&x,&y);
x=d[a[x].x]>d[a[x].to]?a[x].x:a[x].to;
change(rt,,n,pos[x],y);
}
}
}
return ;
}

QTREEⅠ SPOJ的更多相关文章

  1. QTREE 树链剖分---模板 spoj QTREE

    <树链剖分及其应用> 一文讲得非常清楚,我一早上就把他学会了并且A了这题的入门题. spoj QTREE 题目: 给出一棵树,有两种操作: 1.修改一条边的边权. 2.询问节点a到b的最大 ...

  2. SPOJ QTREE 系列解题报告

    题目一 : SPOJ 375 Query On a Tree http://www.spoj.com/problems/QTREE/ 给一个树,求a,b路径上最大边权,或者修改a,b边权为t. #in ...

  3. SPOJ QTREE Query on a tree 树链剖分+线段树

    题目链接:http://www.spoj.com/problems/QTREE/en/ QTREE - Query on a tree #tree You are given a tree (an a ...

  4. spoj QTREE - Query on a tree(树链剖分+线段树单点更新,区间查询)

    传送门:Problem QTREE https://www.cnblogs.com/violet-acmer/p/9711441.html 题解: 树链剖分的模板题,看代码比看文字解析理解来的快~~~ ...

  5. SPOJ QTREE

    QTREE /* 题目大意:维护一棵树,允许修改边权以及查询链上最大值 题解:我们将边权转为点权,标记在深度较深的点上,树链剖分后用线段树处理即可 */ #include <cstdio> ...

  6. SPOJ 375 Query on a tree(树链剖分)(QTREE)

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

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

    [题目分析] 垃圾vjudge又挂了. 树链剖分裸题. 垃圾spoj,交了好几次,基本没改动却过了. [代码](自带常数,是别人的2倍左右) #include <cstdio> #incl ...

  8. 【学术篇】SPOJ QTREE 树链剖分

    发现链剖这东西好久不写想一遍写对是有难度的.. 果然是熟能生巧吧.. WC的dalao们都回来了 然后就用WC的毒瘤题荼毒了我们一波, 本来想打个T1 44分暴力 然后好像是特判写挂了还是怎么的就只能 ...

  9. SPOJ QTree【树链剖分】

    一 题目 QTREE 二 分析 第一道树链剖分的题,写的好艰难啊. 题意还是比较好理解的,就是在树上操作. 对于修改,题中要求的是单点修改,就算是直接树上操作也是非常简单的. 对于查询,查询的时候,是 ...

随机推荐

  1. WPF:ComboBox使用XmlDataProvider做级联

    程序功能: 使用ComboBox做级联,数据源为XML文件,适合小数据量呈现 程序代码: <Window x:Class="WpfApplication1.LayouTest" ...

  2. python学习笔记(十五)之集合

    集合:对应数学中的集合类型.集合中的元素是唯一,且无序的. 创建集合 方法一:使用{},注意python会自动删除重复元素 >>> number = {1,2,3,4,3,2,1} ...

  3. 服务器端包含 SSI简介

    服务器端包含 SSI,是英文 Server Side Includes的简写.SSI是一种可以指挥服务器动态声称网页内容的HTML指令. 通常SSI可以用来确保网页中的一些通用内容,比如版权信息.联系 ...

  4. hdu 5373 The shortest problem(杭电多校赛第七场)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5373 The shortest problem Time Limit: 3000/1500 MS (J ...

  5. 【swupdate文档 四】SWUpdate:使用默认解析器的语法和标记

    SWUpdate:使用默认解析器的语法和标记 介绍 SWUpdate使用库"libconfig"作为镜像描述的默认解析器. 但是,可以扩展SWUpdate并添加一个自己的解析器, ...

  6. HttpURLConnection传json

    private static String sendToWangTing(DataRow dataRow) throws IOException{ String ip = Configuration. ...

  7. 使用正则表达式匹配IP地址

    IP地址分为4段,以点号分隔.要对IP地址进行匹配,首先要对其进行分析,分成如下部分,分别进行匹配:   第一步:地址分析,正则初判 1.0-9 \d 进行匹配 2.10-99 [1-9]\d 进行匹 ...

  8. oracle命令生成AWR报告

    --命令生成AWR报告oracle@linux:~> sqlplus / as sysdba SQL*Plus: Release 11.1.0.7.0 - Production on Fri A ...

  9. Percona XtraDB Cluster(PXC)-高可用架构设计说明

    Mycat+PXC高可用集群 一.架构图 架构说明: 1.mysql 集群高可用部分: l 针对业务场景选用Percona XtraDB Cluter(PXC)复制集群.两个片集群 PXC-dataN ...

  10. CentOS_Linux服务器系统安装之分区

    在software selection中选择Server with GUI>(Compatibility Libraries.Development Tools和Security Tools) ...