# bzoj1984 月下“毛景树”

http://www.elijahqi.win/archives/811
Description

Input

Output

Sample Input

4
1 2 8
1 3 7
3 4 9
Max 2 4
Cover 2 4 5
Add 1 4 10
Change 1 16
Max 2 4
Stop
Sample Output

9
16
【Data Range】
1<=N<=100,000，操作+询问数目不超过100,000。

#include<cstdio>
#define N 110000
inline int read(){
int x=0,f=1;char ch=getchar();
while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
while (ch<='9'&&ch>='0') {x=x*10+ch-'0';ch=getchar();}
return x*f;
}
struct node{
int y,next,z;
}data[N<<1];
struct node1{
int left,right,l,r,max,lazy,add;
}tree[N<<2];
inline void swap(int &x,int &y){
x^=y;y^=x;x^=y;
}
int size[N],a[N],h[N],son[N],w[N],id[N],num,dep[N],fa[N],tp[N],n,son1[N],root;
void dfs1(int x){
size[x]=1;
for (int i=h[x];i;i=data[i].next){
int y=data[i].y,z=data[i].z;
if (fa[x]==y) continue;a[y]=z;son1[i>>1]=y;
dep[y]=dep[x]+1;fa[y]=x;dfs1(y),size[x]+=size[y];
if (size[y]>size[son[x]]) son[x]=y;
}
}
void dfs2(int x,int top){
id[x]=++num;tp[x]=top;w[num]=a[x];
if (son[x]) dfs2(son[x],top);
for (int i=h[x];i;i=data[i].next){
int y=data[i].y;
if (fa[x]==y||son[x]==y) continue;
dfs2(y,y);
}
}
inline int max(int x,int y){
return x>y?x:y;
}
inline void update(int x){
int l=tree[x].left,r=tree[x].right;
tree[x].max=max(tree[l].max,tree[r].max);
}
inline void pushdown(int x){
if (!tree[x].add&&!tree[x].lazy) return;
int l=tree[x].left,r=tree[x].right;
if (tree[x].lazy){
tree[l].max=tree[r].max=tree[l].lazy=tree[r].lazy=tree[x].lazy;
tree[l].add=tree[r].add=0;
tree[x].lazy=0;
}
if (tree[x].add){
tree[l].max+=tree[x].add;tree[r].max+=tree[x].add;
tree[l].add+=tree[x].add;tree[r].add+=tree[x].add;
tree[x].add=0;
}
}
void build(int &x,int l,int r){
x=++num;
tree[x].l=l;tree[x].r=r;
if (l==r) {tree[x].max=w[l];return;}
int mid=l+r>>1;
build(tree[x].left,l,mid);build(tree[x].right,mid+1,r);
update(x);
}
void change(int x,int l,int r,int v){
if (l<=tree[x].l&&r>=tree[x].r){
tree[x].max=tree[x].lazy=v;tree[x].add=0;return;
}
int mid=(tree[x].l+tree[x].r)>>1;pushdown(x);
if (l<=mid) change(tree[x].left,l,r,v);
if (r>mid) change(tree[x].right,l,r,v);
update(x);
}
void add(int x,int l,int r,int v){
if (l<=tree[x].l&&r>=tree[x].r){
tree[x].max+=v;tree[x].add+=v;return;
}
int mid=(tree[x].l+tree[x].r)>>1;pushdown(x);
if (l<=mid) add(tree[x].left,l,r,v);
if (r>mid) add(tree[x].right,l,r,v);
update(x);
}
int query(int x,int l,int r){
if (l<=tree[x].l&&r>=tree[x].r) return tree[x].max;
int mid=(tree[x].l+tree[x].r)>>1;pushdown(x);int max1=-1;
if (l<=mid) max1=max(max1,query(tree[x].left,l,r));
if (r>mid) max1=max(max1,query(tree[x].right,l,r));
return max1;
}
int main(){
freopen("bzoj1984.in","r",stdin);
n=read();num=1;
for (int i=1;i<n;++i){
int x=read(),y=read(),z=read();
data[++num].y=y;data[num].z=z;data[num].next=h[x];h[x]=num;
data[++num].y=x;data[num].z=z;data[num].next=h[y];h[y]=num;
}dep[1]=1;num=0;
dfs1(1);dfs2(1,1);num=0;
build(root,1,n);char str1[10];
while (1){
scanf("%s",str1);
if (str1[1]=='t') break;
if (str1[1]=='o'){
int x=read(),y=read(),z=read();
while(tp[x]!=tp[y]){
if(dep[tp[x]]<dep[tp[y]]) swap(x,y);
change(root,id[tp[x]],id[x],z);
x=fa[tp[x]];
}
if (id[x]>id[y]) swap(x,y);
if (id[x]==id[y]) continue;
change(root,id[x]+1,id[y],z);
continue;
}
if (str1[1]=='d'){
int x=read(),y=read(),z=read();
while(tp[x]!=tp[y]){
if(dep[tp[x]]<dep[tp[y]]) swap(x,y);
add(root,id[tp[x]],id[x],z);
x=fa[tp[x]];
}
if (id[x]>id[y]) swap(x,y);
if (id[x]==id[y]) continue;
add(root,id[x]+1,id[y],z);
continue;
}
if (str1[1]=='h'){
int x=read(),z=read();
change(root,id[son1[x]],id[son1[x]],z);continue;
}
if (str1[1]=='a'){
int x=read(),y=read();int max1=-1;
while (tp[x]!=tp[y]){
if (dep[tp[x]]<dep[tp[y]]) swap(x,y);
max1=max(max1,query(root,id[tp[x]],id[x]));
x=fa[tp[x]];
}
if (id[x]>id[y]) swap(x,y);
if (id[x]==id[y]) {printf("%d\n",max1);continue;}
max1=max(max1,query(root,id[x]+1,id[y]));
printf("%d\n",max1);
}
}
return 0;
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120