Description
有N个节点,标号从1到N,这N个节点一开始相互不连通。第i个节点的初始权值为a[i],接下来有如下一些操作:
U x y: 加一条边,连接第x个节点和第y个节点
A1 x v: 将第x个节点的权值增加v
A2 x v: 将第x个节点所在的连通块的所有节点的权值都增加v
A3 v: 将所有节点的权值都增加v
F1 x: 输出第x个节点当前的权值
F2 x: 输出第x个节点所在的连通块中,权值最大的节点的权值
F3: 输出所有节点中,权值最大的节点的权值
对于30%的数据,保证 N<=100,Q<=10000
对于80%的数据,保证 N<=100000,Q<=100000
对于100%的数据,保证 N<=300000,Q<=300000
对于所有的数据,保证输入合法,并且 -1000<=v, a[1], a[2], …, a[N]<=1000
Solution
正解是可并堆,然而我写的左偏树复杂度似乎并不是特别科学,也许是人丑常数大
可以考虑离线所有操作先把图建出来,一个特别的建图技巧就是把y向x所在的根连边,这样dfs序始终就是连续的一段了(意识流
然后就能线段树搞搞了
Code
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define rep(i,st,ed) for (int i=st;i<=ed;++i)
#define drp(i,st,ed) for (int i=st;i>=ed;--i)
const int INF=0x3f3f3f3f;
const int N=300005;
const int E=300005;
struct edge {int y,next;} e[E];
int max[N*4],tag[N*4];
int ls[N],edCnt;
int fa[N],a[N],x[N],y[N];
int pos[N],L[N],R[N];
int opt[N];
int read() {
int x=0,v=1; char ch=getchar();
for (;ch<'0'||ch>'9';v=(ch=='-')?(-1):(v),ch=getchar());
for (;ch>='0'&&ch<='9';x=x*10+ch-'0',ch=getchar());
return x*v;
}
void add_edge(int x,int y) {
if (x==y) return ;
e[++edCnt]=(edge) {y,ls[x]}; ls[x]=edCnt;
}
int get_father(int x) {
if (fa[x]==x) return x;
return fa[x]=get_father(fa[x]);
}
void dfs(int now) {
pos[now]=L[now]=R[now]=++pos[0];
for (int i=ls[now];i;i=e[i].next) {
dfs(e[i].y);
}
}
void push_down(int now) {
if (!tag[now]) return ;
max[now<<1]+=tag[now];
max[now<<1|1]+=tag[now];
tag[now<<1]+=tag[now];
tag[now<<1|1]+=tag[now];
tag[now]=0;
}
void modify(int now,int tl,int tr,int l,int r,int v) {
if (l>r) return ;
if (tl==l&&tr==r) {
max[now]+=v;
tag[now]+=v;
return ;
}
int mid=(tl+tr)>>1;
push_down(now);
modify(now<<1,tl,mid,l,std:: min(r,mid),v);
modify(now<<1|1,mid+1,tr,std:: max(mid+1,l),r,v);
max[now]=std:: max(max[now<<1],max[now<<1|1]);
}
int query(int now,int tl,int tr,int l,int r) {
if (l>r) return -INF;
if (tl==l&&tr==r) return max[now];
int mid=(tl+tr)>>1;
push_down(now);
int qx=query(now<<1,tl,mid,l,std:: min(mid,r));
int qy=query(now<<1|1,mid+1,tr,std:: max(mid+1,l),r);
return std:: max(qx,qy);
}
int main(void) {
int n=read();
rep(i,1,n) a[i]=read();
rep(i,1,n) fa[i]=i;
int T=read();
rep(i,1,T) {
char str[3]; scanf("%s",str);
if (str[0]=='A') {
if (str[1]=='1') opt[i]=2;
if (str[1]=='2') opt[i]=3;
if (str[1]=='3') opt[i]=4;
x[i]=read();
if (str[1]!='3') y[i]=read();
} else if (str[0]=='F') {
if (str[1]=='1') opt[i]=5;
if (str[1]=='2') opt[i]=6;
if (str[1]=='3') opt[i]=7;
if (str[1]!='3') x[i]=read();
} else {
x[i]=read(),y[i]=read();
x[i]=get_father(x[i]);
y[i]=get_father(y[i]);
fa[y[i]]=x[i];
opt[i]=1;
}
}
drp(i,T,1) {
if (opt[i]!=1) continue;
add_edge(x[i],y[i]);
}
rep(i,1,n) if (get_father(i)==i) dfs(i);
rep(i,1,n) fa[i]=i;
rep(i,1,n) {
modify(1,1,n,pos[i],pos[i],a[i]);
}
rep(i,1,T) {
if (opt[i]==2) {
modify(1,1,n,pos[x[i]],pos[x[i]],y[i]);
} else if (opt[i]==3) {
int fx=get_father(x[i]);
modify(1,1,n,L[fx],R[fx],y[i]);
} else if (opt[i]==4) {
modify(1,1,n,1,n,x[i]);
} else if (opt[i]==5) {
printf("%d\n", query(1,1,n,pos[x[i]],pos[x[i]]));
} else if (opt[i]==6) {
int fx=get_father(x[i]);
printf("%d\n", query(1,1,n,L[fx],R[fx]));
} else if (opt[i]==7) {
printf("%d\n", query(1,1,n,1,n));
} else {
int fx=get_father(x[i]);
int fy=get_father(y[i]);
fa[fy]=fx;
L[fx]=std:: min(L[fx],L[fy]);
R[fx]=std:: max(R[fx],R[fy]);
}
}
return 0;
}