洛谷4315 月下“毛景树”(树链剖分)

传送门

【题目分析】

手残一时爽,代码调一天。。。。。。。。样例都是假的QAQ

旅游这道题类似,都是权值在路径上,所以将权值赋给深度较深的点,注意查询和更新时不能计算lca。

这道题相当于支持单点修改、区间覆盖、区间加、区间求最大值,常规操作。

【代码~】

#include<bits/stdc++.h>
using namespace std;
const int MAXN=1e5+10;
const int MAXM=2e5+10;

int n,m,cnt=1;
int head[MAXN],depth[MAXN],siz[MAXN],son[MAXN],fa[MAXN],top[MAXN];
int dfn[MAXN],ys[MAXN],tot;
int a[MAXN];
struct Tree{
	int l,r;
	int maxx,add;
	int cover;
}tr[MAXN<<2];
struct Edge{
	int nxt,to,w;
}edge[MAXM];

int Read(){
	int i=0,f=1;
	char c;
	for(c=getchar();(c>'9'||c<'0')&&c!='-';c=getchar());
	if(c=='-')
	  f=-1,c=getchar();
	for(;c>='0'&&c<='9';c=getchar())
	  i=(i<<3)+(i<<1)+c-'0';
	return i*f;
}

void add(int x,int y,int z){
	cnt++;
	edge[cnt].nxt=head[x];
	head[x]=cnt;
	edge[cnt].to=y;
	edge[cnt].w=z;
}

void dfs1(int u,int f){
	siz[u]=1;
	for(int i=head[u];i!=-1;i=edge[i].nxt){
		int v=edge[i].to;
		if(v==f)
		  continue;
		a[v]=edge[i].w;
		depth[v]=depth[u]+1,fa[v]=u;
		dfs1(v,u);
		siz[u]+=siz[v];
		if(siz[v]>siz[son[u]])
		  son[u]=v;
	}
}

void dfs2(int u,int tp){
	top[u]=tp;
	dfn[u]=++tot;
	ys[tot]=u;
	if(!son[u])
	  return ;
	dfs2(son[u],tp);
	for(int i=head[u];i!=-1;i=edge[i].nxt){
		int v=edge[i].to;
		if(v==fa[u]||v==son[u])
		  continue;
		dfs2(v,v);
	}
}

void push_up(int root){
	tr[root].maxx=max(tr[root<<1].maxx,tr[root<<1|1].maxx);
}

void push_cover(int root,int v){
	tr[root].cover=v;
	tr[root].add=0;
	tr[root].maxx=v;
}

void push_add(int root,int v){
	tr[root].add+=v;
	tr[root].maxx+=v;
}

void push_down(int root){
	if(tr[root].cover!=-1){
		push_cover(root<<1,tr[root].cover);
		push_cover(root<<1|1,tr[root].cover);
		tr[root].cover=-1;
	}
	if(tr[root].add){
		push_add(root<<1,tr[root].add);
		push_add(root<<1|1,tr[root].add);
		tr[root].add=0;
	}
}

void build(int root,int l,int r){
	tr[root].cover=-1;
	tr[root].l=l,tr[root].r=r;
	if(l==r){
		tr[root].maxx=a[ys[l]];
		return ;
	}
	int mid=l+r>>1;
	build(root<<1,l,mid);
	build(root<<1|1,mid+1,r);
	push_up(root);
}

void update_cover(int root,int l,int r,int L,int R,int key){
	if(l>R||r<L)
	  return ;
	if(L<=l&&r<=R){
		push_cover(root,key);
		return ;
	}
	push_down(root);
	int mid=l+r>>1;
	if(R<=mid)
	  update_cover(root<<1,l,mid,L,R,key);
	else{
		if(L>mid)
		  update_cover(root<<1|1,mid+1,r,L,R,key);
		else
		  update_cover(root<<1,l,mid,L,mid,key),update_cover(root<<1|1,mid+1,r,mid+1,R,key);
	}
	push_up(root);
}

void update_add(int root,int l,int r,int L,int R,int key){
	if(l>R||r<L)
	  return ;
	if(L<=l&&r<=R){
		push_add(root,key);
		return ;
	}
	push_down(root);
	int mid=l+r>>1;
	if(R<=mid)
	  update_add(root<<1,l,mid,L,R,key);
	else{
		if(L>mid)
		  update_add(root<<1|1,mid+1,r,L,R,key);
		else
		  update_add(root<<1,l,mid,L,mid,key),update_add(root<<1|1,mid+1,r,mid+1,R,key);
	}
	push_up(root);
}

int query(int root,int l,int r,int L,int R){
	if(l>R||r<L)
	  return 0;
	if(L<=l&&r<=R)
	  return tr[root].maxx;
	push_down(root);  
	int mid=l+r>>1;
	if(R<=mid)
	  return query(root<<1,l,mid,L,R);
	else{
		if(L>mid)
		  return query(root<<1|1,mid+1,r,L,R);
		else
		  return max(query(root<<1,l,mid,L,mid),query(root<<1|1,mid+1,r,mid+1,R));
	}
}

void updatepath_cover(int x,int y,int key){
	while(top[x]!=top[y]){
		if(depth[top[x]]<depth[top[y]])
		  swap(x,y);
		update_cover(1,1,n,dfn[top[x]],dfn[x],key);
		x=fa[top[x]];
	}
	if(depth[x]<depth[y])
	  swap(x,y);
	update_cover(1,1,n,dfn[y]+1,dfn[x],key);
}

void updatepath_add(int x,int y,int key){
	while(top[x]!=top[y]){
		if(depth[top[x]]<depth[top[y]])
		  swap(x,y);
		update_add(1,1,n,dfn[top[x]],dfn[x],key);
		x=fa[top[x]];
	}
	if(depth[x]<depth[y])
	  swap(x,y);
	update_add(1,1,n,dfn[y]+1,dfn[x],key);
}

int querypath(int x,int y){
	int ret=0;
	while(top[x]!=top[y]){
		if(depth[top[x]]<depth[top[y]])
		  swap(x,y);
		ret=max(ret,query(1,1,n,dfn[top[x]],dfn[x]));
		x=fa[top[x]];
	}
	if(depth[x]<depth[y])
	  swap(x,y);
	ret=max(ret,query(1,1,n,dfn[y]+1,dfn[x]));
	return ret;
}

int find(int x){
	return depth[edge[x<<1].to]>depth[edge[x<<1^1].to]?edge[x<<1].to:edge[x<<1^1].to;
}

int main(){
	memset(head,-1,sizeof(head));
	n=Read();
	for(int i=1;i<n;++i){
		int x=Read(),y=Read(),z=Read();
		add(x,y,z),add(y,x,z);
	}
	dfs1(1,-1);
	dfs2(1,1);
	build(1,1,n);
	char cz[10];
	scanf("%s",cz);
	while(cz[0]!='S'){
		int x=Read(),y=Read();
		if(cz[1]=='h'){
			update_cover(1,1,n,dfn[find(x)],dfn[find(x)],y);
		}
		if(cz[1]=='o'){
			int k=Read();
			updatepath_cover(x,y,k);
		}
		if(cz[0]=='A'){
			int k=Read();
			updatepath_add(x,y,k);
		}
		if(cz[0]=='M')
		  cout<<querypath(x,y)<<'\n';
		scanf("%s",cz);
	}
	return 0;
}
posted @ 2018-11-01 10:46  Ishtar~  阅读(146)  评论(0编辑  收藏  举报