社区讨论

萌新求助qwq真的不知道哪里写挂了

P4315月下“毛景树”参与者 6已保存回复 11

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
11 条
当前快照
1 份
快照标识符
@mi6zfscj
此快照首次捕获于
2025/11/20 13:20
4 个月前
此快照最后确认于
2025/11/20 15:46
4 个月前
查看原帖
CPP
#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_now1(int root,int v){
	tr[root].cover=v;
	tr[root].add=0;
	tr[root].maxx=v;
}

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

void push_down(int root){
	if(tr[root].cover!=-1){
		push_now1(root<<1,tr[root].cover);
		push_now2(root<<1|1,tr[root].cover);
		tr[root].cover=-1;
	}
	if(tr[root].add){
		push_now2(root<<1,tr[root].add);
		push_now2(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 update1(int root,int l,int r,int L,int R,int key){
	if(l>R||r<L)
	  return ;
	if(L<=l&&r<=R){
		push_now1(root,key);
		return ;
	}
	push_down(root);
	int mid=l+r>>1;
	if(R<=mid)
	  update1(root<<1,l,mid,L,R,key);
	else{
		if(L>mid)
		  update1(root<<1|1,mid+1,r,L,R,key);
		else
		  update1(root<<1,l,mid,L,mid,key),update1(root<<1|1,mid+1,r,mid+1,R,key);
	}
	push_up(root);
}

void update2(int root,int l,int r,int L,int R,int key){
	if(l>R||r<L)
	  return ;
	if(L<=l&&r<=R){
		push_now2(root,key);
		return ;
	}
	push_down(root);
	int mid=l+r>>1;
	if(R<=mid)
	  update2(root<<1,l,mid,L,R,key);
	else{
		if(L>mid)
		  update2(root<<1|1,mid+1,r,L,R,key);
		else
		  update2(root<<1,l,mid,L,mid,key),update2(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 updatepath1(int x,int y,int key){
	while(top[x]!=top[y]){
		if(depth[top[x]]<depth[top[y]])
		  swap(x,y);
		update1(1,1,n,dfn[top[x]],dfn[x],key);
		x=fa[top[x]];
	}
	if(depth[x]<depth[y])
	  swap(x,y);
	update1(1,1,n,dfn[y]+1,dfn[x],key);
}

void updatepath2(int x,int y,int key){
	while(top[x]!=top[y]){
		if(depth[top[x]]<depth[top[y]])
		  swap(x,y);
		update2(1,1,n,dfn[top[x]],dfn[x],key);
		x=fa[top[x]];
	}
	if(depth[x]<depth[y])
	  swap(x,y);
	update2(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;
}

回复

11 条回复,欢迎继续交流。

正在加载回复...