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

/*
树链剖分+线段树.
这题呵呵了.
询问字符串竟然卡cin(长记性了).
化边为点.
建树后把边的信息存在son里.
因为son只有一个father,而father可以同时有若干个son.
树剖线段树单点修改区间查询.
这题一开始W的原因是
here:query(1,pos[x],pos[y]).
应该为query(1,pos[x]+1,pos[y]).
因为我们已经把边的信息存到son里边了.
pos[x]存的是(x,fa[x])的edge.
然而我们最后查的是[x,y]的ans.
所以从x的son开始查.
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#define MAXN 100001
using namespace std;
struct edge{int v,next;}e[MAXN*2];
struct data{int l,r,lc,rc,ans;}tree[MAXN*4];
int n,m,ans,head[MAXN],cut,size[MAXN],fa[MAXN],top[MAXN],deep[MAXN],maxsize,pos[MAXN];
struct node{int x,y,z;}s[MAXN];
int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();
return x*f;
}
void add(int u,int v,int x)
{
e[++cut].v=v;
e[cut].next=head[u];
head[u]=cut;
}
void build(int l,int r)
{
int k=++cut;
tree[k].l=l,tree[k].r=r;
if(l==r) return ;
int mid=(l+r)>>1;
tree[k].lc=cut+1;build(l,mid);
tree[k].rc=cut+1;build(mid+1,r);
return ;
}
void dfs1(int u)
{
size[u]=1;
for(int i=head[u];i;i=e[i].next)
{
int v=e[i].v;
if(!fa[v]) deep[v]=deep[u]+1,fa[v]=u,dfs1(v),size[u]+=size[v];
}
return ;
}
void dfs2(int u,int top1)
{
top[u]=top1;pos[u]=++maxsize;
int k=0;
for(int i=head[u];i;i=e[i].next)
{
int v=e[i].v;
if(fa[v]==u&&size[v]>size[k]) k=v;
}
if(!k) return ;
dfs2(k,top1);
for(int i=head[u];i;i=e[i].next)
{
int v=e[i].v;
if(fa[v]==u&&v!=k) dfs2(v,v);
}
return ;
}
void change(int k,int x,int z)
{
if(tree[k].l==tree[k].r)
{
tree[k].ans=z;return ;
}
int mid=(tree[k].l+tree[k].r)>>1;
if(x<=mid) change(tree[k].lc,x,z);
else change(tree[k].rc,x,z);
tree[k].ans=max(tree[tree[k].lc].ans,tree[tree[k].rc].ans);
return ;
}
int query(int k,int l,int r)
{
if(l<=tree[k].l&&tree[k].r<=r) return tree[k].ans;
int tot=-1e9,mid=(tree[k].l+tree[k].r)>>1;
if(l<=mid) tot=max(tot,query(tree[k].lc,l,r));
if(r>mid) tot=max(tot,query(tree[k].rc,l,r));
return tot;
}
int slovequery(int x,int y)
{
ans=-1e9;
while(top[x]!=top[y])
{
if(deep[top[x]]<deep[top[y]]) swap(x,y);
ans=max(ans,query(1,pos[top[x]],pos[x]));
x=fa[top[x]];
}
if(pos[x]>pos[y]) swap(x,y);
ans=max(ans,query(1,pos[x]+1,pos[y]));//1 W.
return ans;
}
void Clear()
{
cut=0;maxsize=0;
memset(head,0,sizeof head);
memset(size,0,sizeof size);
memset(fa,0,sizeof fa);
memset(tree,0,sizeof tree);
}
int main()
{
int x,y,z,p,q,t;
t=read();
while(t--)
{
n=read();Clear();
for(int i=1;i<=n-1;i++)
{
x=read(),y=read(),z=read();
add(x,y,z),add(y,x,z);
s[i].x=x,s[i].y=y,s[i].z=z;
}
cut=0;
build(1,n);fa[1]=1;
dfs1(1),dfs2(1,1);
for(int i=1;i<=n-1;i++)
{
x=s[i].x,y=s[i].y;
if(fa[x]==y) swap(x,y);
change(1,pos[y],s[i].z);
}
char ch[6];
while(true)
{
scanf("%s",ch);
//cin>>ch; 1T
if(ch[0]=='D') break;
x=read(),y=read();
if(ch[0]=='Q') printf("%d\n",slovequery(x,y));
else
{
p=s[x].x,q=s[x].y;
if(fa[p]==q) swap(p,q);
change(1,pos[q],y);
}
}
}
return 0;
}

Spoj Query on a tree SPOJ - QTREE(树链剖分+线段树)的更多相关文章

  1. 【POJ3237】Tree(树链剖分+线段树)

    Description You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edg ...

  2. 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 ...

  3. POJ3237 Tree 树链剖分 线段树

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ3237 题意概括 Description 给你由N个结点组成的树.树的节点被编号为1到N,边被编号为1 ...

  4. 【CF725G】Messages on a Tree 树链剖分+线段树

    [CF725G]Messages on a Tree 题意:给你一棵n+1个节点的树,0号节点是树根,在编号为1到n的节点上各有一只跳蚤,0号节点是跳蚤国王.现在一些跳蚤要给跳蚤国王发信息.具体的信息 ...

  5. Water Tree CodeForces 343D 树链剖分+线段树

    Water Tree CodeForces 343D 树链剖分+线段树 题意 给定一棵n个n-1条边的树,起初所有节点权值为0. 然后m个操作, 1 x:把x为根的子树的点的权值修改为1: 2 x:把 ...

  6. 【BZOJ-2325】道馆之战 树链剖分 + 线段树

    2325: [ZJOI2011]道馆之战 Time Limit: 40 Sec  Memory Limit: 256 MBSubmit: 1153  Solved: 421[Submit][Statu ...

  7. POJ3237 (树链剖分+线段树)

    Problem Tree (POJ3237) 题目大意 给定一颗树,有边权. 要求支持三种操作: 操作一:更改某条边的权值. 操作二:将某条路径上的边权取反. 操作三:询问某条路径上的最大权值. 解题 ...

  8. BZOJ.4034 [HAOI2015]树上操作 ( 点权树链剖分 线段树 )

    BZOJ.4034 [HAOI2015]树上操作 ( 点权树链剖分 线段树 ) 题意分析 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 ...

  9. Aragorn's Story 树链剖分+线段树 && 树链剖分+树状数组

    Aragorn's Story 来源:http://www.fjutacm.com/Problem.jsp?pid=2710来源:http://acm.hdu.edu.cn/showproblem.p ...

随机推荐

  1. SSM集成

    SSM集成   Spring和各个框架的整合   Spring目前是JavaWeb开发中最终的框架,提供一站式服务,可以其他各个框架整合集成   Spring整合方案   SSH Ssh是早期的一种整 ...

  2. 使用UTF8字符集存储中文生僻字

    使用UTF8字符集存储中文生僻字 一.相关学习BLOG https://www.cnblogs.com/jyzhao/p/8654412.html http://blog.itpub.net/7818 ...

  3. 我对DES AES RSA的认识

    1.DES(Data Encryption Standard)算法:数据加密标准.是替换和置换细致而复杂的结合体,替换和置换一个接着一个,共循环16次.算法首先将明文分块,每块64位.密钥也是64位, ...

  4. 数据仓库之抽取数据:通过openrowset执行存储过程

    原文:数据仓库之抽取数据:通过openrowset执行存储过程 在做数据仓库时,最重要的就是ETL的开发,而在ETL开发中的第一步,就是要从原OLTP系统中抽取数据到过渡区中,再对这个过渡区中的数据进 ...

  5. [C#] LINQ之SelectMany和GroupJoin

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  6. react请求接口数据是在componentDidMount 还是componentWillMount周期好

    如果你要获取外部数据并加载到组件上,只能在组件"已经"挂载到真实的网页上才能作这事情,其它情况你是加载不到组件的.componentDidMount方法中的代码,是在组件已经完全挂 ...

  7. 最全排序算法原理解析、java代码实现以及总结归纳

    算法分类 十种常见排序算法可以分为两大类: 非线性时间比较类排序:通过比较来决定元素间的相对次序,由于其时间复杂度不能突破O(nlogn),因此称为非线性时间比较类排序. 线性时间非比较类排序:不通过 ...

  8. el-table——可编辑拖拽转换csv格式的表格

    <!--可拖拽的表格:表格内容+行参数+按钮名称(对话框标题)--> <template> <div> <el-button size="mini& ...

  9. 【Linux】Linux基本命令

    一.Linux关机 shutdown -h 10 10min后关机 shutdown -h 10:00   10:00关机 shutdown -h now 或 halt 或 poweroff 立即关机 ...

  10. vi/vim常用按键

    最近这段时间坚持了vim的使用,我在我的IDEA里面加了一个插件,可以支持vim. 然后不管是IDEA还是Vim都有自己的按键,而且都很好用,所以我就总结下在IDEA下的vim使用命令 当然,都是原生 ...