zoukankan      html  css  js  c++  java
  • [BZOJ4736]温暖会指引我们前行

    BZOJ(BZOJ上的是什么鬼。。。)
    UOJ
    任务描述
    虽然小R住的宿舍楼早已来了暖气,但是由于某些原因,宿舍楼中的某些窗户仍然开着(例如厕所的窗户),这就使得宿舍楼中有一些路上的温度还是很低。
    小R的宿舍楼中有n个地点和一些路,一条路连接了两个地点,小R可以通过这条路从其中任意一个地点到达另外一个地点。但在刚开始,小R还不熟悉宿舍楼中的任何一条路,所以他会慢慢地发现这些路,他在发现一条路时还会知道这条路的温度和长度。每条路的温度都是互不相同的。
    小R需要在宿舍楼中活动,每次他都需要从一个地点到达另一个地点。小R希望每次活动时经过一条最温暖的路径,最温暖的路径的定义为,将路径上各条路的温度从小到大排序后字典序最大。即温度最低的路温度尽量高,在满足该条件的情况下,温度第二低的路温度尽量高,以此类推。小R不会经过重复的路。由于每条路的温度互不相同,因此只存在一条最温暖的路径。
    对于小R的每次活动,你需要求出小R需要走过的路径总长度。如果小R通过当前发现的路不能完成这次活动,则输出 −1。
    注意本题中的字典序与传统意义上的字典序定义有所不同,对于两个序列a,b(a≠b),若a是b的前缀则a的字典序较大,同时可以推出空串的字典序最大。
    输入格式
    第一行两个正整数 n,m。表示小R的宿舍楼中有 n 个地点,共发生了 m 个事件。
    接下来 m 行,每行描述一个事件,事件分为三类。

    find id u v t l 表示小R发现了一条连接u和v之间的路,编号为id。相同id的边只会出现一次。
    
    move u v 表示小R要从u到达v,你需要计算出最温暖的路径的长度 ,若不能从u到达v,则输出−1。
    
    change id l 表示从u到v这条边的长度变为了l(保证在当前时间点这条边存在)。
    

    输出格式

    对于每个询问,输出一行整数,表示最温暖的路径长度。
    样例一
    input

    8 19
    find 0 0 2 7 2
    find 1 2 4 4 4
    find 2 4 6 10 1
    find 3 6 7 8 6
    move 2 7
    move 1 6
    find 4 2 5 3 4
    move 0 5
    change 0 12
    find 5 4 5 5 10
    find 6 2 3 6 9
    move 3 5
    find 7 0 1 12 1
    move 1 6
    find 8 1 7 11 100
    move 1 6
    move 3 7
    move 5 6
    move 2 2
    

    output

    11
    -1
    6
    23
    18
    106
    122
    11
    0
    

    样例二
    input

    15 45
    find 0 1 0 8 5987
    find 1 2 0 14 5455
    find 2 3 0 27 8830
    find 3 4 3 42 7688
    find 4 5 0 25 1756
    find 5 6 5 35 1550
    find 6 7 4 43 9440
    move 3 9
    change 2 9113
    move 10 13
    move 3 3
    move 11 10
    find 7 8 7 6 7347
    find 8 9 8 26 8935
    move 8 4
    change 3 4466
    find 9 10 9 28 8560
    move 6 5
    find 10 11 10 31 6205
    change 9 9228
    find 11 12 10 23 948
    find 12 13 12 45 5945
    move 0 9
    move 2 5
    change 2 6118
    find 13 14 13 12 6906
    move 4 1
    change 2 504
    find 14 4 2 22 9796
    move 10 7
    move 1 14
    move 13 3
    find 15 12 9 39 8985
    find 16 9 8 17 3710
    change 1 5370
    find 17 1 0 36 4669
    find 18 7 6 37 8087
    move 9 0
    find 19 14 9 33 8234
    find 20 0 4 24 5209
    change 1 4883
    find 21 6 3 9 2461
    find 22 5 2 19 4291
    change 1 7219
    change 6 4846
    

    output

    -1
    -1
    0
    -1
    16787
    1550
    39301
    7211
    16571
    25510
    59706
    46309
    30692
    

    数据范围是n<=10w,m<=30w

    题解

    这不就是LCT傻逼题吗?
    题面字典序的哔哔一大堆,其实就是说要你沿着最大生成树走。然后就LCT维护个最大生成树就好了。
    这种题要一遍过样例然后一遍AC。

    code

    #include<cstdio>
    #include<algorithm>
    using namespace std;
    const int N = 400005;
    int n,m,fa[N],ls[N],rs[N],rev[N],tem[N],mn[N],val[N],sum[N],Stack[N],top,u[N],v[N];
    char s[N];
    int gi()
    {
    	int x=0,w=1;char ch=getchar();
    	while ((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
    	if (ch=='-') w=0,ch=getchar();
    	while (ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
    	return w?x:-x;
    }
    bool isroot(int x){return ls[fa[x]]!=x&&rs[fa[x]]!=x;}
    void pushdown(int x){if (!rev[x]) return;swap(ls[x],rs[x]);rev[ls[x]]^=1;rev[rs[x]]^=1;rev[x]=0;}
    void pushup(int x)
    {
    	sum[x]=sum[ls[x]]+sum[rs[x]]+val[x];mn[x]=x;
    	if (ls[x]&&tem[mn[ls[x]]]<tem[mn[x]]) mn[x]=mn[ls[x]];
    	if (rs[x]&&tem[mn[rs[x]]]<tem[mn[x]]) mn[x]=mn[rs[x]];
    }
    void R_rotate(int x)
    {
    	int y=fa[x],z=fa[y];
    	ls[y]=rs[x];
    	if (rs[x]) fa[rs[x]]=y;
    	fa[x]=z;
    	if (!isroot(y)) if (y==ls[z]) ls[z]=x;else rs[z]=x;
    	rs[x]=y;fa[y]=x;
    	pushup(y);
    }
    void L_rotate(int x)
    {
    	int y=fa[x],z=fa[y];
    	rs[y]=ls[x];
    	if (ls[x]) fa[ls[x]]=y;
    	fa[x]=z;
    	if (!isroot(y)) if (y==ls[z]) ls[z]=x;else rs[z]=x;
    	ls[x]=y;fa[y]=x;
    	pushup(y);
    }
    void splay(int x)
    {
    	Stack[top=1]=x;
    	for (int i=x;!isroot(i);i=fa[i])
    		Stack[++top]=fa[i];
    	while (top) pushdown(Stack[top--]);
    	while (!isroot(x))
    	{
    		int y=fa[x],z=fa[y];
    		if (isroot(y))
    			if (x==ls[y]) R_rotate(x);
    			else L_rotate(x);
    		else
    			if (y==ls[z])
    				if (x==ls[y]) R_rotate(y),R_rotate(x);
    				else L_rotate(x),R_rotate(x);
    			else
    				if (x==ls[y]) R_rotate(x),L_rotate(x);
    				else L_rotate(y),L_rotate(x);
    	}
    	pushup(x);
    }
    void access(int x){for (int y=0;x;y=x,x=fa[x]) splay(x),rs[x]=y,pushup(x);}
    void makeroot(int x){access(x);splay(x);rev[x]^=1;}
    int findroot(int x){access(x);splay(x);while (ls[x]) x=ls[x];return x;}
    void split(int x,int y){makeroot(x);access(y);splay(y);}
    void link(int x,int y){makeroot(x);fa[x]=y;}
    void cut(int x,int y){split(x,y);fa[x]=ls[y]=0;}
    int main()
    {
    	n=gi();m=gi();
    	for (int i=1;i<=n;i++) tem[i]=2e9;
    	while (m--)
    	{
    		scanf("%s",s);
    		if (s[0]=='f')
    		{
    			int id=gi()+1+n;
    			u[id]=gi()+1;v[id]=gi()+1;tem[id]=gi();val[id]=gi();
    			if (findroot(u[id])^findroot(v[id]))
    			{
    				link(u[id],id);link(v[id],id);
    			}
    			else
    			{
    				split(u[id],v[id]);int pos=mn[v[id]];
    				if (tem[pos]<tem[id])
    				{
    					cut(u[pos],pos);cut(v[pos],pos);
    					link(u[id],id);link(v[id],id);
    				}
    			}
    		}
    		if (s[0]=='m')
    		{
    			int x=gi()+1,y=gi()+1;
    			if (findroot(x)^findroot(y))
    				puts("-1");
    			else
    				split(x,y),printf("%d
    ",sum[y]);
    		}
    		if (s[0]=='c')
    		{
    			int id=gi()+1+n,l=gi();
    			makeroot(id);val[id]=l;pushup(id);
    		}
    	}
    	return 0;
    }
    
    
  • 相关阅读:
    Java中静态字段和静态方法
    Java抽象方法、抽象类以及接口
    Java单例模式
    java继承
    java构造方法
    java方法
    Java常量与变量
    Java初识
    1
    补码,反码,加减法运算,类型取值范围
  • 原文地址:https://www.cnblogs.com/zhoushuyu/p/8148692.html
Copyright © 2011-2022 走看看