树链剖分【洛谷P1505】 [国家集训队]旅游

P1505 [国家集训队]旅游

题目描述

Ray 乐忠于旅游,这次他来到了T 城。T 城是一个水上城市,一共有 N 个景点,有些景点之间会用一座桥连接。为了方便游客到达每个景点但又为了节约成本,T 城的任意两个景点之间有且只有一条路径。换句话说, T 城中只有N − 1 座桥。

Ray 发现,有些桥上可以看到美丽的景色,让人心情愉悦,但有些桥狭窄泥泞,令人烦躁。于是,他给每座桥定义一个愉悦度w,也就是说,Ray 经过这座桥会增加w 的愉悦度,这或许是正的也可能是负的。有时,Ray 看待同一座桥的心情也会发生改变。

现在,Ray 想让你帮他计算从u 景点到v 景点能获得的总愉悦度。有时,他还想知道某段路上最美丽的桥所提供的最大愉悦度,或是某段路上最糟糕的一座桥提供的最低愉悦度。

输入输出格式

输入格式:

输入的第一行包含一个整数N,表示T 城中的景点个数。景点编号为 0...N − 1。

接下来N − 1 行,每行三个整数u、v 和w,表示有一条u 到v,使 Ray 愉悦度增加w 的桥。桥的编号为1...N − 1。|w| <= 1000。 输入的第N + 1 行包含一个整数M,表示Ray 的操作数目。

接下来有M 行,每行描述了一个操作,操作有如下五种形式:

  • C i w,表示Ray 对于经过第i 座桥的愉悦度变成了w。
  • N u v,表示Ray 对于经过景点u 到v 的路径上的每一座桥的愉悦度都变成原来的相反数。
  • SUM u v,表示询问从景点u 到v 所获得的总愉悦度。
  • MAX u v,表示询问从景点u 到v 的路径上的所有桥中某一座桥所提供的最大愉悦度。
  • MIN u v,表示询问从景点u 到v 的路径上的所有桥中某一座桥所提供的最小愉悦度。

测试数据保证,任意时刻,Ray 对于经过每一座桥的愉悦度的绝对值小于等于1000。

输出格式:

对于每一个询问(操作S、MAX 和MIN),输出答案。

和顾z一起刷树剖的日子。。。

(edge[i].to)打成了(edge[i].nxt)居然还能出样例。。。

code:

#include <iostream>
#include <cstdio>

#define ls(o) o<<1
#define rs(o) o<<1|1

#define int long long

using namespace std;

const int wx=5000017;

inline int read(){
	int sum=0,f=1; char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){sum=(sum<<1)+(sum<<3)+ch-'0';ch=getchar();}
	return sum*f;
}

int a[wx],tid[wx],dfn[wx],topf[wx],top[wx];
int head[wx],dep[wx],size[wx],son[wx],f[500017][23];
int num,n,m,tot;
char opt[wx];

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
struct val_tree{
	int l,r,sum,ma,mi,flag;
	#define sum(o) t[o].sum
	#define mi(o) t[o].mi
	#define ma(o) t[o].ma
	#define flag(o) t[o].flag
}t[wx*4];

void up(int o){
	sum(o)=sum(ls(o))+sum(rs(o));
	ma(o)=max(ma(ls(o)),ma(rs(o)));
	mi(o)=min(mi(ls(o)),mi(rs(o)));
}

