Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 8768 | Accepted: 2343 |
Description
You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edges are numbered 1 through N − 1. Each edge is associated with a weight. Then you are to execute a series of instructions on the tree. The instructions can be one of the following forms:
CHANGE i v | Change the weight of the ith edge to v |
NEGATE a b | Negate the weight of every edge on the path from a to b |
QUERY a b | Find the maximum weight of edges on the path from a to b |
Input
The input contains multiple test cases. The first line of input contains an integer t (t ≤ 20), the number of test cases. Then follow the test cases.
Each test case is preceded by an empty line. The first nonempty line of its contains N (N ≤ 10,000). The next N − 1 lines each contains three integers a, b and c, describing an edge connecting nodes a and b with weight c. The edges are numbered in the order they appear in the input. Below them are the instructions, each sticking to the specification above. A lines with the word “DONE
” ends the test case.
Output
For each “QUERY
” instruction, output the result on a separate line.
Sample Input
1 3 1 2 1 2 3 2 QUERY 1 2 CHANGE 1 3 QUERY 1 2 DONE
Sample Output
1 3题意:一颗树上有3个操作:询问操作,询问a点和b点之间的路径上最长的那条边的长度;取反操作,将a点和b点之间的路径权值都取相反数;变化操作,把某条边的权值变成指定的值
题解:边权的树链剖分,线段树上操作
#include<cstdio>
#include<string.h>
#include<algorithm>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int maxn = 100010;
struct Edge{
int v,c,nxt;
}edge[2*maxn];
int f[maxn],p[maxn],num[maxn],son[maxn],top[maxn],deep[maxn],totp;
int head[maxn],tot,u[maxn],v[maxn],c[maxn];
int Max[maxn<<2],Min[maxn<<2],cover[maxn<<2],d[maxn];
void add(int u,int v,int c){
edge[tot].v=v;
edge[tot].c=c;
edge[tot].nxt=head[u];
head[u]=tot++;
}
void dfs1(int u,int fa,int cnt){
f[u]=fa;
deep[u]=cnt;
son[u]=0;
num[u]=1;
int tmp=0;
for(int i=head[u];~i;i=edge[i].nxt){
int v=edge[i].v;
if(v==fa) continue;
dfs1(v,u,cnt+1);
if(tmp<num[v]) son[u]=v,tmp=num[v];
num[u]+=num[v];
}
}
void dfs2(int u,int t){
top[u]=t;
p[u]=totp++;
if(son[u]) dfs2(son[u],t);
for(int i=head[u];~i;i=edge[i].nxt){
int v=edge[i].v;
if(v==f[u]) continue;
if(v==son[u]){
d[p[v]]=edge[i].c;
continue;
}
dfs2(v,v);
d[p[v]]=edge[i].c;
}
}
void prebuild(){
dfs1(1,0,0);
dfs2(1,1);
}
void PushUP(int rt){
Max[rt]=max(Max[rt<<1],Max[rt<<1|1]);
Min[rt]=min(Min[rt<<1],Min[rt<<1|1]);
}
void PushDown(int rt){
if(cover[rt]==-1){
cover[rt<<1]=-cover[rt<<1];
cover[rt<<1|1]=-cover[rt<<1|1];
cover[rt]=1;
int tmp=Max[rt<<1];
Max[rt<<1]=-Min[rt<<1];
Min[rt<<1]=-tmp;
tmp=Max[rt<<1|1];
Max[rt<<1|1]=-Min[rt<<1|1];
Min[rt<<1|1]=-tmp;
}
}
void build(int l,int r,int rt){
cover[rt]=1;
if(l==r){
Max[rt]=Min[rt]=d[l];
return;
}
int m=(l+r)>>1;
build(lson);
build(rson);
PushUP(rt);
}
void update1(int L,int R,int l,int r,int rt){
if(L<=l&&R>=r) {
cover[rt]=-cover[rt];
int tmp=Max[rt];
Max[rt]=-Min[rt];
Min[rt]=-tmp;
return;
}
PushDown(rt);
int m=(l+r)>>1;
if(L<=m) update1(L,R,lson);
if(R>m) update1(L,R,rson);
PushUP(rt);
}
void update(int p,int c,int l,int r,int rt){
if(l==r){
cover[rt]=1;
Max[rt]=Min[rt]=c;
return;
}
PushDown(rt);
int m=(l+r)>>1;
if(p<=m) update(p,c,lson);
else update(p,c,rson);
PushUP(rt);
}
int query(int L,int R,int l,int r,int rt){
if(L<=l&&R>=r) return Max[rt];
PushDown(rt);
int m=(l+r)>>1;
int ret=-INF;
if(L<=m) ret=max(ret,query(L,R,lson));
if(R>m) ret=max(ret,query(L,R,rson));
return ret;
}
void Negate(int u,int v){
int f1=top[u],f2=top[v];
while(f1!=f2){
if(deep[f1]<deep[f2]){
swap(f1,f2);
swap(u,v);
}
update1(p[f1],p[u],1,totp-1,1);
u=f[f1];f1=top[u];
}
if(u!=v){
if(deep[u]>deep[v]) swap(u,v);
update1(p[son[u]],p[v],1,totp-1,1);
}
}
void Query(int u,int v){
int f1=top[u],f2=top[v],ans=-INF;
while(f1!=f2){
if(deep[f1]<deep[f2]){
swap(f1,f2);
swap(u,v);
}
ans=max(ans,query(p[f1],p[u],1,totp-1,1));
u=f[f1];f1=top[u];
}
if(u!=v){
if(deep[u]>deep[v]) swap(u,v);
ans=max(ans,query(p[son[u]],p[v],1,totp-1,1));
}
printf("%d\n",ans);
}
int main(){
// freopen("in.txt","r",stdin);
int n,T;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
memset(head,-1,sizeof(head));
tot=totp=0;
for(int i=1;i<n;i++){
scanf("%d%d%d",&u[i],&v[i],&c[i]);//要用数组保存
add(u[i],v[i],c[i]);
add(v[i],u[i],c[i]);
}
prebuild();
build(1,totp-1,1);
char op[20];
int a,b;
while(1){
scanf("%s",op);
if(op[0]=='D') break;
scanf("%d%d",&a,&b);
if(op[0]=='C'){
int tmp=deep[u[a]]>deep[v[a]]?u[a]:v[a];//找出深度大的那个点
update(p[tmp],b,1,totp-1,1);//更新进入深度大的点那条边
}
else if(op[0]=='N') Negate(a,b);
else if(op[0]=='Q') Query(a,b);
}
}
return 0;
}