rt,全WA了,调不出来了/kk
望大佬指教蒟蒻qwq
代码有亿点长,如果有人帮我那万分感谢!
每个节点维护了三个信息:覆盖值、最大值、增加值。下传的时候先覆盖后增加,修改时覆盖的话把加的标记清零。
发现了nt错误别嘲讽/kel
#include<bits/stdc++.h>
using namespace std;
const int N=100010;
int head[N],nex[N*2],to[N*2],e[N*2],cnt;
void add(int x,int y,int z){
cnt++;
nex[cnt]=head[x];
to[cnt]=y;
e[cnt]=z;
head[x]=cnt;
}
int t[N],a[N],dfn[N],tp[N],fa[N],dep[N],sz[N],son[N],tot;
void dfs1(int x,int f){
fa[x]=f;
dep[x]=dep[f]+1;
sz[x]=1;
int maxn=-1;
for(int i=head[x];i;i=nex[i]){
int y=to[i];
if(y==f)
continue;
t[y]=e[i];
dfs1(y,x);
sz[x]+=sz[y];
if(sz[y]>maxn){
maxn=sz[y];
son[x]=y;
}
}
}
void dfs2(int x,int top){
tp[x]=top;
tot++;
dfn[x]=tot;
a[tot]=t[x];
if(son[x])
dfs2(son[x],top);
for(int i=head[x];i;i=nex[i]){
int y=to[i];
if(y==fa[x]||y==son[x])
continue;
dfs2(y,y);
}
}
struct Segment_Tree{
int maxx;
int add_tag;
int change_tag;
}st[N*4];
void build(int root,int l,int r){
if(l==r){
st[root].maxx=a[l];
return;
}
int mid=(l+r)/2;
build(root*2,l,mid);
build(root*2+1,mid+1,r);
st[root].maxx=max(st[root*2].maxx,st[root*2+1].maxx);
}
int res;
void push_down(int root,int l,int r){
if(st[root].change_tag==-1){
st[root*2].maxx+=st[root].add_tag;
st[root*2+1].maxx+=st[root].add_tag;
st[root*2].add_tag+=st[root].add_tag;
st[root*2+1].add_tag+=st[root].add_tag;
}
else{
st[root*2].maxx=st[root].change_tag+st[root].add_tag;
st[root*2+1].maxx=st[root].change_tag+st[root].add_tag;
st[root*2].change_tag=st[root].change_tag;
st[root*2+1].change_tag=st[root].change_tag;
st[root*2].add_tag=st[root].add_tag;
st[root*2+1].add_tag=st[root].add_tag;
}
st[root].maxx=max(st[root*2].maxx,st[root*2+1].maxx);
st[root].add_tag=0;
st[root].change_tag=-1;
}
void change(int root,int l,int r,int x,int y,int k){
if(l>=x&&r<=y){
st[root].maxx=k;
st[root].change_tag=k;
st[root].add_tag=0;
return;
}
push_down(root,l,r);
int mid=(l+r)/2;
if(mid>=x)
change(root*2,l,mid,x,y,k);
if(mid+1<=y)
change(root*2+1,mid+1,r,x,y,k);
st[root].maxx=max(st[root*2].maxx,st[root*2+1].maxx);
}
void Add(int root,int l,int r,int x,int y,int k){
if(l>=x&&r<=y){
st[root].maxx+=k;
st[root].add_tag+=k;
return;
}
push_down(1,l,r);
int mid=(l+r)/2;
if(mid>=x)
Add(root*2,l,mid,x,y,k);
if(mid+1<=y)
Add(root*2+1,mid+1,r,x,y,k);
st[root].maxx=max(st[root*2].maxx,st[root*2+1].maxx);
}
void ask(int root,int l,int r,int x,int y){
if(l>=x&&r<=y){
res=max(res,st[root].maxx);
return;
}
push_down(root,l,r);
int mid=(l+r)/2;
if(mid>=x)
ask(root*2,l,mid,x,y);
if(mid+1<=y)
ask(root*2+1,mid+1,r,x,y);
}
struct Edge{
int x;
int y;
}g[N];
int main(){
// freopen("P4315 月下“毛景树”.in","r",stdin);
// freopen("P4315 月下“毛景树”.out","w",stdout);
int n;
cin>>n;
for(int i=1;i<n;i++){
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
add(y,x,z);
g[i].x=x;
g[i].y=y;
}
dfs1(1,1);
dfs2(1,1);
build(1,1,n);
for(int i=1;i<=4*n;i++)
st[i].change_tag=-1;
string opt;
while(cin>>opt&&opt!="Stop"){
if(opt=="Max"){
int x,y;
res=0;
scanf("%d%d",&x,&y);
while(tp[x]!=tp[y]){
if(dep[tp[x]]<dep[tp[y]])
swap(x,y);
ask(1,1,n,dfn[tp[x]],dfn[x]);
x=fa[tp[x]];
}
if(x!=y){
if(dep[x]>dep[y])
swap(x,y);
ask(1,1,n,dfn[x]+1,dfn[y]);
}
printf("%d\n",res);
}
if(opt=="Change"){
int p,k;
int x,y;
scanf("%d%d",&p,&k);
x=g[p].x;
y=g[p].y;
change(1,1,n,max(dfn[x],dfn[y]),max(dfn[x],dfn[y]),k);
}
if(opt=="Add"){
int x,y,k;
scanf("%d%d%d",&x,&y,&k);
while(tp[x]!=tp[y]){
if(dep[tp[x]]<dep[tp[y]])
swap(x,y);
Add(1,1,n,dfn[tp[x]],dfn[x],k);
x=fa[tp[x]];
}
if(x!=y){
if(dep[x]>dep[y])
swap(x,y);
Add(1,1,n,dfn[x]+1,dfn[y],k);
}
}
if(opt=="Cover"){
int x,y,k;
scanf("%d%d%d",&x,&y,&k);
while(tp[x]!=tp[y]){
if(dep[tp[x]]<dep[tp[y]])
swap(x,y);
change(1,1,n,dfn[tp[x]],dfn[x],k);
x=fa[tp[x]];
}
if(x!=y){
if(dep[x]>dep[y])
swap(x,y);
change(1,1,n,dfn[x]+1,dfn[y],k);
}
}
}
return 0;
}