跳到正文

题解

题解:P11266 【模板】完全体·堆

Water__Problem约 4 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 4 分钟
题目
P11266

版本

仅有当前版本

一些废话

显然我不会 pbds 的 priority_queue。所以这能用 std 类里的 priority_queue。


题解

让我们来分析一下操作。

-1:对于每个 1≤i≤n1\le i\le n 建一个堆。存储的节点要记录两个量:在aa数组中的位置和现在存储的值,按存储的值排序。

0:对于删除,建一个数组 ledled 如果删了,就打一个标记。

1:对于查询,我们先要把堆中已经变了的量删除,显然我们只要保证堆顶的量是正确的即可。

2:直接把 yy 的桶中的元素全部搬到 xx 中就行。

3:对于修改。直接再往 xx 的桶中加进 {y,zy,z} 这个量,并把 aya_y 赋值成 zz 就行。


code

C++
#include<bits/stdc++.h>
using namespace std;
template<typename T> inline void read(T &n){
	n=0;short f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
	while('0'<=ch&&ch<='9'){n=(n<<1)+(n<<3)+(ch^48);ch=getchar();}
	n=n*f;
}
template<typename T> void write(T n){
	if(n<0){printf("-");write(-n);return;}
	if(n<10){putchar(char(n+'0'));return;}
	write(n/10);
	putchar(char(n%10+'0'));
}
template<typename T> void Write(T n,char ch){
	write(n);
	putchar(ch);
}
#define int long long
int n,m,a[1000005],led[1000005];
struct node{
	int id,num;
};
struct cmp{
	bool operator()(node x,node y){
		return x.num>y.num;
	}
};
priority_queue<node,vector<node>,cmp> p[1000005];//堆
signed main(){
	read(n);read(m);
	for(int i=1;i<=n;i++) read(a[i]);
	for(int i=1;i<=n;i++) p[i].push(node{i,a[i]});
	for(int i=1;i<=m;i++){
		int tag;
		read(tag);
		if(tag==0){
			int x,y;
			read(x);read(y);
			led[y]=1;
		}
		if(tag==1){
			int x;
			read(x);
			while(!p[x].empty()){//维护堆顶
				node u=p[x].top();
				p[x].pop();
				if(led[u.id]) continue;
				if(a[u.id]!=u.num) p[x].push(node{u.id,a[u.id]});
				else{
					p[x].push(u);
					break;
				}
			}
			Write(p[x].top().num,'\n');
		}
		if(tag==2){
			int x,y;
			read(x);read(y);
			while(!p[y].empty()){
				p[x].push(p[y].top());
				p[y].pop();
			}
		}
		if(tag==3){
			int x,y,z;
			read(x);read(y);read(z);
			a[y]=z;
			p[x].push(node{y,z});
		}
	}
	return 0;
}
原文