题目大意:给棵树,有边权,然后有三种操作:把第i条边的权值变成w;把x到y路径上的所有边的权值取相反数;查询x到y路径上所有边的权值的最大值。
思路:还是老套路把边权下放到点权,有点不同的是这回维护的是最大值,也正因如此,第二个操作:路径权值取相反数就可以比较简单实现了:x到y路径上的最大值取最小值的相反数,最小值取最大值的相反数就可以了。于是这道题就变成了写一棵维护最大值最小值,且支持单点修改,区间取相反数,区间查询的线段树;以及写一下点权边权转换的一些操作的题了。
注意:区间最大值可能是个负数,所以中间有些mx的初值要取到-inf
写树剖真是费手
AC代码:
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std;
typedef long long LL;//其实应该不用开longlong
const int maxn=1e4+10;
const LL inf=(LL)2e9;
struct edge{
int to,next;
LL w;
}e[maxn<<1];
int head[maxn<<1],cnt;
int n;
int d[maxn],fa[maxn],tot[maxn],top[maxn];
int son[maxn],idx[maxn];
LL a[maxn],W[maxn];
int ct,m,r;
void add_edge(int u,int v,LL w)
{
cnt++;
e[cnt].to=v;
e[cnt].w=w;
e[cnt].next=head[u];
head[u]=cnt;
}
struct node{
int left,right;
LL mx,mn;
int tag;
}tree[maxn<<2];
void pushup(int root)
{
tree[root].mx=max(tree[root<<1].mx,tree[root<<1|1].mx);
tree[root].mn=min(tree[root<<1].mn,tree[root<<1|1].mn);
}
void pushdown(int root)
{
if(!tree[root].tag) return;
LL tmpx,tmpn;
tree[root<<1].tag=!tree[root<<1].tag;
tmpx=tree[root<<1].mx,tmpn=tree[root<<1].mn;
tree[root<<1].mx=-tmpn;
tree[root<<1].mn=-tmpx;
tree[root<<1|1].tag=!tree[root<<1|1].tag;
tmpx=tree[root<<1|1].mx,tmpn=tree[root<<1|1].mn;
tree[root<<1|1].mx=-tmpn;
tree[root<<1|1].mn=-tmpx;
tree[root].tag=0;
}
void build(int root,int left,int right)
{
tree[root].left=left,tree[root].right=right;
if(left==right){
tree[root].mx=a[left];
tree[root].mn=a[left];
return;
}
int mid=(left+right)>>1;
build(root<<1,left,mid);
build(root<<1|1,mid+1,right);
pushup(root);
}
void change(int root,int k,LL val)
{
if(k==tree[root].left&&k==tree[root].right){
tree[root].mx=tree[root].mn=val;
return;
}
pushdown(root);
if(k<=tree[root<<1].right) change(root<<1,k,val);
if(k>=tree[root<<1|1].left) change(root<<1|1,k,val);
pushup(root);
}
void neg(int root,int left,int right)
{
if(left<=tree[root].left&&tree[root].right<=right){
LL tmpx=tree[root].mx;
LL tmpn=tree[root].mn;
tree[root].mx=-tmpn;
tree[root].mn=-tmpx;
tree[root].tag=!tree[root].tag;
return;
}
pushdown(root);
if(tree[root<<1].right>=left) neg(root<<1,left,right);
if(tree[root<<1|1].left<=right) neg(root<<1|1,left,right);
pushup(root);
}
LL search(int root,int left,int right)
{
if(left<=tree[root].left&&tree[root].right<=right){
return tree[root].mx;
}
pushdown(root);
LL mx=-inf;
if(tree[root<<1].right>=left) mx=max(mx,search(root<<1,left,right));
if(tree[root<<1|1].left<=right) mx=max(mx,search(root<<1|1,left,right));
return mx;
}
int dfs1(int now,int pre,int dep)
{
d[now]=dep;
fa[now]=pre;
tot[now]=1;
int mxson=-1;
for(int i=head[now];i;i=e[i].next){
int nxt=e[i].to;
if(nxt==pre) continue;
tot[now]+=dfs1(nxt,now,dep+1);
if(tot[nxt]>mxson) mxson=tot[nxt],son[now]=nxt;
}
return tot[now];
}
void dfs2(int now,int topf)
{
idx[now]=++ct;
a[ct]=W[now];
top[now]=topf;
if(!son[now]) return;
dfs2(son[now],topf);
for(int i=head[now];i;i=e[i].next){
int nxt=e[i].to;
if(nxt==fa[now]||nxt==son[now]) continue;
dfs2(nxt,nxt);
}
}
LL tree_mx(int x,int y)
{
LL mx=-inf;
while(top[x]!=top[y]){
if(d[top[x]]<d[top[y]]) swap(x,y);
mx=max(mx,search(1,idx[top[x]],idx[x]));
x=fa[top[x]];
}
if(x==y) return mx;
if(d[x]>d[y]) swap(x,y);
mx=max(mx,search(1,idx[x]+1,idx[y]));
return mx;
}
void tree_negate(int x,int y)
{
while(top[x]!=top[y]){
if(d[top[x]]<d[top[y]]) swap(x,y);
neg(1,idx[top[x]],idx[x]);
x=fa[top[x]];
}
if(x==y) return;
if(d[x]>d[y]) swap(x,y);
neg(1,idx[x]+1,idx[y]);
}
void init()
{
memset(head,0,sizeof(head));
memset(son,0,sizeof(son));
memset(tree,0,sizeof(tree));
cnt=ct=0;
}
struct RD{
int u,v;
LL w;
}road[maxn];
int main()
{
int t;scanf("%d",&t);
while(t--){
init();
scanf("%d",&n);
for(int i=1;i<n;i++){
int u,v;LL w;
scanf("%d%d%lld",&u,&v,&w);
add_edge(u,v,w);
add_edge(v,u,w);
road[i].u=u,road[i].v=v,road[i].w=w;
}
r=1;
dfs1(r,0,1);
for(int i=1;i<n;i++){
if(d[road[i].u]>d[road[i].v])
swap(road[i].v,road[i].u);
W[road[i].v]=road[i].w;
}
dfs2(r,r);
build(1,1,n);
char opt[10];
while(~scanf("%s",opt)){
int x,y;
LL w;
if(opt[0]=='D') break;
if(opt[0]=='Q'){
scanf("%d%d",&x,&y);
LL ans=tree_mx(x,y);
printf("%lld\n",ans);
}
else if(opt[0]=='C'){
scanf("%d%lld",&x,&w);
int v=road[x].v;
change(1,idx[v],w);
}
else if(opt[0]=='N'){
scanf("%d%d",&x,&y);
tree_negate(x,y);
}
}
}
}