0分求调
查看原帖
0分求调
482610
Mortidesperatslav楼主2023/10/3 11:19
#include<bits/stdc++.h>
using namespace std;
int n,q,a[200005],fs[200005],qz[200005],zs[200005],dy[200005];
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin>>n>>q;
	for(int i=1;i<=n;i++)cin>>a[i];
	for(int i=1;i<=n;i++)if(a[i]<0)fs[i]=fs[i-1]+1;else fs[i]=fs[i-1];
	int p=0;
	for(int i=1;i<=n;i++)if(a[i]>0)zs[++p]=i,dy[i]=p;
	for(int i=q;i<=n;i++)qz[i]=qz[i-1]+a[i];
	for(int _=1;_<=q;_++){
		int l,r,ans=0;
		cin>>l>>r;
		while(1){
			if(l>=r)break;
			int *fz=lower_bound(zs+1,zs+p+1,l);
			int *lz=upper_bound(zs+1,zs+p+1,r)-1;
			ans+=(dy[*lz]-dy[*fz]+1);
			if(fz==lz)break;
			l=dy[*(upper_bound(zs+1,zs+p+1,l))];
		}
		cout<<ans<<"\n";
	}
}
2023/10/3 11:19
加载中...