【BZOJ2157】旅游 裸树链剖分

#include <stdio.h>
int main()
{
	puts("转载请注明出处[vmurder]谢谢");
	puts("网址:blog.csdn.net/vmurder/article/details/43965845");
}


重写大发好!!!!!

****什么题解都没有,水题一道,

挂了就去调,调不过就去重写。


代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 20100
#define ls (note<<1)
#define rs (note<<1|1)
#define inf 0x3f3f3f3f
using namespace std;
struct KSD
{
	int v,len,next;
}e[N<<1];
int head[N],cnt;
inline void add(int u,int v,int len)
{
	e[++cnt].v=v;
	e[cnt].len=len;
	e[cnt].next=head[u];
	head[u]=cnt;
}
int dep[N],pos[N],road[N],crs[N];
int fa[N],top[N],son[N];
int dfs1(int x,int p)
{
	int i,v,size=1,huge=0;
	fa[x]=p,dep[x]=dep[p]+1;
	for(i=head[x];i;i=e[i].next)
	{
		v=e[i].v;
		if(v==p)continue ;
		int temp=dfs1(v,x);size+=temp;
		if(huge<temp)son[x]=v,huge=temp;
		crs[i>>1]=v;
	}
	return size;
}
void dfs2(int x,int p)
{
	int i,v;
	top[x]=p,pos[x]=++cnt;
	if(son[x])dfs2(son[x],p);
	for(i=head[x];i;i=e[i].next)
	{
		v=e[i].v;
		if(v==son[x])road[pos[son[x]]]=e[i].len;
		if(v==son[x]||v==fa[x])continue ;
		road[cnt+1]=e[i].len;
		dfs2(v,v);
	}
}
struct Segment_Tree
{
	int l,r,sum;
	int mx,mi,c;
	bool f,rev;
}s[N<<2];
inline void pushup(int note)
{
	s[note].mx=max(s[ls].mx,s[rs].mx);
	s[note].mi=min(s[ls].mi,s[rs].mi);
	s[note].sum=s[ls].sum+s[rs].sum;
}
inline void pushdown(int note) // 覆盖的优先级高
{
	if(s[note].f)
	{
		s[note].rev=s[note].f=0;
		s[note].mx=s[note].mi=s[note].c;
		s[note].sum=(s[note].r-s[note].l+1)*s[note].c;

		if(s[note].l<s[note].r)
		{
			s[ls].c=s[rs].c=s[note].c;
			s[ls].f=s[rs].f=1;
			s[ls].rev=s[rs].rev=0;
		}
	}
	else if(s[note].rev)
	{
		s[note].rev=0;
		swap(s[note].mx,s[note].mi);
		s[note].mx*=(-1),s[note].mi*=(-1);
		s[note].sum*=(-1);

		if(s[note].l<s[note].r)
		{
			s[ls].c*=(-1),s[rs].c*=(-1);
			s[ls].rev^=1,s[rs].rev^=1;
		}
	}
}
void build(int note,int l,int r)
{
	s[note].l=l,s[note].r=r;
	if(l==r)
	{
		s[note].mx=s[note].mi=s[note].sum=road[l];
		return ;
	}
	int mid=l+r>>1;
	build(ls,l,mid);
	build(rs,mid+1,r);
	pushup(note);
}
void cover(int note,int l,int r,int x)
{
	if(s[note].l==l&&r==s[note].r)
	{
		s[note].c=x,s[note].f=1;
		pushdown(note);
		return ;
	}
	pushdown(note);
	int mid=s[note].l+s[note].r>>1;
	if(r<=mid)cover(ls,l,r,x),pushdown(rs);
	else if(l>mid)cover(rs,l,r,x),pushdown(ls);
	else cover(ls,l,mid,x),cover(rs,mid+1,r,x);
	pushup(note);
}
void rever(int note,int l,int r)
{
	if(s[note].l==l&&r==s[note].r)
	{
		s[note].c*=(-1),s[note].rev^=1;
		pushdown(note);
		return ;
	}
	pushdown(note);
	int mid=s[note].l+s[note].r>>1;
	if(r<=mid)rever(ls,l,r),pushdown(rs);
	else if(l>mid)rever(rs,l,r),pushdown(ls);
	else rever(ls,l,mid),rever(rs,mid+1,r);
	pushup(note);
}
int query(int note,int l,int r,int f) // 0sum 1max 2min
{
	pushdown(note);
	if(s[note].l==l&&r==s[note].r)
	{
		if(f==0)return s[note].sum;
		else if(f==1)return s[note].mx;
		else return s[note].mi;
	}
	int mid=s[note].l+s[note].r>>1;
	if(r<=mid)return query(ls,l,r,f);
	else if(l>mid)return query(rs,l,r,f);
	else {
		int a=query(ls,l,mid,f);
		int b=query(rs,mid+1,r,f);
		if(f==0)return a+b;
		else if(f==1)return max(a,b);
		else return min(a,b);
	}
}
inline void Cover(int a,int b){cover(1,pos[crs[a]],pos[crs[a]],b);}
inline void Rever(int a,int b)
{
	for(int A=top[a],B=top[b];A!=B;a=fa[A],A=top[a])
	{
		if(dep[A]<dep[B])swap(A,B),swap(a,b);
		rever(1,pos[A],pos[a]);
	}
	if(dep[a]<dep[b])swap(a,b);
	if(a!=b)rever(1,pos[b]+1,pos[a]);
}
inline int Query(int a,int b,int f)
{
	int ans;
	if(f==0)ans=0;
	else if(f==1)ans=-inf;
	else ans=inf;
	for(int A=top[a],B=top[b];A!=B;a=fa[A],A=top[a])
	{
		if(dep[A]<dep[B])swap(A,B),swap(a,b);
		int temp=query(1,pos[A],pos[a],f);
		if(f==0)ans+=temp;
		else if(f==1)ans=max(ans,temp);
		else ans=min(ans,temp);
	}
	if(dep[a]<dep[b])swap(a,b);
	if(a!=b)
	{
		int temp=query(1,pos[b]+1,pos[a],f);
		if(f==0)ans+=temp;
		else if(f==1)ans=max(ans,temp);
		else ans=min(ans,temp);
	}
	return ans;
}
int main()
{
	freopen("test.in","r",stdin);
//	freopen("test.out","w",stdout);

	int i,j,k;
	int a,b,c;
	int n,m;

	scanf("%d",&n);
	for(cnt=i=1;i<n;i++)
	{
		scanf("%d%d%d",&a,&b,&c);
		add(a+1,b+1,c),add(b+1,a+1,c);
	}
	cnt=0,dfs1(1,1),dfs2(1,1);
	build(1,1,n);

	char src[5];
	scanf("%d",&m);
	while(m--)
	{
		scanf("%s%d%d",src,&a,&b);
		if(src[0]=='C')Cover(a,b);
		else if(src[0]=='N')Rever(a+1,b+1);
		else if(src[2]=='M')printf("%d\n",Query(a+1,b+1,0));
		else if(src[2]=='X')printf("%d\n",Query(a+1,b+1,1));
		else if(src[2]=='N')printf("%d\n",Query(a+1,b+1,2));
	}
	return 0;
}


你可能感兴趣的:(旅游,树链剖分,裸题,BZOJ2157)