10分,A第十一个点,求调
查看原帖
10分,A第十一个点,求调
757101
junee楼主2024/1/8 17:19
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long LL;
const int N=1e5+10;
int h[N],e[N],ne[N],idx;
LL f[N];
int id[N],nw[N],cnt;
LL sz[N],top[N],dep[N],son[N],fa[N];
LL w[N];
void add(int a,int b,LL c){
	e[idx]=b,ne[idx]=h[a],f[idx]=c,h[a]=idx++;
}
void dfs_son(int u,int father,int d){
	sz[u]=1,fa[u]=father,dep[u]=d;
	for(int i=h[u];~i;i=ne[i]){
		int j=e[i];
		if(j==father)continue;
		w[j]=f[i];
		dfs_son(j,u,d+1);
		sz[u]+=sz[j];
		if(sz[son[u]]<sz[j])son[u]=j;
	} 
}
void dfs(int u,int t){
	id[u]=++cnt,nw[cnt]=w[u],top[u]=t;
	if(!son[u])return;
	dfs(son[u],t);
	for(int i=h[u];~i;i=ne[i]){
		int j=e[i];
		if(j==fa[u]||j==son[u])continue;
		dfs(j,j);
	}
}
struct node{
	int l,r;
	LL m,add,tag; 
}tr[N*4];
void pushup(int u){
	tr[u].m=max(tr[u<<1].m,tr[u<<1|1].m);
}
void pushdown(int u){
	if(tr[u].tag!=-1){
		tr[u<<1|1].tag=tr[u<<1].tag=tr[u].tag;
		tr[u<<1].m=tr[u<<1|1].m=tr[u].tag;
		tr[u].tag=-1;
		tr[u].add=0;
	}
	if(tr[u].add){
		tr[u<<1].add+=tr[u].add;
		tr[u<<1|1].add+=tr[u].add;
		tr[u<<1].m+=tr[u].add;
		tr[u<<1|1].m+=tr[u].add;
		tr[u].add=0;
	}
}
void build(int u,int l,int r){
	tr[u]={l,r,nw[r],0,-1};
	if(l==r)return;
	int mid=(l+r)>>1;
	build(u<<1,l,mid);
	build(u<<1|1,mid+1,r);
	pushup(u);
}
void modify(int u,int l,int r,LL k){
	if(tr[u].l>=l&&tr[u].r<=r){
		tr[u].m+=k;
		tr[u].add+=k;
		return;
	}
	pushdown(u);
	int mid=(tr[u].l+tr[u].r)>>1;
	if(l<=mid)modify(u<<1,l,r,k);
	if(r>mid)modify(u<<1|1,l,r,k);
	pushup(u);
}
void cover(int u,int l,int r,LL k){
	if(tr[u].l>=l&&tr[u].r<=r){
		tr[u].tag=k;
		tr[u].m=k;
		tr[u].add=0;
		return;
	}
	pushdown(u);
	int mid=(tr[u].l+tr[u].r)>>1;
	if(l<=mid)cover(u<<1,l,r,k);
	if(r>mid)cover(u<<1|1,l,r,k);
	pushup(u);
}
LL query(int u,int l,int r){
	if(tr[u].l>=l&&tr[u].r<=r){
		return tr[u].m;
	}
	pushdown(u);
	int mid=(tr[u].l+tr[u].r)>>1;
	LL res=0;
	if(l<=mid)res=max(res,query(u<<1,l,r));
	if(r>mid)res=max(res,query(u<<1|1,l,r));
	return res;
}
void modify_path(int u,int v,int k){
	while(top[u]!=top[v]){
		if(dep[top[u]]<dep[top[v]])swap(u,v);
		modify(1,id[top[u]],id[u],k);
		u=fa[top[u]];
	}
	if(u==v)return;
	if(dep[u]<dep[v])swap(u,v);
	modify(1,id[son[v]],id[u],k);
}
LL query_path(int u,int v){
	LL res=-1;
	while(top[u]!=top[v]){
		if(dep[top[u]]<dep[top[v]])swap(u,v);
		res=max(res,query(1,id[top[u]],id[u]));
		u=fa[top[u]];
	}
	if(u==v)return res;
	if(dep[u]<dep[v])swap(u,v);
	res=max(res,query(1,id[son[v]],id[u]));
	return res;
}
void cover_path(int u,int v,int k){
	while(top[u]!=top[v]){
		if(dep[top[u]]<dep[top[v]])swap(u,v);
		cover(1,id[top[u]],id[u],k);
		u=fa[top[u]];
	}
	if(u==v)return;
	if(dep[u]<dep[v])swap(u,v);
	cover(1,id[son[v]],id[u],k);
}
int main(){
	//freopen("1.in","r",stdin);
	memset(h,-1,sizeof h);
	int n;
	cin>>n;
	for(int i=1;i<n;i++){
		int a,b,c;
		cin>>a>>b>>c;
		add(a,b,c);
		add(b,a,c);
	}
	dfs_son(1,-1,1);
	dfs(1,1);
	build(1,1,n);
	while(1){
		string a;
		int u,v,x,k;
		cin>>a;
		if(a=="Change"){
			cin>>x>>k;
			u=e[x*2-1],v=e[(x*2-1)^1];
			if(fa[u]!=v)swap(u,v);
			cover_path(v,u,k);
		}
		if(a=="Cover"){
			cin>>u>>v>>k;
			cover_path(u,v,k);
		}
		if(a=="Add"){
			cin>>u>>v>>k;
			modify_path(u,v,k);
		}
		if(a=="Max"){
			cin>>u>>v;
			cout<<query_path(u,v)<<endl;			
		}
		if(a=="Stop"){
			return 0;
		}
	}
	return 0;
}
2024/1/8 17:19
加载中...