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 ab 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种操作:
1.把第i条边的边权更改为v
2.查询路径u,v中最大的边权 注意:是边权,所以查询时候u,v的lca不能包括进来
因为lca所表示的边权并没有在u,v的路径中 树链剖分+线段树(单点更新,区间查询)
线段树连lazy都不用
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm> #define LL long long
#define debug printf("eeeeeeeeeeeeeeeeeeeee")
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1 using namespace std; const int maxn=1e4+;
const int inf=0x3f3f3f3f; int dep[maxn];
int son[maxn];
int siz[maxn];
int top[maxn];
int fa[maxn];
int chg[maxn];
int rev[maxn];
int cost[maxn];
int e[maxn][]; struct Edge
{
int to,next;
};
Edge edge[maxn<<];
int head[maxn];
int tot; int ma[maxn<<]; void init()
{
memset(head,-,sizeof head);
tot=;
} void addedge(int u,int v)
{
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot++;
} void solve(int ); int main()
{
int test;
scanf("%d",&test);
while(test--){
int n;
init();
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%d %d %d",&e[i][],&e[i][],&e[i][]);
addedge(e[i][],e[i][]);
addedge(e[i][],e[i][]);
}
solve(n);
}
return ;
} void dfs0(int u,int pre)
{
fa[u]=pre;
siz[u]=;
dep[u]=dep[pre]+;
for(int i=head[u];~i;i=edge[i].next){
int v=edge[i].to;
if(v==pre)
continue;
dfs0(v,u);
siz[u]+=siz[v];
if(son[u]==- || siz[v]>siz[son[u]])
son[u]=v;
}
} void dfs1(int u,int tp)
{
top[u]=tp;
chg[u]=++tot;
rev[tot]=u;
if(son[u]==-)
return ;
dfs1(son[u],tp);
for(int i=head[u];~i;i=edge[i].next){
int v=edge[i].to;
if(v==fa[u] || v==son[u])
continue;
dfs1(v,v);
}
} void pushup(int rt)
{
ma[rt]=max(ma[rt<<],ma[rt<<|]);
} void build(int l,int r,int rt)
{
if(l==r){
ma[rt]=cost[rev[l]];
return ;
}
int m=(l+r)>>;
build(lson);
build(rson);
pushup(rt);
} void update(int p,int add,int l,int r,int rt)
{
if(l==r){
ma[rt]=add;
return ;
}
int m=(l+r)>>;
if(p<=m)
update(p,add,lson);
else
update(p,add,rson);
pushup(rt);
} int query(int L,int R,int l,int r,int rt)
{
if(L<=l && R>=r){
return ma[rt];
}
int m=(l+r)>>;
int ret=-inf;
if(L<=m)
ret=max(ret,query(L,R,lson));
if(R>m)
ret=max(ret,query(L,R,rson));
return ret;
} int query_path(int u,int v)
{
int ret=-inf;
while(top[u]!=top[v]){
if(dep[top[u]]<dep[top[v]])
swap(u,v);
ret=max(ret,query(chg[top[u]],chg[u],,tot,));
u=fa[top[u]];
}
if(dep[u]<dep[v])
swap(u,v);
ret=max(ret,query(chg[v]+,chg[u],,tot,));
return ret;
} void solve(int n)
{
memset(dep,,sizeof dep);
memset(son,-,sizeof son);
memset(cost,,sizeof cost);
dfs0(,);
tot=;
dfs1(,);
for(int i=;i<n;i++){
if(dep[e[i][]]>dep[e[i][]])
swap(e[i][],e[i][]);
cost[e[i][]]=e[i][];
}
build(,tot,);
char str[];
while(scanf("%s",str)){
if(str[]=='D')
break;
int u,v;
scanf("%d %d",&u,&v);
if(str[]=='Q'){
printf("%d\n",query_path(u,v));
}
else{
update(chg[e[u][]],v,,tot,);
}
}
return ;
}

