找了好久了还是找不出来,样例都过不了 题目P4315 代码:
//月下毛景树
#include<bits/stdc++.h>
using namespace std;
inline int read() {
int x=0,f=0;char ch=getchar();
for(;!isdigit(ch);ch=getchar()) f|=(ch=='-');
for(;isdigit(ch);ch=getchar()) x=(x<<1)+(x<<3)+(ch^48);
return f?-x:x;
}
void print(int x) {
if(x<0) putchar('-'),x=-x;
if(x>9) print(x/10);
putchar(x%10+48);
}
int x,y,z,n,m,a[1023231],head[1012311],tot,cnt,U[1023123],V[1023213];
int top[1023213],son[1023123],size[1023131],dfn[13021313],pre[1023123],dep[1023123],fa[1023123];
struct node{
int next,to,w;
}e[10231231];
void add(int u,int v,int w) {
e[++cnt].next=head[u];
e[cnt].to=v;
e[cnt].w=w;
head[u]=cnt;
}
namespace ss{
#define lson pos<<1
#define rson pos<<1|1
struct node{
int sum,len,lazy,lazy2;
}tree[1210232];
void build(int pos,int l,int r) {
tree[pos].len=r-l+1; tree[pos].lazy2=-0x7ffffff;
if (l==r) {
tree[pos].sum=a[pre[l]]; return ;
}
int mid=l+r>>1;
build(lson,l,mid); build(rson,mid+1,r);
tree[pos].sum=max(tree[lson].sum,tree[rson].sum);
}
void push_down(int pos) {
if (tree[pos].lazy) {
tree[lson].lazy+=tree[pos].lazy;
tree[rson].lazy+=tree[pos].lazy;
tree[lson].sum+=tree[pos].lazy*tree[lson].len;
tree[rson].sum+=tree[pos].lazy*tree[rson].len;
tree[pos].lazy=0;
}
if (tree[pos].lazy2!=-0x7ffffff) {
tree[rson].sum=tree[lson].sum=tree[rson].lazy2=tree[lson].lazy2=tree[pos].lazy2;
tree[pos].lazy2=-0x7ffffff;
}
}
void change(int pos,int l,int r,int L,int R,int k){
if (l>=L && r<=R) {
tree[pos].sum+=k*tree[pos].len;
tree[pos].lazy+=k; return ;
}
int mid=l+r>>1; push_down(pos);
if (L<=mid) change(lson,l,mid,L,R,k);
if (R>mid) change(rson,mid+1,r,L,R,k);
tree[pos].sum=max(tree[lson].sum,tree[rson].sum);
}
void update(int pos,int l,int r,int L,int R,int k) {
if (l>=L && r<=R) {
tree[pos].sum=k; tree[pos].lazy2=k; return ;
}
int mid=l+r>>1; push_down(pos);
if (L<=mid) update(lson,l,mid,L,R,k);
if (R>mid) update(rson,mid+1,r,L,R,k);
tree[pos].sum=max(tree[lson].sum,tree[rson].sum);
}
int query(int pos,int l,int r,int L,int R) {
if (l>=L && r<=R) return tree[pos].sum;
int mid=l+r>>1,res=-0x7ffffff; push_down(pos);
if (L<=mid) res=max(res,query(lson,l,mid,L,R));
if (R>mid) res=max(res,query(rson,mid+1,r,L,R));
return res;
}
}
namespace sp {
void dfs1(int now,int Fa) {
size[now]=1; fa[now]=Fa; dep[now]=dep[Fa]+1;
for (int i=head[now];i;i=e[i].next) {
if (e[i].to==Fa) continue;
a[e[i].to]=e[i].w;
dfs1(e[i].to,now);
size[now]+=size[e[i].to];
if (size[son[now]]<size[e[i].to]) son[now]=e[i].to;
}
}
void dfs2(int now,int Top) {
dfn[now]=++tot; pre[tot]=now; top[now]=Top;
if (son[now]) dfs2(son[now],Top);
for (int i=head[now];i;i=e[i].next) {
if (e[i].to==son[now]||e[i].to==fa[now]) continue;
dfs2(e[i].to,e[i].to);
}
}
void change(int x,int y,int p) {
while(top[x]^top[y]) {
if (dep[top[x]]<dep[top[y]]) swap(x,y);
ss::change(1,1,n,dfn[top[x]],dfn[x],p);
x=fa[top[x]];
}
if (dep[x]>dep[y]) swap(x,y);
ss::change(1,1,n,dfn[x]+1,dfn[y],p);
}
void update(int x,int y,int p) {
while(top[x]^top[y]) {
if (dep[top[x]]<dep[top[y]]) swap(x,y);
ss::update(1,1,n,dfn[top[x]],dfn[x],p);
x=fa[top[x]];
}
if (dep[x]>dep[y]) swap(x,y);
ss::update(1,1,n,dfn[x]+1,dfn[y],p);
}
int query(int x,int y) {
int res=-0x7ffffff;
while(top[x]^top[y]) {
if (dep[top[x]]<dep[top[y]]) swap(x,y);
res=max(res,ss::query(1,1,n,dfn[top[x]],dfn[x]));
x=fa[top[x]];
}
if (dep[x]>dep[y]) swap(x,y);
res=max(res,ss::query(1,1,n,dfn[x]+1,dfn[y]));
return res;
}
}
signed main() {
n=read();
for (int i=1;i<n;++i) {
x=read(); y=read(); z=read();
U[i]=x; V[i]=y;
add(x,y,z); add(y,x,z);
}
sp::dfs1(1,0); sp::dfs2(1,1); ss::build(1,1,n);
while(1) {
string s;
cin>>s;
if (s[0]=='S') return 0;
if (s[0]=='M') {
x=read(); y=read();
print(sp::query(x,y)); putchar('\n');
}
else if (s[1]=='h') {
x=read(); z=read();
ss::update(1,1,n,dfn[U[x]],dfn[U[x]],z);
}
else if (s[1]=='o') {
x=read(); y=read(); z=read();
sp::update(x,y,z);
}
else if (s[0]=='A') {
x=read(); y=read(); z=read();
sp::change(x,y,z);
}
}
return 0;
}