void down(int o){
	if(flag(o)){
		sum(ls(o))=-sum(ls(o)); sum(rs(o))=-sum(rs(o));
		int tmp1=ma(ls(o)); int tmp2=ma(rs(o));
		ma(ls(o))=-mi(ls(o)); ma(rs(o))=-mi(rs(o));
		mi(ls(o))=-tmp1; mi(rs(o))=-tmp2;
		flag(ls(o))^=1; flag(rs(o))^=1;
		flag(o)=0;
	}
}
void build(int o,int l,int r){
	t[o].l=l; t[o].r=r;
	if(l==r){sum(o)=ma(o)=mi(o)=a[tid[l]];return ;}
	int mid=t[o].l+t[o].r>>1;
	if(l<=mid)build(ls(o),l,mid);
	if(r>mid)build(rs(o),mid+1,r);
	up(o);
}
void update_t(int o,int l,int r,int k){//单点覆盖 
	if(l<=t[o].l&&t[o].r<=r){
		sum(o)=k*(t[o].r-t[o].l+1);
		mi(o)=k; ma(o)=k;
		return ; 
	}
	down(o); 
	int mid=t[o].l+t[o].r>>1;
	if(l<=mid)update_t(ls(o),l,r,k);
	if(r>mid)update_t(rs(o),l,r,k);
	up(o);
}
void update_t_f(int o,int l,int r){//区间取反操作 
	if(l<=t[o].l&&t[o].r<=r){
		sum(o)=-sum(o);
		int tmp=ma(o); ma(o)=-mi(o);
		mi(o)=-tmp; flag(o)^=1;
		return ;
	}
	int mid=t[o].l+t[o].r>>1;
	down(o);
	if(l<=mid)update_t_f(ls(o),l,r);
	if(r>mid)update_t_f(rs(o),l,r);
	up(o);
} 
int query_sum_t(int o,int l,int r){//查询和
	if(l<=t[o].l&&t[o].r<=r){
		return sum(o);
	}
	down(o); int sum=0;
	int mid=t[o].l+t[o].r>>1;
	if(l<=mid)sum+=query_sum_t(ls(o),l,r);
	if(r>mid)sum+=query_sum_t(rs(o),l,r);
	return sum;
}
int query_ma_t(int o,int l,int r){//查询区间最大值 
	if(l<=t[o].l&&t[o].r<=r){
		return ma(o);
	}
	down(o); int ma=-2147483642;
	int mid=t[o].l+t[o].r>>1;
	if(l<=mid)ma=max(ma,query_ma_t(ls(o),l,r));
	if(r>mid)ma=max(ma,query_ma_t(rs(o),l,r));
	return ma;
}
int query_mi_t(int o,int l,int r){//查询区间最小值 
	if(l<=t[o].l&&t[o].r<=r){
		return mi(o);
	}
	down(o); int mi=0x3f3f3f3f;
	int mid=t[o].l+t[o].r>>1;
	if(l<=mid)mi=min(mi,query_mi_t(ls(o),l,r));
	if(r>mid)mi=min(mi,query_mi_t(rs(o),l,r));
	return mi;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
struct e{
	int nxt,to,dis;
}edge[wx*2];

void add(int from,int to,int dis){
	edge[++num].nxt=head[from];
	edge[num].to=to;
	edge[num].dis=dis;
	head[from]=num;
}

void first_dfs(int u,int fa){
	dep[u]=dep[fa]+1; size[u]=1;
	int maxson=-1;
	for(int i=head[u];i;i=edge[i].nxt){
		int v=edge[i].to;
		if(v==fa)continue;
		a[v]=edge[i].dis; //cout<<a[v]<<" xxoo"<<v<<endl;
		f[v][0]=u;
		first_dfs(v,u);
		size[u]+=size[v];
		if(maxson<size[v]){
			maxson=size[v];
			son[u]=v;
		}
	}
}

void second_dfs(int u,int topf){
	dfn[u]=++tot; top[u]=topf; tid[tot]=u;
	if(son[u])second_dfs(son[u],topf);
	for(int i=head[u];i;i=edge[i].nxt){
		int v=edge[i].to;
		if(dfn[v]||son[u]==v)continue;
		second_dfs(v,v);
	}
}

void pre(){
	for(int j=1;j<=21;j++)for(int i=1;i<=n;i++)f[i][j]=f[f[i][j-1]][j-1];
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


int LCA(int x,int y){
	if(dep[x]<dep[y])swap(x,y);
	for(int i=21;i>=0;i--)if(dep[f[x][i]]>=dep[y])x=f[x][i];
	if(x==y)return x;
	for(int i=21;i>=0;i--)if(f[x][i]!=f[y][i])x=f[x][i],y=f[y][i];
	return f[x][0];
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void update(int x,int y){
	int fx=top[x]; int fy=top[y];
	while(fx!=fy){
		if(dep[fx]>dep[fy]){
			update_t_f(1,dfn[fx],dfn[x]);
			x=f[fx][0];
		}
		else{
			update_t_f(1,dfn[fy],dfn[y]);
			y=f[fy][0];
		}
		fx=top[x]; fy=top[y];
	}
	if(dfn[x]>dfn[y])swap(x,y);
	update_t_f(1,dfn[x],dfn[y]);
}

int query_sum(int x,int y){
	int fx=top[x]; int fy=top[y]; int sum=0;
	while(fx!=fy){
		if(dep[fx]>dep[fy]){
			sum+=query_sum_t(1,dfn[fx],dfn[x]);
			x=f[fx][0];
		}
		else{
			sum+=query_sum_t(1,dfn[fy],dfn[y]);
			y=f[fy][0];
		}
		fx=top[x]; fy=top[y];
	}
	if(dfn[x]>dfn[y])swap(x,y);
	sum+=query_sum_t(1,dfn[x],dfn[y]);
	return sum;
}


int query_max(int x,int y){
	int fx=top[x]; int fy=top[y]; int maxx=-0x3f3f3f3f;
	while(fx!=fy){
		if(dep[fx]>dep[fy]){
			maxx=max(maxx,query_ma_t(1,dfn[fx],dfn[x]));
			x=f[fx][0];
		}
		else{
			maxx=max(maxx,query_ma_t(1,dfn[fy],dfn[y]));
			y=f[fy][0];
		}
		fx=top[x]; fy=top[y];
	}
	if(dfn[x]>dfn[y])swap(x,y);
	maxx=max(maxx,query_ma_t(1,dfn[x],dfn[y]));
	return maxx;
}


int query_min(int x,int y){
	int fx=top[x]; int fy=top[y]; int minn=0x3f3f3f3f;
	while(fx!=fy){
		if(dep[fx]>dep[fy]){
			minn=min(minn,query_mi_t(1,dfn[fx],dfn[x]));
			x=f[fx][0];
		}
		else{
			minn=min(minn,query_mi_t(1,dfn[fy],dfn[y]));
			y=f[fy][0];
		}
		fx=top[x]; fy=top[y];
	}
	if(dfn[x]>dfn[y])swap(x,y);
	minn=min(minn,query_mi_t(1,dfn[x],dfn[y]));
	return minn;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
signed main(){
	n=read();
	for(int i=1;i<n;i++){
		int x,y,z;
		x=read(); y=read(); z=read();
		x++; y++;
		add(x,y,z); add(y,x,z);
	}
	first_dfs(1,0); second_dfs(1,1); pre();
	build(1,1,n);
	m=read();
	for(int i=1;i<=m;i++){
		scanf("%s",opt+1);
		if(opt[1]=='C'){
			int x,y;
			x=read(); y=read();
			if(dfn[edge[x*2].to]>dfn[edge[x*2-1].to]){
				update_t(1,dfn[edge[x*2].to],dfn[edge[x*2].to],y);
			}
			else update_t(1,dfn[edge[x*2-1].to],dfn[edge[x*2-1].to],y);
		}
		else if(opt[1]=='N'){
			int x,y;
			x=read(); y=read();
			x++; y++;
			int lca=LCA(x,y);
			int tmp=query_sum_t(1,dfn[lca],dfn[lca]);
			update(x,y);
			update_t(1,dfn[lca],dfn[lca],tmp);
		}
		else if(opt[3]=='X'){
			int x,y;
			x=read();y=read();
			x++; y++;
			int lca=LCA(x,y);
			int tmp=query_sum_t(1,dfn[lca],dfn[lca]);
			update_t(1,dfn[lca],dfn[lca],-2147483642);
			printf("%lld
",query_max(x,y));
			update_t(1,dfn[lca],dfn[lca],tmp);
		}
		else if(opt[3]=='N'){
			int x,y;
			x=read(); y=read(); 
			x++; y++;
			int lca=LCA(x,y);
			int tmp=query_sum_t(1,dfn[lca],dfn[lca]);
			update_t(1,dfn[lca],dfn[lca],2147483642);
			printf("%lld
",query_min(x,y));
			update_t(1,dfn[lca],dfn[lca],tmp);
		}
		else if(opt[3]=='M'){
			int x,y;
			x=read(); y=read();
			x++; y++;
			int lca=LCA(x,y);
			int tmp=query_sum_t(1,dfn[lca],dfn[lca]);
			printf("%lld
",query_sum(x,y)-tmp);
		}
	}
	return 0;
}
原文地址:https://www.cnblogs.com/wangxiaodai/p/9797993.html