Description
寒冬又一次肆虐了北国大地
无情的北风穿透了人们御寒的衣物
可怜虫们在冬夜中发出无助的哀嚎
“冻死宝宝了!”
这时
远处的天边出现了一位火焰之神
“我将赐予你们温暖和希望!”
只见他的身体中喷射出火焰之力
通过坚固的钢铁,传遍了千家万户
这时,只听见人们欢呼
“暖气来啦!”
任务描述:
虽然小R住的宿舍楼早已来了暖气,但是由于某些原因,宿舍楼中的某些窗户仍然开着(例如厕所的窗户),这就使得宿舍楼中有一些路上的温度还是很低。
小R的宿舍楼中有n个地点和一些路,一条路连接了两个地点,小R可以通过这条路从其中任意一个地点到达另外一个地点。但在刚开始,小R还不熟悉宿舍楼中的任何一条路,所以他会慢慢地发现这些路,他在发现一条路时还会知道这条路的温度和长度。每条路的温度都是互不相同的。
小R需要在宿舍楼中活动,每次他都需要从一个地点到达另一个地点。小R希望每次活动时经过一条最温暖的路径,最温暖的路径的定义为,将路径上各条路的温度从小到大排序后字典序最大。即温度最低的路温度尽量高,在满足该条件的情况下,温度第二低的路温度尽量高,以此类推。小R不会经过重复的路。由于每条路的温度互不相同,因此只存在一条最温暖的路径。
对于小R的每次活动,你需要求出小R需要走过的路径总长度。如果小R通过当前发现的路不能完成这次活动,则输出 −1。
注意本题中的字典序与传统意义上的字典序定义有所不同,对于两个序列a,b(a≠b),若a是b的前缀则a的字典序较大,同时可以推出空串的字典序最大。
Input
第一行两个正整数 n,m。表示小R的宿舍楼中有 n 个地点,共发生了 m 个事件。
接下来 m 行,每行描述一个事件,事件分为三类。
1、 find id u v t l 表示小R发现了一条连接u和v之间的路,编号为id。相同id的边只会出现一次。
2、 move u v 表示小R要从u到达v,你需要计算出最温暖的路径的长度 ,若不能从u到达v,则输出−1。
3、 change id l 表示从u到v这条边的长度变为了l(保证在当前时间点这条边存在)。
Output
对于每个询问,输出一行整数,表示最温暖的路径长度。
Sample 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
Sample Output
11 -1 6 23 18 106 122 11 0
Hint
样例二
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对于move操作:(0≤u,v对于change操作:(0≤l≤10000)。
对于100%的数据,1≤n≤100000,1≤m≤300000 。
本题共有20个数据点,每个数据点5分。
题目大意:维护最大生成树,询问路径和。
#include<bits/stdc++.h>
using namespace std;
const int Maxn=600005;
struct Splay{
bool rev[Maxn];
int p[Maxn],ch[Maxn][2];
int t[Maxn],v[Maxn],sum[Maxn],minp[Maxn];
#define Ls(x) ch[x][0]
#define rs(x) ch[x][1]
inline bool isroot(int x){
return (Ls(p[x])^x)&&(rs(p[x])^x);
}
inline void maintain(int x){
minp[x]=x;
sum[x]=v[x]+sum[Ls(x)]+sum[rs(x)];
if(t[minp[Ls(x)]]<t[minp[x]])minp[x]=minp[Ls(x)];
if(t[minp[rs(x)]]<t[minp[x]])minp[x]=minp[rs(x)];
}
inline void rotate(int x){
int f=p[x],gf=p[f],tp=rs(f)==x,son=ch[x][!tp];
if(!isroot(f))ch[gf][rs(gf)==f]=x;p[x]=gf;
ch[p[son]=f][tp]=son,maintain(f);
ch[p[f]=x][!tp]=f,maintain(x);
}
inline void pushdown(int x){
if(rev[x]){
rev[Ls(x)]^=1,rev[rs(x)]^=1;
swap(Ls(x),rs(x));
rev[x]^=1;
}
}
int top,s[Maxn];
inline void splay(int x){
s[++top]=x;for(int i=x;!isroot(i);i=p[i])s[++top]=p[i];
while(top)pushdown(s[top--]);
while(!isroot(x)){
if(!isroot(p[x])&&((rs(p[p[x]])==p[x])==(rs(p[x])==x)))rotate(p[x]);
rotate(x);
}
}
};
struct LinkCutTree{
Splay splay;
inline void access(int x){
for(int lastx=0;x;lastx=x,x=splay.p[x])
splay.splay(x),splay.rs(x)=lastx,splay.maintain(x);
}
inline void makeroot(int x){
access(x),splay.splay(x),splay.rev[x]^=1;
}
inline void link(int x,int y){
makeroot(x),splay.p[x]=y;
}
inline void split(int x,int y){
makeroot(x),access(y),splay.splay(y);
}
inline void cut(int x,int y){
split(x,y);splay.Ls(y)=splay.p[x]=0;
splay.maintain(y);
}
inline int getroot(int x){
access(x),splay.splay(x);
for(;splay.Ls(x);x=splay.Ls(x));
return x;
}
}Lct;
int n,m,L[Maxn],r[Maxn];
inline void trylink(){
int idx,x,y,t,l;scanf("%d%d%d%d%d",&idx,&x,&y,&t,&l);
++x,++y,idx+=n+1;
if(Lct.getroot(x)==Lct.getroot(y)){
Lct.split(x,y);
int minp=Lct.splay.minp[y];
if(Lct.splay.t[minp]<t){
// cout<<L[minp]<<" "<<r[m]
Lct.cut(minp,L[minp]),Lct.cut(minp,r[minp]);
Lct.splay.t[idx]=t;
Lct.splay.minp[idx]=idx;
Lct.splay.v[idx]=Lct.splay.sum[idx]=l;
L[idx]=x,r[idx]=y;
Lct.link(x,idx),Lct.link(idx,y);
}
}else {
Lct.splay.t[idx]=t;
Lct.splay.minp[idx]=idx;
Lct.splay.v[idx]=Lct.splay.sum[idx]=l;
L[idx]=x,r[idx]=y;
Lct.link(x,idx),Lct.link(idx,y);
}
}
inline void trycalc(){
int x,y;scanf("%d%d",&x,&y);++x,++y;
// cout<<x<<" "<<y<<"\n";
if(Lct.getroot(x)^Lct.getroot(y))return puts("-1"),void();
Lct.split(x,y);
cout<<Lct.splay.sum[y]<<"\n";
}
inline void modify(){
int idx,l;scanf("%d%d",&idx,&l);idx+=n+1;
// cout<<idx<<"\n";
Lct.access(idx),Lct.splay.splay(idx);
Lct.splay.v[idx]=l,Lct.splay.maintain(idx);
}
int main(){
memset(Lct.splay.t,63,sizeof(Lct.splay.t));
scanf("%d%d",&n,&m);
while(m--){
char op[10];scanf("%s",op+1);
if(op[1]=='f')trylink();
if(op[1]=='m')trycalc();
if(op[1]=='c')modify();
}
return 0;
}