值轮兵团站岗

· · 个人记录

可以发现,这其实是一个双头队列的模拟操作。但我们显然不能简单地进行模拟,因为 O(mn) 的时间复杂度难以承受,我们需要利用其它变量来进行记录。

#include<bits/stdc++.h>
using namespace std;
const int N =3e5+5;
long long a[N],tail=0,head=1;
long long n,m,k;
long long opt,x,cmp;
int main(){
    cin>>n>>m>>k;
    for(int i=1;i<=n;i++){
        cin>>a[++tail];
    }
    sort(a+1,a+1+n);
    while(m--)
    {
        cin>>opt;
        if(opt==1)
        {
        cin>>x;
        cmp+=x;
        while(a[tail]+cmp>k&&tail-head+1>0){
            tail--;
        }   
        }
        if(opt==2)
        {
        cin>>x;
        cmp-=x;
        while(a[head]+cmp<-k&&tail-head+1>0){
            head++;
        }   
        }
        if(opt==3){
            cout<<tail-head+1<<endl;
        }
    }
    return 0;
}