[ABC212D] Querying Multiset
Genius_Star · · 题解
思路:
感觉 ~priority_queue 好慢啊
发现这是一个对于堆的模板题目。
对于操作 push_heap 函数,插入这个数之后可以保持堆的性质,对于操作 pop_heap 函数维护堆。
对于操作
对于此题我们要维护的是小根堆,用数组怎么办呢?没事,将其转换为负数就行了。
完整代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1000100;
inline ll read(){
ll x=0,f=1;
char c=getchar();
while(c<'0'||c>'9'){
if(c=='-')
f=-1;
c=getchar();
}
while(c>='0'&&c<='9'){
x=(x<<1)+(x<<3)+(c^48);
c=getchar();
}
return x*f;
}
inline void write(ll x){
if(x<0){
putchar('-');
x=-x;
}
if(x>9)
write(x/10);
putchar(x%10+'0');
}
ll a[N],q,op,x,sum=0,l=1;
int main(){
q=read();
while(q--){
op=read();
if(op==1){
x=read()-sum;
a[l++]=-x;
push_heap(a+1,a+l);
}
else if(op==2)
sum+=read();
else{
write(-(a[1])+sum),putchar('\n');
swap(a[1],a[l]);
pop_heap(a+1,a+l);
l--;
}
}
return 0;
}