跳到正文

个人记录

值轮兵团站岗

w20130311约 1 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 1 分钟

版本

  1. v1当前

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

C++
#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;
}
原文