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 }

 

转载于:https://www.cnblogs.com/SilverNebula/p/6098730.html

洛谷的SPOJ需要注册一个SPOJ账号并进行绑定才能进行交题。您可以按照以下步骤进行注册: 1. 打开洛谷网站(https://www.luogu.com.cn/)并登录您的洛谷账号。 2. 在网站顶部导航栏中找到“题库”选项,将鼠标悬停在上面,然后选择“SPOJ”。 3. 在SPOJ页面上,您会看到一个提示,要求您注册SPOJ账号并进行绑定。点击提示中的链接,将会跳转到SPOJ注册页面。 4. 在SPOJ注册页面上,按照要求填写您的用户名、密码和邮箱等信息,并完成注册。 5. 注册完成后,返回洛谷网站,再次进入SPOJ页面。您会看到一个输入框,要求您输入刚刚注册的SPOJ用户名。输入用户名后,点击“绑定”按钮即可完成绑定。 现在您已经成功注册并绑定了SPOJ账号,可以开始在洛谷的SPOJ题库上刷题了。祝您顺利完成编程练习!\[1\]\[2\] #### 引用[.reference_title] - *1* *3* [(洛谷入门系列,适合洛谷新用户)洛谷功能全解](https://blog.csdn.net/rrc12345/article/details/122500057)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [luogu p7492 序列](https://blog.csdn.net/zhu_yin233/article/details/122051384)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值