寒冬又一次肆虐了北国大地
无情的北风穿透了人们御寒的衣物
可怜虫们在冬夜中发出无助的哀嚎
“冻死宝宝了!”
这时
远处的天边出现了一位火焰之神
“我将赐予你们温暖和希望!”
只见他的身体中喷射出火焰之力
通过坚固的钢铁,传遍了千家万户
这时,只听见人们欢呼
“暖气来啦!”
任务描述
虽然小R住的宿舍楼早已来了暖气,但是由于某些原因,宿舍楼中的某些窗户仍然开着(例如厕所的窗户),这就使得宿舍楼中有一些路上的温度还是很低。
小R的宿舍楼中有nn个地点和一些路,一条路连接了两个地点,小R可以通过这条路从其中任意一个地点到达另外一个地点。但在刚开始,小R还不熟悉宿舍楼中的任何一条路,所以他会慢慢地发现这些路,他在发现一条路时还会知道这条路的温度和长度。每条路的温度都是互不相同的。
小R需要在宿舍楼中活动,每次他都需要从一个地点到达另一个地点。小R希望每次活动时经过一条最温暖的路径,最温暖的路径的定义为,将路径上各条路的温度从小到大排序后字典序最大。即温度最低的路温度尽量高,在满足该条件的情况下,温度第二低的路温度尽量高,以此类推。小R不会经过重复的路。由于每条路的温度互不相同,因此只存在一条最温暖的路径。
对于小R的每次活动,你需要求出小R需要走过的路径总长度。如果小R通过当前发现的路不能完成这次活动,则输出 −1−1。
注意本题中的字典序与传统意义上的字典序定义有所不同,对于两个序列a,b(a≠b)a,b(a≠b),若aa是bb的前缀则aa的字典序较大,同时可以推出空串的字典序最大。
输入格式
第一行两个正整数 n,mn,m。表示小R的宿舍楼中有 nn 个地点,共发生了 mm 个事件。
接下来 mm 行,每行描述一个事件,事件分为三类。
find id u v t lfind id u v t l 表示小R发现了一条连接uu和vv之间的路,编号为idid。相同idid的边只会出现一次。
move u vmove u v 表示小R要从uu到达vv,你需要计算出最温暖的路径的长度 ,若不能从uu到达vv,则输出−1−1。
change id lchange id l 表示从uu到vv这条边的长度变为了ll(保证在当前时间点这条边存在)。
输出格式
对于每个询问,输出一行整数,表示最温暖的路径长度。
样例一
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)(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分。
测试点 nn mm 其它其它
1−21−2 ≤20≤20 ≤50≤50 无特殊约定
3−53−5 ≤1000≤1000 ≤3000≤3000
6−106−10 ≤100000≤100000 ≤300000≤300000 所有的find事件都在move事件之前,且没有change事件
11−1411−14 所有的find事件都在move事件之前
15−2015−20 无特殊约定
时间限制:2s2s
空间限制:512MB
题解
lct
类似于魔法森林,动态维护最大生成树。
依次加入边< u,v>
如果u与v不连通,则直接加入这条边(把边看做点)
如果连通,则删除这个环上温度最低的边。
怎么维护呢?
我们记录一个数组mn[u]表示mn[u]这条边(splay中是个点)的温度最低
void update(int x)
{
int l=c[x][0],r=c[x][1];
mn[x]=x;
if (val[mn[l]]< val[mn[x]]) mn[x]=mn[l];
if (val[mn[r]]< val[mn[x]]) mn[x]=mn[r];
sum[x]=sum[l]+sum[r]+dis[x];
}
val表示温度,sum表示路程,dis表示这条边的路程长度。
询问直接makeroot(x)+access(y)+splay(y)计算答案
修改边长度splay(x)+update(x)
代码
#include<bits/stdc++.h>
#define ll long long
#define inf 1000000000
using namespace std;
inline int read()
{
int x=0;char ch=getchar();
while(ch<'0'||ch>'9')ch=getchar();
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x;
}
int f[400005],fa[400005],c[400005][2],rev[400005],mn[400005],val[400005];
int n,m,sum[400005],q[400005],u[400005],v[400005],dis[400005];
int find(int i){if (f[i]!=i)f[i]=find(f[i]);return f[i];}
bool isroot(int x){return c[fa[x]][0]!=x&&c[fa[x]][1]!=x;}
void pushdown(int x)
{
int l=c[x][0],r=c[x][1];
if (rev[x])
{
rev[l]^=1;rev[r]^=1;rev[x]^=1;
swap(c[x][0],c[x][1]);
}
}
void update(int x)
{
int l=c[x][0],r=c[x][1];
mn[x]=x;
if (val[mn[l]]<val[mn[x]]) mn[x]=mn[l];
if (val[mn[r]]<val[mn[x]]) mn[x]=mn[r];
sum[x]=sum[l]+sum[r]+dis[x];
}
void rotate(int x)
{
int y=fa[x],z=fa[y],l,r;
if (c[y][0]==x)l=0;else l=1;r=l^1;
if (!isroot(y))
{
if (c[z][0]==y) c[z][0]=x;else c[z][1]=x;
}
fa[x]=z;fa[y]=x;
c[y][l]=c[x][r];fa[c[x][r]]=y;c[x][r]=y;
update(y);update(x);
}
void splay(int x)
{
int top=1;q[top]=x;
for (int i=x;!isroot(i);i=fa[i]) q[++top]=fa[i];
while (top) pushdown(q[top--]);
while (!isroot(x))
{
int y=fa[x],z=fa[y];
if (!isroot(y))
{
if (c[z][0]==y^c[y][0]==x)rotate(x);else rotate(y);
}
rotate(x);
}
}
void access(int x)
{
int t=0;
while (x)
{
splay(x);
c[x][1]=t;
t=x;
update(x);
x=fa[x];
}
}
void makeroot(int x)
{
access(x);splay(x);rev[x]^=1;
}
void link(int x,int y)
{
makeroot(x);
fa[x]=y;
}
void cut(int x,int y)
{
makeroot(x);access(y);splay(y);
c[y][0]=fa[x]=0;update(y);
}
int query(int x,int y)
{
makeroot(x);access(y);splay(y);
return mn[y];
}
int main()
{
n=read();m=read();val[0]=inf;
for (int i=1;i<=n;i++) f[i]=i,val[i]=inf;
while (m--)
{
char ch[10];scanf("%s",ch);
if (ch[0]=='f')
{
int id=read()+n+1;f[id]=id;
u[id]=read()+1,v[id]=read()+1,val[id]=read(),dis[id]=read();
if (find(u[id])!=find(v[id]))
{
mn[id]=id;
link(u[id],id);link(v[id],id);
f[f[u[id]]]=id;
f[f[v[id]]]=id;
}
else
{
int t=query(u[id],v[id]);
if (val[id]>val[t])
{
val[t]=inf;
cut(t,u[t]);cut(t,v[t]);mn[id]=id;
link(u[id],id);link(v[id],id);
}
}
}
else if (ch[0]=='m')
{
int u=read()+1,v=read()+1;
if (find(u)!=find(v)) puts("-1");
else
{
makeroot(u);access(v);splay(v);
printf("%d\n",sum[v]);
}
}
else
{
int id=read()+n+1;dis[id]=read();
splay(id);update(id);
}
}
return 0;
}