题解:CF1732D2 Balance (Hard version)
这还能有 *2400 的,打 duel 的时候半个小时不到切了。
这个值域是是
我们维护两个集合
查询的时候我们直接从
加入 set 即可。
删除
这样的正确性是对的,因为你查询的时候枚举到一个值,那么只有比这个值小的被删了之后可能会更新答案,所以当这个东西被删掉的时候再加回来是对的。
考虑这个东西的复杂度,首先查询的删除是同阶的,所以只看查询,发现对于一个
#include<bits/stdc++.h>
#define int long long
#define set set<int>
using namespace std;
map<int,set>mp,ans;
set st;
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int T;
cin>>T;
while(T--){
char op;
cin>>op;
int x;
cin>>x;
if(op=='+'){
st.insert(x);
}else if(op=='-'){
set t=mp[x];
for(auto it=t.begin();it!=t.end();it++){
int y=(*it);
ans[y].insert(x);
}
st.erase(x);
}else{
int t=x;
while(st.find(t)!=st.end()){
mp[t].insert(x);
if(!ans[x].empty()&&(*ans[x].rbegin())>=t+x){
auto it=ans[x].lower_bound(t+x);
if(ans[x].find(t)!=ans[x].end())
ans[x].erase(ans[x].find(t));
t=(*it);
}else{
if(ans[x].find(t)!=ans[x].end())
ans[x].erase(ans[x].find(t));
t+=x;
}
}
ans[x].insert(t);
cout<<t<<'\n';
}
}
return 0;
}