萌新刚学OI,10pts 堆求条
查看原帖
萌新刚学OI,10pts 堆求条
1220111
xiaoyang222楼主2024/5/21 22:29
#include <iostream>
#include <queue>
#define int long long
using namespace std;
const int N=3e5+5;
int a[N];//当前对应的数,是没有任何懒标记 
priority_queue<pair<int,int>> q[N];//连通块
priority_queue<pair<int,int>> t;//全局 
priority_queue<pair<int,int>> del1[N];//连通块的懒删除
priority_queue<pair<int,int>> del2;//全局的懒删除 
//对于t和del2,不加全局的懒标记 
//对于q和del2,不加局部的懒标记和全局的懒标记 
int f[N];//并查集
int tag1[N],tag2;//两个懒标记 
int find(int x){
	return f[x]==x ? x : f[x]=find(f[x]);
}
void update1(int idx/*连通块*/){
	idx=find(idx);
	while (!del1[idx].empty() && !q[idx].empty() && q[idx].top()==del1[idx].top()){
		del1[idx].pop();
		q[idx].pop();
	}
}
void update2(){
	while (!del2.empty() && !t.empty() && t.top()==del2.top()){
		t.pop();
		del2.pop();
	}
}
void debug(){
	priority_queue<pair<int,int>> qwq=t;
	while (!qwq.empty()){
		cout<<qwq.top().first<<" "<<qwq.top().second<<endl;
		qwq.pop();
	}
}
signed main(){
	int n;
	cin >> n;
	for (int i=1;i<=n;++i){
		scanf("%lld",&a[i]);
		f[i]=i;
		q[i].emplace(a[i],i);
		t.emplace(a[i],i);
	}
	int u,v,w;
	char op;
	int m;
	cin >> m;
	pair<int,int> p;
	for (int _=0;_<m;++_){
		op=getchar();
		while (op==' ' || op=='\n' || op=='\r' || op==EOF){
			op=getchar();			
		}
		if (op=='U'){
			scanf("%lld%lld",&u,&v);
			u=find(u),v=find(v);
			if (u==v){
				continue;
			}
			if (q[u].size()>q[v].size()){//u合并到v 
				swap(u,v);
			}
			update1(u);
			while (!q[u].empty()){//合并 
				update1(u);//刷新防止有错误的 
				p=q[u].top();//顶上 
				q[u].pop();
				a[p.second]+=tag1[u];
				a[p.second]-=tag1[v];//换标记 
				p.first=a[p.second];//换一个 
				q[v].emplace(a[p.second],p.second);
				update1(u);
			}
			update1(v);
			f[u]=v;
		}else if (op=='A'){
			op=getchar();
			if (op=='1'){
				scanf("%lld%lld",&u,&w);
				del1[find(u)].emplace(a[u],u);//加入懒删除 
				del2.emplace(a[u]+tag1[find(u)],u);//加入懒删除 
				a[u]+=w;
				q[find(u)].emplace(a[u],u);//局部 
				t.emplace(a[u]+tag1[find(u)],u);//加上懒标记的全局 
			}else if (op=='2'){
				scanf("%lld%lld",&u,&w);
				u=find(u);
				update1(u);
				p=q[u].top();
				del2.emplace(p);//全局删除 
				tag1[u]+=w;
				p.first+=w;
				t.emplace(p);//全局 
			}else{
				scanf("%lld",&w);
				tag2+=w;//加上懒标记 
			}
		}else{
			op=getchar();
			if (op=='1'){
				scanf("%lld",&u);	
				printf("%lld\n",a[u]+tag1[find(u)]+tag2);
			}else if (op=='2'){
				scanf("%lld",&u);
				u=find(u);
				update1(u);
				p=q[u].top();
				printf("%lld\n",p.first+tag1[u]+tag2);
			}else{
				update2();
				p=t.top();
				printf("%lld\n",p.first+tag2);
			}
		}
//		debug();
//		for (int i=1;i<=n;++i){
//			cout<<a[i]<<" ";
//		}
//		cout<<endl;
	}
	return 0;
}
/*
5
0 0 0 0 0
6
A3 3
F3
A1 1 -2
U 1 3
F2 3
F1 1
*/
2024/5/21 22:29
加载中...