萌新刚学OI,线段树+二分求调
查看原帖
萌新刚学OI,线段树+二分求调
527425
wyhnet楼主2023/7/7 15:53

RT,样例都过不了,程序输出不是全0

#include <bits/stdc++.h>
#define endl '\n';
using namespace std;
const int N=1e5+10;
struct node1 {
	int l,r;
	bool flag;
}query[N];
struct node2 {
	int l,r,cover,sum;
	bool iscovered;
}t[N<<2];
int n,m,q,a[N];
void pushup(int p){
	t[p].sum = t[p<<1].sum+t[p<<1|1].sum;
}
void pushdown(int p){
	if(t[p].iscovered){
		t[p<<1].cover = t[p<<1|1].cover = t[p].cover;
		t[p<<1].sum = t[p].cover*(t[p<<1].r-t[p<<1].l+1);
		t[p<<1|1].sum = t[p].cover*(t[p<<1|1].r-t[p<<1|1].l+1);
		t[p<<1].iscovered = t[p<<1|1].iscovered = 1;
		t[p].iscovered = t[p].cover = 0;
	}
}
void build(int p,int l,int r,int val){
	t[p].l = l;
	t[p].r = r;
	if(l==r){
		t[p].sum = a[l]>=val;
		return;
	}
	int mid=l+r>>1;
	build(p<<1,l,mid,val);
	build(p<<1|1,mid+1,r,val);
	pushup(p);
}
void cover(int p,int l,int r,int k){
	if(l<=t[p].l&&t[p].r){
		t[p].iscovered = 1;
		t[p].cover = k;
		return;
	}
	pushdown(p);
	int mid=t[p].l+t[p].r>>1;
	if(l<=mid) cover(p<<1,l,r,k);
	if(mid<r) cover(p<<1|1,l,r,k);
	pushup(p);
}
int ask(int p,int l,int r){
	if(l<=t[p].l&&t[p].r<=r) return t[p].sum;
	pushdown(p);
	int mid=t[p].l+t[p].r>>1,ret=0;
	if(l<=mid) ret += ask(p<<1,l,r);
	if(mid<r) ret += ask(p<<1|1,l,r);
	return ret;
}
void Sort(int l,int r,bool flag){
	int x=ask(1,l,r);
	if(!flag){
		x = r-l+1-x;
		cover(1,l,l+x-1,0);
		cover(1,l+x,r,1);
	}
	else {
		cover(1,l,l+x-1,1);
		cover(1,l+x,r,0);
	}
}
bool check(int x){
	memset(t,0,sizeof(t));
	build(1,1,n,x);
	for(int i=1;i<=m;i++){
		Sort(query[i].l,query[i].r,query[i].flag);
	}
	return ask(1,q,q);
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n >> m;
	for(int i=1;i<=n;i++){
		cin >> a[i];
	}
	for(int i=1;i<=m;i++){
		cin >> query[i].flag >> query[i].l >> query[i].r;
	}
	cin >> q;
	int l=1,r=n,ans=-1;
	while(l<=r){
		int mid=l+r>>1;
		if(check(mid)){
			ans = mid;
			l = mid+1;
		}
		else r = mid-1;
	}
	cout << ans;
	return 0;
}
2023/7/7 15:53
加载中...