0%

abc212d题解

简单的小根堆优先队列

设置小根堆

1
2
3
4
5
6
struct cmp_pq{
bool operator()(ll a,ll b){
return a>b;//返回true:表示a的优先级低于b,因此b应该被放在更靠近堆顶的位置
}
};
priority_queue<ll,vector<ll>,cmp_pq> pq;

完整代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include<bits/stdc++.h>
using namespace std;
#define maxn 200010
#define MOD 998244353
#define ll long long
#define endl '\n'

struct cmp_pq{
bool operator()(ll a,ll b){
return a>b;//返回true:表示a的优先级低于b,因此b应该被放在更靠近堆顶的位置
}
};
ll basex=0;
ll p,x,ans;
int main() {
ll n;
cin>>n;
priority_queue<ll,vector<ll>,cmp_pq> pq;
for(int i=1;i<=n;i++){

cin>>p;
if(p==1){
cin>>x;
pq.push((ll)x-basex);
}else if(p==2){
cin>>x;
basex+=x;
}else{
ans=pq.top()+basex;
pq.pop();
cout<<ans<<endl;
}
//cout<<pq.top()<<"+"<<basex<<endl;
}
return 0;
}