SPOJ375 Query on a tree

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

Hint

Added by: Thanh-Vy Hua
Date: 2005-06-08
Time limit: 0.851s
Source limit: 15000B
Memory limit: 1536MB
Cluster: Cube (Intel G860)
Languages: ADA ASM BASH BF C C# C++ 5 CLPS LISP sbcl LISP clisp D FORT HASK ICON ICK JAVA LUA NEM NICE CAML PAS gpc PAS fpc PERL PHP PIKE PRLG PYTH 2.7 RUBY SCM qobi SCM guile ST TEXT WSPC

 

 

树链剖分模板题。

维护单边权修改,查询链上边权最大值。

树链剖分是以点为基本单位的,需要维护边时,可以将边映射到它深度较大的那个端点上。查询时,不能经过LCA结点(因为该点对应的边不在所求链上)。

 

  1 /*by SilverN*/
  2 #include<algorithm>
  3 #include<iostream>
  4 #include<cstring>
  5 #include<cstdio>
  6 #include<cmath>
  7 #include<vector>
  8 #define lc rt<<1
  9 #define rc rt<<1|1
 10 using namespace std;
 11 const int mxn=10010;
 12 int read(){
 13     int x=0,f=1;char ch=getchar();
 14     while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();}
 15     while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
 16     return x*f;
 17 }
 18 int T;
 19 int n,m;
 20 int pe[mxn][3];
 21 struct edge{
 22     int v,nxt,w;
 23 }e[mxn<<1];
 24 int hd[mxn],mct=0;
 25 void add_edge(int u,int v,int d){
 26     e[++mct].v=v;e[mct].nxt=hd[u];e[mct].w=d;hd[u]=mct;return;
 27 }
 28 struct node{
 29     int f,son;
 30     int top,size;
 31     int w,e,dep;
 32 }tr[mxn];
 33 int sz=0;
 34 void DFS1(int u){
 35     tr[u].size=1;
 36     tr[u].son=0;
 37     for(int i=hd[u];i;i=e[i].nxt){
 38         int v=e[i].v;
 39         if(v==tr[u].f)continue;
 40         tr[v].dep=tr[u].dep+1;
 41         tr[v].f=u;
 42         DFS1(v);
 43         tr[u].size+=tr[v].size;
 44         if(tr[v].size>tr[tr[u].son].size)tr[u].son=v;
 45     }
 46     return;
 47 }
 48 void DFS2(int u,int top){
 49     tr[u].top=top;
 50     tr[u].w=++sz;
 51     if(tr[u].son){
 52         DFS2(tr[u].son,top);
 53         for(int i=hd[u];i;i=e[i].nxt){
 54             int v=e[i].v;
 55             if(v!=tr[u].f && v!=tr[u].son){
 56                 DFS2(v,v);
 57             }
 58         }
 59     }
 60     tr[u].e=sz;
 61 }
 62 //
 63 struct segtree{
 64     int mx;
 65 }st[mxn<<2];
 66 void change(int p,int v,int l,int r,int rt){
 67     if(l==r){
 68         if(l==p)
 69             st[rt].mx=v;
 70         return;
 71     }
 72     int mid=(l+r)>>1;
 73     if(p<=mid)change(p,v,l,mid,lc);
 74     else change(p,v,mid+1,r,rc);
 75     st[rt].mx=max(st[lc].mx,st[rc].mx);
 76     return;
 77 }
 78 int qmx(int L,int R,int l,int r,int rt){
 79     if(L<=l && r<=R)return st[rt].mx;
 80     int mid=(l+r)>>1;
 81     int res=-1e9;
 82     if(L<=mid)res=max(res,qmx(L,R,l,mid,lc));
 83     if(R>mid)res=max(res,qmx(L,R,mid+1,r,rc));
 84     return res;
 85 }
 86 int query(int x,int y){
 87     int res=-1e9;
 88     while(tr[x].top!=tr[y].top){
 89         if(tr[tr[x].top].dep<tr[tr[y].top].dep)swap(x,y);
 90         res=max(res,qmx(tr[tr[x].top].w,tr[x].w,1,n,1));
 91         x=tr[tr[x].top].f;
 92     }
 93     if(tr[x].dep>tr[y].dep)swap(x,y);
 94     if(x!=y)res=max(res,qmx(tr[tr[x].son].w,tr[y].w,1,n,1));//不经过公共祖先 
 95     return res;
 96 }
 97 //
 98 void init(){
 99     memset(st,0,sizeof st);
100     memset(hd,0,sizeof hd);
101     mct=0;sz=0;
102 }
103 int main(){
104     T=read();
105     int i,j,x,y,z;
106     while(T--){
107         init();
108         n=read();
109         int rt=n/2+1;
110         for(i=1;i<n;i++){
111             x=read();y=read();z=read();
112             add_edge(x,y,z);
113             add_edge(y,x,z);
114             pe[i][0]=x;pe[i][1]=y;pe[i][2]=z;//记录边信息 
115         }
116         tr[rt].f=tr[rt].son=tr[rt].dep=0;
117         DFS1(rt);
118         DFS2(rt,rt);
119         for(i=1;i<n;i++){
120             if(tr[pe[i][0]].dep>tr[pe[i][1]].dep)swap(pe[i][0],pe[i][1]);
121             change(tr[pe[i][1]].w,pe[i][2],1,n,1);
122         }
123         char op[10];
124         while(scanf("%s",op) && op[0]!='D'){
125             if(op[0]=='Q'){
126                 x=read();y=read();
127                 printf("%d\n",query(x,y));
128             }
129             if(op[0]=='C'){
130                 x=read();y=read();
131                 change(tr[pe[x][1]].w,y,1,n,1);
132             }
133         }
134     }
135     return 0;
136 }

 

posted @ 2016-11-24 17:35  SilverNebula  阅读(185)  评论(0编辑  收藏  举报
AmazingCounters.com