Description:
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:
13
1 2 1
2 3 2
QUERY 1 2
CHANGE 1 3
QUERY 1 2
DONEOutput:
1
3
在线上的区间求最值树剖模板题。
AC代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<iomanip>
#include<math.h>
using namespace std;
typedef long long ll;
typedef double ld;
const int N = 1e4 + 7;
#define ls nd<<1
#define rs nd<<1|1
int eg[N][4], pos[N], sz[N], top[N], fat[N], dep[N], son[N];
int id=0,n;
int i,j,k;
struct Edge
{
int nxt,to;
} e[N<<1];
int head[N],tot=0;
void addeage(int u,int v)
{
e[++tot].nxt=head[u], e[tot].to=v;
head[u]=tot;
}
void dfs1(int u,int fa)
{
son[u]=0;
sz[u]=1;
dep[u]=dep[fa]+1;
for(i=head[u];i;i=e[i].nxt )
{
int v=e[i].to;
if( v==fa )
continue;
fat[v]=u;
dfs1(v, u);
sz[u]+=sz[v];
if( sz[v]>sz[son[u]] )
son[u]=v;
}
}
void dfs2(int u, int fa, int fq)
{
pos[u]=++id;
top[u]=fq;
if( son[u] )
{
dfs2(son[u], u, fq);
}
for ( int i=head[u]; i; i=e[i].nxt )
{
int v=e[i].to;
if( v==fa || v==son[u] )
continue;
dfs2(v, u, v);
}
}
struct Node
{
int maxv;
} tr[N<<2];
void pushup(int nd)
{
tr[nd].maxv=max(tr[ls].maxv, tr[rs].maxv);
}
void modify(int nd,int l,int r,int p,int val)
{
if(l==r)
{
tr[nd].maxv=val;
return;
}
int mid=(l+r)>>1;
if( mid>=p )
modify(ls,l,mid,p,val);
else
modify(rs,mid+1,r,p,val);
pushup(nd);
}
int query(int nd,int l,int r,int L,int R)
{
if(L<=l&&r<=R)
{
return tr[nd].maxv;
}
int mid=(l+r)>>1;
int mx=0;
if(mid>=L)
mx=max(mx,query(ls,l,mid,L,R));
if(mid<R)
mx=max(mx,query(rs,mid+1,r,L,R));
return mx;
}
void swap(int &a, int &b)
{
a=a^b;
b=a^b;
a=a^b;
}
int query2(int u, int v)
{
int f1=top[u],f2=top[v];
int ret=0;
while(f1!=f2)
{
if( dep[f1]<dep[f2] )
swap(f1,f2);
swap(u, v);
ret=max(ret,query(1,1,n,pos[f1],pos[u]) );
u=fat[f1];
f1=top[u];
}
if( u==v )
return ret;
if( dep[u]>dep[v] )
swap(u, v);
ret=max(ret, query(1,1,n,pos[son[u]],pos[v]) );
return ret;
}
void init()
{
memset(head,0,sizeof(head));
tot=0;
id=0;
}
int main()
{
int T;
scanf("%d",&T);
while( T-- )
{
init();
scanf("%d", &n );
for(i=1; i<n; i++ )
{
int x,y,z;
scanf("%d %d %d",&x,&y,&z);
eg[i][1]=x;
eg[i][2]=y;
eg[i][3]=z;
addeage(x,y);
addeage(y,x);
}
dfs1(1,1);
dfs2(1,1,1);
for(i=1; i<n; i++)
{
if( dep[eg[i][1]]<dep[eg[i][2]] )
swap(eg[i][1], eg[i][2]);
modify(1,1,n,pos[eg[i][1]],eg[i][3]);
}
for(;;)
{
char ch[10];
int x,y;
scanf("%s",ch);
if( ch[0]=='D' )
{
break;
}
scanf("%d %d",&x,&y);
if( ch[0]=='Q' )
{
printf("%d\n",query2(x,y) );
}
else if(ch[0]=='C')
{
modify(1,1,n,pos[eg[x][1]],y);
}
}
}
return 0;
}
标签:剖分,--,tree,mid,son,int,QUERY,line,include From: https://blog.51cto.com/u_15952369/6034929