SPOJ Query on a tree 树链剖分 水题的更多相关文章

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

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

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

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

  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 375 Query on a tree (树链剖分)

    Query on a tree You are given a tree (an acyclic undirected connected graph) with N nodes, and edges ...

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

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

  6. Query on a tree——树链剖分整理

    树链剖分整理 树链剖分就是把树拆成一系列链,然后用数据结构对链进行维护. 通常的剖分方法是轻重链剖分,所谓轻重链就是对于节点u的所有子结点v,size[v]最大的v与u的边是重边,其它边是轻边,其中s ...

  7. SPOJ 375 Query on a tree 树链剖分模板

    第一次写树剖~ #include<iostream> #include<cstring> #include<cstdio> #define L(u) u<&l ...

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

    题意:给一棵树,每次更新某条边或者查询u->v路径上的边权最大值. 解法:做过上一题,这题就没太大问题了,以终点的标号作为边的标号,因为dfs只能给点分配位置,而一棵树每条树边的终点只有一个. ...

  9. Query on a tree 树链剖分 [模板]

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

随机推荐

  1. poj2912 带权并查集

    题意:多个人玩石头剪刀布,每个人提前选定了自己出哪个手势,而其中有一种特殊的人他可以随意出什么手势,问是否能够从给出的一系列石头剪刀布游戏中判断出哪个是特殊的,可以从第几局游戏中判断出来. 首先按照食 ...

  2. 安装了iis之后,打开默认网站http://localhost/要求输入用户名和密码解决办法

        开始-运行gpedit.msc回车.     计算机配置--管理模板-windows 组件-Internet Exporer-Internet控制面板-安全页-Internet区域:双击登陆选 ...

  3. Linux 下 Lua 与 LuaSQL 模块安装

    相关说明: Lua最近在Nginx的web服务器上挺火的, 它的高效让更多开发喜欢上它, 本文讲述Lua与LuaSQL的安装. 在上几篇mysql-proxy的安装中有提到和操作过. 操作系统: Li ...

  4. C#中调用C++的dll的参数为指针类型的导出函数(包括二级指针的情况)

    严格来说这篇文章算不上C++范围的,不过还是挂了点边,还是在自己的blog中记录一下吧. C++中使用指针是家常便饭了,也非常的好用,这也是我之所以喜欢C++的原因之一.但是在C#中就强调托管的概念了 ...

  5. Win7+VS2010环境下CEGUI 0.8.4编译过程详解

    转载▼   1. 在http://cegui.org.uk/download 下载CEGUI源码包 cegui-0.8.4 以及CEGUI依赖库(Windows / Apple OS X only) ...

  6. Apache模块 mod_proxy 转自http://www.php100.com/manual/apache2/mod/mod_proxy.html

    Apache模块 mod_proxy 说明 提供HTTP/1.1的代理/网关功能支持 状态 扩展(E) 模块名 proxy_module 源文件 mod_proxy.c 概述 警告 在您没有对服务器采 ...

  7. javascript语法体系

    5 代表此属性ECMAScript 5(一般IE9+)才支持. 图解:-- 图解:      undefined,null,NaN Infinity ECMAScript 5禁止修改     每个函数 ...

  8. 【转】Asp.net中时间格式化的6种方法详细总结

    1. 数据控件绑定时格式化日期方法: 代码如下: <asp:BoundColumn DataField="AddTime" HeaderText="添加时间&quo ...

  9. Externalizable接口 序列化

    Java默认的序列化机制非常简单,而且序列化后的对象不需要再次调用构造器重新生成,但是在实际中,我们可以会希望对象的某一部分不需要被序列化,或者说一个对象被还原之后, 其内部的某些子对象需要重新创建, ...

  10. 邮件发送工具类 SendMail.java

    package com.util; import org.apache.commons.mail.EmailException; import org.apache.commons.mail.Simp ...