[ABC212D] Querying Multiset
Description
给你一个集合,让你支持三种操作:
-
将 \(x\) 加入集合。
-
把集合中的数都加上 \(x\)。
-
将集合中最小的数删除,并且输出这个数。
Solution
考虑使用优先队列维护这个集合。
注意到操作 2 这个区间加比较难维护,可以考虑设置一个增加量 \(s\) 来代表所有操作 2 中增加的数。
那么在操作 1 中,输入的 \(x\) 没有增加过 \(s\) ,所以直接 push(x-s)。
在操作 2 中比较简单,直接把 \(x\) 加到 \(s\) 里即可。
操作 3 ,直接输出 \(x+s\)。
然后就做完了,复杂度 \(O(T\log |S|)\)。
#include<bits/stdc++.h>
#define int long long
using namespace std;
long long T,opt,x;
long long diff=0;
priority_queue<int>pq;
signed main(){ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);cin>>T;while(T--){cin>>opt;if(opt==1){cin>>x;pq.push(diff-x);}else if(opt==2){cin>>x;diff+=x;}else{cout<<diff-pq.top()<<endl;pq.pop();}}return 0;
}