求调 0pts 详细评测记录在贴内 能过样例
查看原帖
求调 0pts 详细评测记录在贴内 能过样例
1258467
hao12345ia楼主2025/7/1 19:26

代码:

#include<bits/stdc++.h>
using namespace std;
int n,m;
int a[1000001];
int tree[4000001];
int las[1000001],pre[1000001];
void build(int l,int r,int u){
	if(l==r){
		tree[u]=las[l]+pre[l];
		return ;
	}
	int mid=(l+r)/2;
	build(l,mid,u*2);
	build(mid+1,r,u*2+1);
	tree[u]=max(tree[u*2],tree[u*2+1]); 
}
int find(int l,int r,int u,int x,int y){
	if(x>=l&&y<=r){
		return tree[u];
	}
	int mid=(l+r)/2;
	int ans=0;
	if(x<=mid){
		ans=find(l,mid,u*2,x,y);
	} 
	if(y>mid){
		ans=max(find(mid+1,r,u*2+1,x,y),ans);
	}
	return ans;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		las[i]=las[i-1]+(a[i]==0?1:0); 
	}
	for(int i=n;i>=1;i--){
		pre[i]=pre[i+1]+(a[i]==1?0:1);
	}
	build(1,n,1);
	for(int i=1;i<=m;i++){
		int l,r,opt;
		cin>>opt>>l>>r;
		if(opt==1){
			cout<<find(1,n,1,l,r)-las[l-1]-pre[r];
		}
		if(opt==2){
			cout<<1+(pre[l]!=pre[r]);
		}
		cout<<endl; 
	} 
}
2025/7/1 19:26
加载中...