zoukankan      html  css  js  c++  java
  • 【刷题】BZOJ 2959 长跑

    Description

    某校开展了同学们喜闻乐见的阳光长跑活动。为了能“为祖国健康工作五十年”,同学们纷纷离开寝室,离开教室,离开实验室,到操场参加3000米长跑运动。一时间操场上熙熙攘攘,摩肩接踵,盛况空前。

    为了让同学们更好地监督自己,学校推行了刷卡机制。

    学校中有n个地点,用1到n的整数表示,每个地点设有若干个刷卡机。

    有以下三类事件:

    1. 修建了一条连接A地点和B地点的跑道。

    2. A点的刷卡机台数变为了B。

    3. 进行了一次长跑。问一个同学从A出发,最后到达B最多可以刷卡多少次。

    具体的要求如下:

    当同学到达一个地点时,他可以在这里的每一台刷卡机上都刷卡。但每台刷卡机只能刷卡一次,即使多次到达同一地点也不能多次刷卡。

    为了安全起见,每条跑道都需要设定一个方向,这条跑道只能按照这个方向单向通行。最多的刷卡次数即为在任意设定跑道方向,按照任意路径从A地点到B地点能刷卡的最多次数。

    Input

    输入的第一行包含两个正整数n,m,表示地点的个数和操作的个数。

    第二行包含n个非负整数,其中第i个数为第个地点最开始刷卡机的台数。

    接下来有m行,每行包含三个非负整数P,A,B,P为事件类型,A,B为事件的两个参数。

    最初所有地点之间都没有跑道。

    每行相邻的两个数之间均用一个空格隔开。表示地点编号的数均在1到n之间,每个地点的刷卡机台数始终不超过10000,P=1,2,3。

    Output

    输出的行数等于第3类事件的个数,每行表示一个第3类事件。如果该情况下存在一种设定跑道方向的方案和路径的方案,可以到达,则输出最多可以刷卡的次数。如果A不能到达B,则输出-1。

    Sample Input

    9 31  
    10 20 30 40 50 60 70 80 90  
    3 1 2  
    1 1 3  
    1 1 2  
    1 8 9  
    1 2 4  
    1 2 5  
    1 4 6  
    1 4 7  
    3 1 8  
    3 8 8  
    1 8 9  
    3 8 8  
    3 7 5  
    3 7 3  
    1 4 1  
    3 7 5  
    3 7 3  
    1 5 7  
    3 6 5  
    3 3 6  
    1 2 4  
    1 5 5  
    3 3 6  
    2 8 180  
    3 8 8  
    2 9 190  
    3 9 9  
    2 5 150  
    3 3 6  
    2 1 210  
    3 3 6  
    

    Sample Output

    -1  
    -1  
    80  
    170  
    180  
    170  
    190  
    170  
    250  
    280  
    280  
    270  
    370  
    380  
    580  
    

    HINT

    数据规模及约定

    对于100%的数据,m<=5n,任意时刻,每个地点的刷卡机台数不超过10000。N<=1.5×10^5
      

    Solution

    LCT维护边双

    我们只要维护好超级点的权值(dfs的时候加上所有所有属于它的小点的权值),LCT就只要维护一个sum,询问的时候先拉链再查询就行了

    对于修改,我们只要在LCT外记录一个数组,要修改了,就把它所属的超级点先减掉以前的这个点的权值,再加上新的权值,pushup一下,就搞定了

    #include<bits/stdc++.h>
    #define ll long long
    #define db double
    #define ld long double
    const int MAXN=150000+10;
    int n,m,fa[MAXN],val[MAXN];
    #define lc(x) ch[(x)][0]
    #define rc(x) ch[(x)][1]
    struct LCT{
    	int ch[MAXN][2],fa[MAXN],val[MAXN],sum[MAXN],rev[MAXN],stack[MAXN],cnt,bel[MAXN];
    	inline void init()
    	{
    		for(register int i=1;i<=n;++i)bel[i]=i;
    	}
    	inline int find(int x)
    	{
    		return bel[x]==x?x:bel[x]=find(bel[x]);
    	}
    	inline bool nroot(int x)
    	{
    		return lc(find(fa[x]))==x||rc(find(fa[x]))==x;
    	}
    	inline void reverse(int x)
    	{
    		std::swap(lc(x),rc(x));
    		rev[x]^=1;
    	}
    	inline void dfs(int x,int rt)
    	{
    		if(lc(x))dfs(lc(x),rt);
    		if(rc(x))dfs(rc(x),rt);
    		if(x!=rt)bel[x]=rt,val[rt]+=val[x];
    	}
    	inline void pushup(int x)
    	{
    		sum[x]=sum[lc(x)]+sum[rc(x)]+val[x];
    	}
    	inline void pushdown(int x)
    	{
    		if(rev[x])
    		{
    			if(lc(x))reverse(lc(x));
    			if(rc(x))reverse(rc(x));
    			rev[x]=0;
    		}
    	}
    	inline void rotate(int x)
    	{
    		int f=find(fa[x]),p=find(fa[f]),c=(rc(f)==x);
    		if(nroot(f))ch[p][rc(p)==f]=x;
    		fa[ch[f][c]=ch[x][c^1]]=f;
    		fa[ch[x][c^1]=f]=x;
    		fa[x]=p;
    		pushup(f);
    		pushup(x);
    	}
    	inline void splay(int x)
    	{
    		cnt=0;
    		stack[++cnt]=x;
    		for(register int i=x;nroot(i);i=find(fa[i]))stack[++cnt]=find(fa[i]);
    		while(cnt)pushdown(stack[cnt--]);
    		for(register int y=find(fa[x]);nroot(x);rotate(x),y=find(fa[x]))
    			if(nroot(y))rotate((lc(y)==x)==(find(fa[y])==y)?y:x);
    		pushup(x);
    	}
    	inline void access(int x)
    	{
    		for(register int y=0;x;x=find(fa[y=x]))splay(x),rc(x)=y,pushup(x);
    	}
    	inline void makeroot(int x)
    	{
    		access(x);splay(x);reverse(x);
    	}
    	inline void split(int x,int y)
    	{
    		makeroot(x);access(y);splay(y);
    	}
    	inline void link(int x,int y)
    	{
    		makeroot(x);fa[x]=y;
    	}
    };
    LCT T;
    #undef lc
    #undef rc
    template<typename T> inline void read(T &x)
    {
    	T data=0,w=1;
    	char ch=0;
    	while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
    	if(ch=='-')w=-1,ch=getchar();
    	while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
    	x=data*w;
    }
    template<typename T> inline void write(T x,char c='')
    {
    	if(x<0)putchar('-'),x=-x;
    	if(x>9)write(x/10);
    	putchar(x%10+'0');
    	if(c!='')putchar(c);
    }
    template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
    template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
    template<typename T> inline T min(T x,T y){return x<y?x:y;}
    template<typename T> inline T max(T x,T y){return x>y?x:y;}
    inline int found(int x)
    {
    	if(fa[x]!=x)fa[x]=found(fa[x]);
    	return fa[x];
    }
    inline void add(int u,int v)
    {
    	u=T.find(u),v=T.find(v);
    	int x=found(u),y=found(v);
    	if(u==v)return ;
    	if(x!=y)
    	{
    		fa[x]=y;
    		T.link(u,v);
    		return ;
    	}
    	T.split(u,v);T.dfs(T.ch[v][0],v);
    }
    int main()
    {
    	read(n);read(m);
    	for(register int i=1;i<=n;++i)
    	{
    		read(val[i]),fa[i]=i;
    		T.val[i]=val[i];
    	}
    	T.init();
    	while(m--)
    	{
    		int opt,a,b;
    		read(opt);read(a);read(b);
    		if(opt==1)add(a,b);
    		if(opt==2)
    		{
    			int u=T.find(a);
    			T.access(u);T.splay(u);
    			T.val[u]-=val[a];
    			val[a]=b;
    			T.val[u]+=val[a];
    			T.pushup(u);
    		}		
    		if(opt==3)
    		{
    			if(found(a)!=found(b))
    			{
    				puts("-1");
    				continue;
    			}
    			a=T.find(a),b=T.find(b);
    			T.split(a,b);
    			write(T.sum[b],'
    ');
    		}
    	}
    	return 0;
    }
    
  • 相关阅读:
    《冒号课堂》目录 书名:冒号课堂——编程范式与OOP思想
    很好的WEB打印网页打印功能
    桌面搜索程序 Python
    面向对象保存爬虫数据 Python
    爬取微博热搜榜 李白之死 Python
    雪中悍刀行热播,来做一篇关于python的作业 爬虫与数据分析
    几个简单的例子 巩固Xpath语法 Python
    替换特殊字符 Python
    爬取酷狗榜单并可视化词云 Python
    Selenium尝试更改useragent 基于Python
  • 原文地址:https://www.cnblogs.com/hongyj/p/8721288.html
Copyright © 2011-2022 走看看