zoukankan      html  css  js  c++  java
  • 【刷题】UOJ #274 【清华集训2016】温暖会指引我们前行

    寒冬又一次肆虐了北国大地

    无情的北风穿透了人们御寒的衣物

    可怜虫们在冬夜中发出无助的哀嚎

    “冻死宝宝了!”

    这时

    远处的天边出现了一位火焰之神

    “我将赐予你们温暖和希望!”

    只见他的身体中喷射出火焰之力

    通过坚固的钢铁,传遍了千家万户

    这时,只听见人们欢呼

    “暖气来啦!”

    任务描述

    虽然小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) 行,每行描述一个事件,事件分为三类。

    1. (find~id~u~v~t~l) 表示小R发现了一条连接uu和vv之间的路,编号为idid。相同idid的边只会出现一次。

    2. (move~u~v) 表示小R要从 (u) 到达 (v) ,你需要计算出最温暖的路径的长度 ,若不能从 (u) 到达 (v) ,则输出 (−1)

    3. (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
    

    样例三

    见样例数据下载

    限制与约定

    对于find操作:((0≤id<m,0≤u,v<n,u≠v,0≤t≤1000000000,0≤l≤10000))

    对于move操作:((0≤u,v<n)(0≤u,v<n))

    对于change操作:((0≤l≤10000)(0≤l≤10000))

    对于100%的数据,(1≤n≤100000,1≤m≤3000001≤n≤100000,1≤m≤300000)

    本题共有20个数据点,每个数据点5分。

    测试点 n m 其它其它
    1−2 ≤20 ≤50 无特殊约定
    3−5 ≤1000 ≤3000
    6−10 ≤100000 ≤300000 所有的find事件都在move事件之前,且没有change事件
    11−14 所有的find事件都在move事件之前
    15−20 无特殊约定

    时间限制:2s

    空间限制:512MB

    下载

    样例数据下载

    题解

    这道题看到那句

    温度最低的路温度尽量高

    那就是维护最大生成树了

    然后这就是裸题了?!

    LCT多维护的有Mn,id,sum(路径之和),val(边权变点权的温度),was(边权变点权的长度)

    对于 (find) ,就直接套路加边

    对于 (move) ,就查询,输出维护的sum

    对于 (change) ,因为修改的只是长度,对于温度的最优性没有影响,所以直接splay到根直接改就行了

    #include<bits/stdc++.h>
    #define ll long long
    #define db double
    #define ld long double
    const int MAXN=100000+10,MAXM=300000+10,inf=0x3f3f3f3f;
    int n,m,fa[MAXN];
    struct edge{
    	int u,v,t,l;
    };
    edge side[MAXM];
    #define lc(x) ch[(x)][0]
    #define rc(x) ch[(x)][1]
    struct LCT{
    	int ch[MAXN+MAXM][2],fa[MAXN+MAXM],id[MAXN+MAXM],val[MAXN+MAXM],Mn[MAXN+MAXM],rev[MAXN+MAXM],stack[MAXN+MAXM],cnt,sum[MAXN+MAXM],was[MAXN+MAXM];
    	inline void init()
    	{
    		memset(ch,0,sizeof(ch));
    		memset(fa,0,sizeof(fa));
    		memset(id,0,sizeof(id));
    		memset(Mn,inf,sizeof(Mn));
    		memset(val,inf,sizeof(val));
    		memset(sum,0,sizeof(sum));
    		memset(was,0,sizeof(was));
    		memset(rev,0,sizeof(rev));
    	}
    	inline bool nroot(int x)
    	{
    		return lc(fa[x])==x||rc(fa[x])==x;
    	}
    	inline void reverse(int x)
    	{
    		std::swap(lc(x),rc(x));
    		rev[x]^=1;
    	}
    	inline void pushup(int x)
    	{
    		sum[x]=sum[lc(x)]+sum[rc(x)]+was[x];
    		Mn[x]=val[x],id[x]=x;
    		if(Mn[lc(x)]<Mn[x])Mn[x]=Mn[lc(x)],id[x]=id[lc(x)];
    		if(Mn[rc(x)]<Mn[x])Mn[x]=Mn[rc(x)],id[x]=id[rc(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=fa[x],p=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=fa[i])stack[++cnt]=fa[i];
    		while(cnt)pushdown(stack[cnt--]);
    		for(register int y=fa[x];nroot(x);rotate(x),y=fa[x])
    			if(nroot(y))rotate((lc(y)==x)==(lc(fa[y])==y)?y:x);
    		pushup(x);
    	}
    	inline void access(int x)
    	{
    		for(register int y=0;x;x=fa[y=x])splay(x),rc(x)=y,pushup(x);
    	}
    	inline int findroot(int x)
    	{
    		access(x);splay(x);
    		while(lc(x))pushdown(x),x=lc(x);
    		splay(x);
    		return 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);
    		if(findroot(y)!=x)fa[x]=y;
    	}
    	inline void cut(int x,int y)
    	{
    		makeroot(x);
    		if(findroot(y)==x&&fa[y]==x&&!rc(y))fa[y]=lc(x)=0,pushup(x);
    	}
    };
    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];
    }
    int main()
    {
    	read(n);read(m);
    	for(register int i=1;i<=n;++i)fa[i]=i;
    	T.init();
    	while(m--)
    	{
    		char opt[10];
    		scanf("%s",opt);
    		if(opt[0]=='f')
    		{
    			int id,u,v,t,l;
    			read(id);read(u);read(v);read(t);read(l);
    			if(u>v)std::swap(u,v);
    			id++;u++;v++;
    			side[id].u=u;side[id].v=v;
    			side[id].t=t;side[id].l=l;
    			int x=found(u),y=found(v),sn=n+id;
    			if(x!=y)
    			{
    				fa[x]=y;
    				T.val[sn]=t;T.was[sn]=l;
    				T.link(sn,u);T.link(sn,v);
    			}
    			else
    			{
    				T.split(u,v);
    				int so=T.id[v];
    				if(t>T.Mn[v])
    				{
    					T.val[sn]=t;T.was[sn]=l;
    					T.cut(so,side[so-n].u);T.cut(so,side[so-n].v);
    					T.link(sn,u);T.link(sn,v);
    				}
    			}
    		}
    		if(opt[0]=='m')
    		{
    			int u,v;
    			read(u);read(v);
    			if(u>v)std::swap(u,v);
    			u++;v++;
    			if(found(u)!=found(v))puts("-1");
    			else T.split(u,v),write(T.sum[v],'
    ');
    		}
    		if(opt[0]=='c')
    		{
    			int id,l;
    			read(id);read(l);
    			id++;
    			T.splay(n+id);T.was[n+id]=l;T.pushup(n+id);
    		}
    	}
    	return 0;
    }
    
  • 相关阅读:
    Android开发探秘之一:创建可以点击的Button
    TCP之心跳包实现思路
    Asp.net与Dojo交互:仪器仪表实现
    Asp.net Json数据解析的一种思路
    Asp.net通过Jquery操作WebService进行Ajax读写
    GridView自定义删除操作
    从客户端中检测到有潜在危险的request.form值
    JUC-Condition线程通信
    ModelAndView 配置与使用
    SpringMVC之ModelAndView的用法(转)
  • 原文地址:https://www.cnblogs.com/hongyj/p/8710237.html
Copyright © 2011-2022 走看看