小清新珂朵莉题。
我们考虑在推区间的时候维护一下极大的连续 0 的长度集合。
由于不同的长度最多有 种,所以在询问的时候可以直接进行枚举的复杂度是 的,可以通过。
AC Code
#include<bits/stdc++.h>
using namespace std;using ll=long long;using ull=unsigned long long;using uint=unsigned int;mt19937_64 rnd(time(0));
constexpr int N=5e4+9,inf=0x3f3f3f3f;
int n,m;
map<int,set<int>> lenpos;
map<int,pair<int,bool>> tr={{-inf,{inf,1}}};
void erase(auto it){
if(it->second.second) return;
int len=it->second.first-it->first+1;
lenpos[len].erase(it->first);
if(lenpos[len].empty()) lenpos.erase(len);
}
void insert(auto it){
if(it->second.second) return;
lenpos[it->second.first-it->first+1].insert(it->first);
}
void split(int x){
auto it=prev(tr.upper_bound(x));
if(it->first==x) return;
erase(it);
tr[x]=it->second;
it->second.first=x-1;
insert(it),insert(tr.find(x));
}
void join(auto itl,auto itr){
if(itl->second.second!=itr->second.second) return;
erase(itl),erase(itr);
itl->second=itr->second;
tr.erase(itr);
insert(itl);
}
void assign(int l,int r,bool k){
split(l),split(r+1);
for(auto it=tr.find(l);it->first<=r;) erase(it),it=tr.erase(it);
tr[l]={r,k};
auto it=tr.find(l);
insert(it);
join(it,next(it));
join(prev(it),it);
}
int find(int x){
int mi=inf;
for(auto& [p,q]:lenpos){
if(p<x) continue;
mi=min(mi,*q.begin());
}
if(mi==inf) return 0;
assign(mi,mi+x-1,1);
return mi;
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
cin>>n>>m;
assign(1,n,0);
for(int i=1,opt,x,y;i<=m;i++){
cin>>opt>>x;
if(opt==1) cout<<find(x)<<'\n';
else{
cin>>y;
assign(x,x+y-1,0);
}
}
return 0;
}