萌新刚学线段树,求查错
查看原帖
萌新刚学线段树,求查错
261947
yama是女孩子楼主2020/7/23 20:58

Code:

#include <cstdio>
#include <cstring>
#define lc(x) (x << 1)
#define rc(x) (x << 1 | 1)
using namespace std ;
const int MAXN = 1e5 + 10 ;
int a[MAXN] , b[MAXN] , opt[MAXN] , x[MAXN] , y[MAXN] ;
int n , m , q ;
struct sgt {
	int sum , lazy , l , r ;	
} t[MAXN << 2] ;
void build_tree (int o , int l , int r) {
	t[o].l = l ; t[o].r = r ;
	if (l == r) {
		t[o].sum = b[l] ;
		t[o].lazy = -1 ;
		return ;
	}
	int mid = l + r >> 1 ;
	build_tree (lc(o) , l , mid) ;
	build_tree (rc(o) , mid + 1 , r) ;
	t[o].sum = t[lc(o)].sum + t[rc(o)].sum ;
}
void push_down (int o) {
	t[lc(o)].lazy = t[rc(o)].lazy = t[o].lazy ;
	t[lc(o)].sum = (t[lc(o)].r - t[lc(o)].l + 1) * t[o].lazy ;
	t[rc(o)].sum = (t[rc(o)].r - t[rc(o)].l + 1) * t[o].lazy ;
	t[o].lazy = -1 ;
}
void update (int o , int xx , int yy , int k) {
	if (xx <= t[o].l && t[o].r <= yy) {
		t[o].sum = (t[o].r - t[o].l + 1) * k ;
		t[o].lazy = k ;
		return ;
	}
	if (t[o].lazy != -1) push_down (o) ;
	int mid = t[o].l + t[o].r >> 1 ;
	if (xx <= mid) update (lc(o) , xx , yy , k) ;
	if (mid < yy) update (rc(o) , xx , yy , k) ;
	t[o].sum = t[lc(o)].sum + t[rc(o)].sum ;
}
int query (int o , int xx , int yy) {
	if (xx <= t[o].l && t[o].r <= yy)
		return t[o].sum ;
	if (t[o].lazy != -1) push_down (o) ;
	int mid = t[o].l + t[o].r >> 1 , ret = 0 ;
	if (xx <= mid) ret += query (lc(o) , xx , yy) ;
	if (mid < yy) ret += query (rc(o) , xx , yy) ;
	return ret ;
}
bool check (int mid) {
	for (int i = 1 ; i <= n ; i++)
		b[i] = (a[i] >= mid) ? 1 : 0 ;
	build_tree (1 , 1 , n) ;
	for (int i = 1 ; i <= m ; i++) {
		int cnt = query (1 , x[i] , y[i]) ;
		if (!opt[i]) {
			update (1 , x[i] , y[i] - cnt , 0) ;
			update (1 , y[i] - cnt + 1 , y[i] , 1) ;
		}
		else {
			update (1 , x[i] , x[i] + cnt - 1 , 1) ;
			update (1 , x[i] + cnt , y[i] , 0) ;
		}
	}
	return query (1 , q , q) ;
}
int main () {
	scanf ("%d %d" , &n , &m) ;
	for (int i = 1 ; i <= n ; i++)
		scanf ("%d" , &a[i]) ;
	for (int i = 1 ; i <= m ; i++)
		scanf ("%d %d %d" , &opt[i] , &x[i] , &y[i]) ;
	scanf ("%d" , &q) ;
	int l = 1 , r = n , ans = 0 ;
	while (l <= r) {
		int mid = l + r >> 1 ;
		if (check (mid)) ans = l = mid , l++ ;
		else r = mid - 1 ;
	}
	printf ("%d" , ans) ;
	return 0 ;
}
2020/7/23 20:58
加载中...