http://www.elijahqi.win/2018/02/11/spoj-qtree1-luogu4114/
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
1.29启程长沙的时候早晨起来敲的复习板子 然而在noiwc 2018上却成为了tj选手中的倒数第一 qwq 太菜太菜了
洛谷到是很快A了 spoj死活过不去 菜死 直到今天才调过
洛谷:洛谷数据比较水..建议写过之后再去spoj挑战下
#include<cstdio>
#include<algorithm>
#define N 110000
using namespace std;
inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}
while(ch<='9'&&ch>='0') x=x*10+ch-'0',ch=getchar();
return x*f;
}
int h[N],size[N],fa[N],n,num,top[N],w[N],a[N],id[N],dep[N],son[N],root;
char s[10];
struct node{
int y,z,next;
}data[N<<1];
struct node1{
int left,right,max;
}tree[N<<1];
inline void dfs1(int x){
size[x]=1;
for (int i=h[x];i;i=data[i].next){
int y=data[i].y,z=data[i].z;if (y==fa[x]) continue;fa[y]=x;a[y]=z;
dep[y]=dep[x]+1;dfs1(y);size[x]+=size[y];if (size[son[x]]<size[y]) son[x]=y;
}
}
inline void dfs2(int x,int tp){
id[x]=++num;top[x]=tp;w[num]=a[x];
if (son[x]) dfs2(son[x],tp);
for (int i=h[x];i;i=data[i].next){
int y=data[i].y;if (fa[x]==y||y==son[x]) continue;
dfs2(y,y);
}
}
inline void update(int x){
tree[x].max=max(tree[tree[x].left].max,tree[tree[x].right].max);
}
inline void build(int &x,int l,int r){
x=++num;if(l==r) {tree[x].max=w[l];return;}
int mid=l+r>>1;build(tree[x].left,l,mid);
build(tree[x].right,mid+1,r);update(x);
}
inline int query(int x,int l,int r,int l1,int r1){
if (l1<=l&&r1>=r) return tree[x].max;int mid=l+r>>1,tmp=0;
if(l1<=mid) tmp=max(tmp,query(tree[x].left,l,mid,l1,r1));
if (r1>mid) tmp=max(tmp,query(tree[x].right,mid+1,r,l1,r1));return tmp;
}
inline void change(int x,int l,int r,int p,int v){
if (l==r) {tree[x].max=v;return;}int mid=l+r>>1;
if (p<=mid) change(tree[x].left,l,mid,p,v);else change(tree[x].right,mid+1,r,p,v);update(x);
}
int main(){
//freopen("4114.in","r",stdin);
n=read();
for (int i=1;i<n;++i){
int x=read(),y=read(),z=read();
data[++num].y=y;data[num].z=z;data[num].next=h[x];h[x]=num;
data[++num].y=x;data[num].z=z;data[num].next=h[y];h[y]=num;
}dfs1(1);num=0;dfs2(1,1);num=0;build(root,1,n);
while(1){
scanf("%s",s);if (s[0]=='D') return 0;int x=read(),y=read();
if (s[0]=='Q'){
int ans=0;if(x==y) {puts("0");continue;}
while(top[x]!=top[y]){
if (dep[top[x]]<dep[top[y]]) swap(x,y);
ans=max(ans,query(root,1,n,id[top[x]],id[x]));x=fa[top[x]];
}if (id[x]>id[y]) swap(x,y);if(id[x]==id[y]){printf("%d\n",ans);continue;}
ans=max(ans,query(root,1,n,id[x]+1,id[y]));printf("%d\n",ans);
}
if (s[0]=='C'){
x=data[(x<<1)-1].y;x=id[x];change(root,1,n,x,y);
}
}
return 0;
}
spoj版: 注意多组数据需要针对重儿子和h数组清0 并且题目中给边的顺序不一定是按照深度小到深度大这样逐次给的而是乱序给的所以输出答案的时候注意判断一下
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 110000
using namespace std;
inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}
while(ch<='9'&&ch>='0') x=x*10+ch-'0',ch=getchar();
return x*f;
}
int h[N],size[N],fa[N],n,num,top[N],w[N],a[N],id[N],dep[N],son[N],root;
char s[10];
struct node{
int y,z,next;
}data[N<<1];
struct node1{
int left,right,max;
}tree[N<<1];
inline void dfs1(int x){
size[x]=1;
for (int i=h[x];i;i=data[i].next){
int y=data[i].y,z=data[i].z;if (y==fa[x]) continue;fa[y]=x;a[y]=z;
dep[y]=dep[x]+1;dfs1(y);size[x]+=size[y];if (size[son[x]]<size[y]) son[x]=y;
}
}
inline void dfs2(int x,int tp){
id[x]=++num;top[x]=tp;w[num]=a[x];
if (son[x]) dfs2(son[x],tp);
for (int i=h[x];i;i=data[i].next){
int y=data[i].y;if (fa[x]==y||y==son[x]) continue;
dfs2(y,y);
}
}
inline void update(int x){
tree[x].max=max(tree[tree[x].left].max,tree[tree[x].right].max);
}
inline void build(int &x,int l,int r){
x=++num;if(l==r) {tree[x].max=w[l];return;}
int mid=l+r>>1;build(tree[x].left,l,mid);
build(tree[x].right,mid+1,r);update(x);
}
inline int query(int x,int l,int r,int l1,int r1){
if (l1<=l&&r1>=r) return tree[x].max;int mid=l+r>>1,tmp=-0x3f3f3f3f;
if(l1<=mid) tmp=max(tmp,query(tree[x].left,l,mid,l1,r1));
if (r1>mid) tmp=max(tmp,query(tree[x].right,mid+1,r,l1,r1));return tmp;
}
inline void change(int x,int l,int r,int p,int v){
if (l==r) {tree[x].max=v;return;}int mid=l+r>>1;
if (p<=mid) change(tree[x].left,l,mid,p,v);else change(tree[x].right,mid+1,r,p,v);update(x);
}
int main(){
freopen("1.in","r",stdin);
int T=read();
while(T--){
n=read();memset(h,0,sizeof(h));num=0;memset(son,0,sizeof(son));
for (int i=1;i<n;++i){
int x=read(),y=read(),z=read();
data[++num].y=y;data[num].z=z;data[num].next=h[x];h[x]=num;
data[++num].y=x;data[num].z=z;data[num].next=h[y];h[y]=num;
}dfs1(1);num=0;dfs2(1,1);num=0;build(root,1,n);
while(1){
scanf("%s",s);if (s[0]=='D') break;int x=read(),y=read();
if (s[0]=='Q'){
int ans=-0x3f3f3f3f;if(x==y) {puts("0");continue;}
while(top[x]!=top[y]){
if (dep[top[x]]<dep[top[y]]) swap(x,y);
ans=max(ans,query(root,1,n,id[top[x]],id[x]));x=fa[top[x]];
}if (id[x]>id[y]) swap(x,y);if(id[x]==id[y]){printf("%d\n",ans);continue;}
ans=max(ans,query(root,1,n,id[x]+1,id[y]));printf("%d\n",ans);
}
if (s[0]=='C'){
int xx=data[(x<<1)-1].y,yy=data[(x<<1)].y;if (dep[xx]<dep[yy])swap(xx,yy);
x=xx;x=id[x];change(root,1,n,x,y);
}
}
}
return 0;
}