zoukankan      html  css  js  c++  java
  • 【BZOJ2157】旅游 树链剖分+线段树

    【BZOJ2157】旅游

    Description

    Ray 乐忠于旅游,这次他来到了T 城。T 城是一个水上城市,一共有 N 个景点,有些景点之间会用一座桥连接。为了方便游客到达每个景点但又为了节约成本,T 城的任意两个景点之间有且只有一条路径。换句话说, T 城中只有N − 1 座桥。Ray 发现,有些桥上可以看到美丽的景色,让人心情愉悦,但有些桥狭窄泥泞,令人烦躁。于是,他给每座桥定义一个愉悦度w,也就是说,Ray 经过这座桥会增加w 的愉悦度,这或许是正的也可能是负的。有时,Ray 看待同一座桥的心情也会发生改变。现在,Ray 想让你帮他计算从u 景点到v 景点能获得的总愉悦度。有时,他还想知道某段路上最美丽的桥所提供的最大愉悦度,或是某段路上最糟糕的一座桥提供的最低愉悦度。

    Input

    输入的第一行包含一个整数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。

    Output

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

    Sample Input

    3
    0 1 1
    1 2 2
    8
    SUM 0 2
    MAX 0 2
    N 0 1
    SUM 0 2
    MIN 0 2
    C 1 3
    SUM 0 2
    MAX 0 2

    Sample Output

    3
    2
    1
    -1
    5
    3

    HINT

    一共有10 个数据,对于第i (1 <= i <= 10) 个数据, N = M = i * 2000。

    题解:又是树剖模板题,已经不知道该注意什么了,仍然会因为没写pushdown而狂WA不止~

    搬运个数据生成器吧~  //from GXZlegend

    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <ctime>
    #include <iostream>
    #include <string>
    #include <algorithm>
     
    using namespace std;
     
    int n , m;
     
    void build_tree() {
    	for( int i = 1 ; i < n ; ++i )
    		printf( "%d %d %d
    " , i , rand() % i , rand() % 11 * ( rand() & 1 ? -1 : 1 ) );
    }
     
    string s[ 5 ] = { "C" , "N" , "SUM" , "MAX" , "MIN" };
     
    void build_query() {
    	while( m-- ) {
    		int op = rand() % 5;
    		cout << s[ op ] << ' ';
    		if( ! op ) {
    			printf( "%d %d" , rand() % ( n - 1 ) + 1 , rand() % 11 * ( rand() & 1 ? -1 : 1 ) );
    		} else {
    			int u = rand() % n , v = rand() % n;
    			while( v == u ) v =rand() % n;
    			printf( "%d %d" , u , v );
    		}
    		putchar( '
    ' );
    	}
    }
     
    int main() {
    	
    	srand( time( NULL ) );
    	n =1000 , m = 2000;
    	cout << n << ' ' << "
    ";
    	build_tree();
    	cout << m << "
    ";
    	build_query();
    	
    	return 0;
    }
    
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <queue>
    #define lson x<<1
    #define rson x<<1|1
    using namespace std;
    const int maxn=200010;
    int head[maxn],next[maxn<<1],to[maxn<<1],val[maxn<<1],v[maxn<<1],p[maxn];
    int dep[maxn],siz[maxn],son[maxn],top[maxn],fa[maxn],bel[maxn];
    int s[maxn],rs[maxn],sm[maxn],sn[maxn];
    int n,m,cnt,tot;
    char str[5];
    int rd()
    {
    	int ret=0,flag=1;	char gc=getchar();
    	while(gc<'0'||gc>'9'){if(gc=='-')	flag=-flag;	gc=getchar();}
    	while(gc>='0'&&gc<='9')	ret=ret*10+gc-'0',gc=getchar();
    	return ret*flag;
    }
    void add(int a,int b,int c)
    {
    	to[cnt]=b,val[cnt]=c,next[cnt]=head[a],head[a]=cnt++;
    }
    void pushup(int x)
    {
    	s[x]=s[lson]+s[rson],sm[x]=max(sm[lson],sm[rson]),sn[x]=min(sn[lson],sn[rson]);
    }
    void pushdown(int x)
    {
    	if(rs[x])
    	{
    		sm[lson]=-sm[lson],sm[rson]=-sm[rson],sn[lson]=-sn[lson],sn[rson]=-sn[rson];
    		swap(sm[lson],sn[lson]),swap(sm[rson],sn[rson]);
    		s[lson]=-s[lson],s[rson]=-s[rson],rs[lson]^=1,rs[rson]^=1,rs[x]=0;
    	}
    }
    void dfs1(int x)
    {
    	siz[x]=1;
    	for(int i=head[x];i!=-1;i=next[i])
    	{
    		if(to[i]!=fa[x])
    		{
    			fa[to[i]]=x,dep[to[i]]=dep[x]+1,v[to[i]]=val[i],bel[i>>1]=to[i];
    			dfs1(to[i]);
    			siz[x]+=siz[to[i]];
    			if(siz[to[i]]>siz[son[x]])	son[x]=to[i];
    		}
    	}
    }
    void updata(int l,int r,int x,int a,int b)
    {
    	if(l==r)
    	{
    		s[x]=sm[x]=sn[x]=b;
    		return ;
    	}
    	pushdown(x);
    	int mid=l+r>>1;
    	if(a<=mid)	updata(l,mid,lson,a,b);
    	else	updata(mid+1,r,rson,a,b);
    	pushup(x);
    }
    void dfs2(int x,int tp)
    {
    	top[x]=tp,p[x]=++tot;
    	updata(1,n,1,p[x],v[x]);
    	if(son[x])	dfs2(son[x],tp);
    	for(int i=head[x];i!=-1;i=next[i])	if(to[i]!=fa[x]&&to[i]!=son[x])	dfs2(to[i],to[i]);
    }
    void uprs(int l,int r,int x,int a,int b)
    {
    	if(a<=l&&r<=b)
    	{
    		sm[x]=-sm[x],sn[x]=-sn[x],swap(sm[x],sn[x]),s[x]=-s[x],rs[x]^=1;
    		return ;
    	}
    	pushdown(x);
    	int mid=l+r>>1;
    	if(a<=mid)	uprs(l,mid,lson,a,b);
    	if(b>mid)	uprs(mid+1,r,rson,a,b);
    	pushup(x);
    }
    int qs(int l,int r,int x,int a,int b)
    {
    	if(a<=l&&r<=b)	return s[x];
    	pushdown(x);
    	int mid=l+r>>1;
    	if(b<=mid)	return qs(l,mid,lson,a,b);
    	if(a>mid)	return qs(mid+1,r,rson,a,b);
    	return qs(l,mid,lson,a,b)+qs(mid+1,r,rson,a,b);
    }
    int qm(int l,int r,int x,int a,int b)
    {
    	if(a<=l&&r<=b)	return sm[x];
    	pushdown(x);
    	int mid=l+r>>1;
    	if(b<=mid)	return qm(l,mid,lson,a,b);
    	if(a>mid)	return qm(mid+1,r,rson,a,b);
    	return max(qm(l,mid,lson,a,b),qm(mid+1,r,rson,a,b));
    }
    int qn(int l,int r,int x,int a,int b)
    {
    	if(a<=l&&r<=b)	return sn[x];
    	pushdown(x);
    	int mid=l+r>>1;
    	if(b<=mid)	return qn(l,mid,lson,a,b);
    	if(a>mid)	return qn(mid+1,r,rson,a,b);
    	return min(qn(l,mid,lson,a,b),qn(mid+1,r,rson,a,b));
    }
    void N(int x,int y)
    {
    	while(top[x]!=top[y])
    	{
    		if(dep[top[x]]<dep[top[y]])	swap(x,y);
    		uprs(1,n,1,p[top[x]],p[x]),x=fa[top[x]];
    	}
    	if(x==y)	return ;
    	if(dep[x]>dep[y])	swap(x,y);
    	uprs(1,n,1,p[x]+1,p[y]);
    }
    int S(int x,int y)
    {
    	int ret=0;
    	while(top[x]!=top[y])
    	{
    		if(dep[top[x]]<dep[top[y]])	swap(x,y);
    		ret+=qs(1,n,1,p[top[x]],p[x]),x=fa[top[x]];
    	}
    	if(x==y)	return ret;
    	if(dep[x]>dep[y])	swap(x,y);
    	return ret+qs(1,n,1,p[x]+1,p[y]);
    }
    int MAX(int x,int y)
    {
    	int ret=-(1<<30);
    	while(top[x]!=top[y])
    	{
    		if(dep[top[x]]<dep[top[y]])	swap(x,y);
    		ret=max(ret,qm(1,n,1,p[top[x]],p[x])),x=fa[top[x]];
    	}
    	if(x==y)	return ret;
    	if(dep[x]>dep[y])	swap(x,y);
    	return max(ret,qm(1,n,1,p[x]+1,p[y]));
    }
    int MIN(int x,int y)
    {
    	int ret=1<<30;
    	while(top[x]!=top[y])
    	{
    		if(dep[top[x]]<dep[top[y]])	swap(x,y);
    		ret=min(ret,qn(1,n,1,p[top[x]],p[x])),x=fa[top[x]];
    	}
    	if(x==y)	return ret;
    	if(dep[x]>dep[y])	swap(x,y);
    	return min(ret,qn(1,n,1,p[x]+1,p[y]));
    }
    int main()
    {
    	memset(head,-1,sizeof(head));
    	memset(sm,0x80,sizeof(sm));
    	memset(sn,0x3f,sizeof(sn));
    	n=rd();
    	int i,a,b,c;
    	for(i=1;i<n;i++)
    	{
    		a=rd()+1,b=rd()+1,c=rd();
    		add(a,b,c),add(b,a,c);
    	}
    	dep[1]=1;
    	dfs1(1),dfs2(1,1);
    	m=rd();
    	for(i=1;i<=m;i++)
    	{
    		scanf("%s",str);
    		a=rd(),b=rd();
    		if(str[0]=='C')	updata(1,n,1,p[bel[a-1]],b);
    		if(str[0]=='N')	N(a+1,b+1);
    		if(str[0]=='S')	printf("%d
    ",S(a+1,b+1));
    		if(str[0]=='M'&&str[1]=='A')	printf("%d
    ",MAX(a+1,b+1));
    		if(str[0]=='M'&&str[1]=='I')	printf("%d
    ",MIN(a+1,b+1));
    	}
    	return 0;
    }
  • 相关阅读:
    微软老将Philip Su的离职信:回首12年职场生涯的心得和随笔[转]
    <Programming Ruby 1.9 The Pragmatic Programmer>读书笔记
    Ruby:Update value on specific row but keep the headers
    解决ImportError: cannot import name webdriver
    Ruby几个相关目录
    《发财日记》处处都是名言警句
    软件研发管理最佳实践(20121020 深圳)
    中国过程改进年会会前培训:让敏捷落地! 软件研发管理最佳实践(2012530 北京)
    展示你的才华,成就你的事业!—— 疯狂讲师
    网络直播课程 体验极限编程(XP)
  • 原文地址:https://www.cnblogs.com/CQzhangyu/p/6636412.html
Copyright © 2011-2022 走看看