luogu P4315 月下“毛景树”
题面传送门
普通树剖,注意线段树标记下推清零时左右儿子的懒标记要清零
代码实现;
#include<cstdio>
#include<cstring>
#define max(a,b) ((a)>(b)?(a):(b))
using namespace std;
int n,m,k,son[100039],siz[100039],d[100039],fa[100039],id[100039],idea,top[100039],x[100039],y[100039],z[100039],sx,sy, f[400039],flag[400039],sum[400039],sz;
char _s;
struct yyy{int to,w,z;};
struct ljb{
int head,h[100039];
yyy f[200039];
inline void add(int x,int y,int z){
f[++head]=(yyy){y,z,h[x]};
h[x]=head;
}
}s;
inline void push(int l,int r,int now){
if(flag[now]){
flag[now<<1]=flag[now<<1|1]=1;
sum[now<<1]=sum[now<<1|1]=0;
f[now<<1]=f[now<<1|1]=0;
flag[now]=0;
}
if(f[now]){
f[now<<1]+=f[now];f[now<<1|1]+=f[now];
sum[now<<1]+=f[now];
sum[now<<1|1]+=f[now];
f[now]=0;
}
}
inline void get1(int x,int y,int l,int r,int now){
if(x<=l&&r<=y) {push(l,r,now);flag[now]=1,sum[now]=0;return;}
push(l,r,now);
int m=(l+r)>>1;
if(x<=m)get1(x,y,l,m,now<<1);
if(y>m) get1(x,y,m+1,r,now<<1|1);
sum[now]=max(sum[now<<1],sum[now<<1|1]);
}
inline void get2(int x,int y,long long z,int l,int r,int now){
if(x<=l&&r<=y) {push(l,r,now);f[now]+=z,sum[now]+=z;return;}
push(l,r,now);
int m=(l+r)>>1;
if(x<=m) get2(x,y,z,l,m,now<<1);
if(y>m) get2(x,y,z,m+1,r,now<<1|1);
sum[now]=max(sum[now<<1],sum[now<<1|1]);
}
inline int find(int x,int y,int l,int r,int now){
if(x<=l&&r<=y)return sum[now];
push(l,r,now);
int m=(l+r)>>1,ans1=-1e10,ans2=-1e10;
if(x<=m) ans1=find(x,y,l,m,now<<1);
if(y>m) ans2=find(x,y,m+1,r,now<<1|1);
return max(ans1,ans2);
}
inline void dfs1(int x,int last){
fa[x]=last;
d[x]=d[last]+1;
siz[x]=1;
int cur=s.h[x],pus=-1;
yyy tmp;
while(cur!=-1){
tmp=s.f[cur];
if(tmp.to!=last){
dfs1(tmp.to,x);
siz[x]+=siz[tmp.to];
if(siz[tmp.to]>pus)pus=siz[tmp.to],son[x]=tmp.to;
}
cur=tmp.z;
}
}
inline void dfs2(int x,int last){
top[x]=last;
id[x]=++idea;
int cur=s.h[x];
yyy tmp;
while(cur!=-1){
tmp=s.f[cur];
if(tmp.to==son[x]) get2(idea+1,idea+1,tmp.w,1,n,1),dfs2(tmp.to,last);
cur=tmp.z;
}
cur=s.h[x];
while(cur!=-1){
tmp=s.f[cur];
if(tmp.to!=fa[x]&&tmp.to!=son[x]) get2(idea+1,idea+1,tmp.w,1,n,1),dfs2(tmp.to,tmp.to);
cur=tmp.z;
}
}
inline void swap(int &x,int &y){x^=y,y^=x,x^=y;}
inline void gets1(int x,int y,int z){
if(d[x]>d[y]) swap(x,y);
get1(id[y],id[y],1,n,1);
get2(id[y],id[y],z,1,n,1);
}
inline void gets2(int x,int y,int z){
while(top[x]!=top[y]){
if(d[top[x]]<d[top[y]]) swap(x,y);
get1(id[top[x]],id[x],1,n,1);
get2(id[top[x]],id[x],z,1,n,1);
x=fa[top[x]];
}
if(d[x]>d[y]) swap(x,y);
get1(id[x]+1,id[y],1,n,1);
get2(id[x]+1,id[y],z,1,n,1);
}
inline void gets3(int x,int y,int z){
while(top[x]!=top[y]){
if(d[top[x]]<d[top[y]]) swap(x,y);
get2(id[top[x]],id[x],z,1,n,1);
x=fa[top[x]];
}
if(d[x]>d[y]) swap(x,y);
get2(id[x]+1,id[y],z,1,n,1);
}
inline int finds(int x,int y){
int ans=0,tot=0;
while(top[x]!=top[y]) {
if(d[top[x]]<d[top[y]]) swap(x,y);
tot=find(id[top[x]],id[x],1,n,1);
ans=max(ans,tot);
x=fa[top[x]];
}
if(d[x]>d[y]) swap(x,y);
tot=find(id[x]+1,id[y],1,n,1);
return max(ans,tot);
}
int main(){
freopen("1.in","r",stdin);
memset(s.h,-1,sizeof(s.h));
register int i;
scanf("%d",&n);
for(i=1;i<n;i++) scanf("%d%d%d",&x[i],&y[i],&z[i]),s.add(x[i],y[i],z[i]),s.add(y[i],x[i],z[i]);
dfs1(1,0);
dfs2(1,1);
while(1){
_s=getchar();
while(_s<'A'||_s>'Z') _s=getchar();
if(_s=='S') return 0;
if(_s=='C'){
_s=getchar();
if(_s=='h') _s=getchar(),_s=getchar(),_s=getchar(),_s=getchar(),scanf("%d%d",&sx,&sz),gets1(x[sx],y[sx],sz);
else _s=getchar(),_s=getchar(),_s=getchar(),scanf("%d%d%d",&sx,&sy,&sz),gets2(sx,sy,sz);
}
else if(_s=='A'){
_s=getchar(),_s=getchar();
scanf("%d%d%d",&sx,&sy,&sz);
gets3(sx,sy,sz);
}
else{
_s=getchar(),_s=getchar();
scanf("%d%d",&sx,&sy);
printf("%d\n",finds(sx,sy));
}
}
}