# BZOJ4552 [Tjoi2016&Heoi2016]排序 【二分 + 线段树】

BZOJ4552

## 题解

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
#define ls (u << 1)
#define rs (u << 1 | 1)
using namespace std;
const int maxn = 100005,maxm = 100005,INF = 1000000000;
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
int n,m,opt[maxn],ql[maxn],qr[maxn],a[maxn],M,q;
int sum[maxn << 2],tag[maxn << 2];
void upd(int u){sum[u] = sum[ls] + sum[rs];}
void pd(int u,int l,int r){
if (tag[u] == 1){
int mid = l + r >> 1;
sum[ls] = mid - l + 1;
sum[rs] = r - mid;
tag[ls] = tag[rs] = 1;
tag[u] = 0;
}
else if (tag[u] == 2){
sum[ls] = sum[rs] = 0;
tag[ls] = tag[rs] = 2;
tag[u] = 0;
}
}
void build(int u,int l,int r){
tag[u] = 0;
if (l == r){
sum[u] = (a[l] >= M);
return;
}
int mid = l + r >> 1;
build(ls,l,mid);
build(rs,mid + 1,r);
upd(u);
}
void modify(int u,int l,int r,int L,int R,int v){
if (l >= L && r <= R){sum[u] = v * (r - l + 1); tag[u] = v ? 1 : 2; return;}
pd(u,l,r);
int mid = l + r >> 1;
if (mid >= L) modify(ls,l,mid,L,R,v);
if (mid < R) modify(rs,mid + 1,r,L,R,v);
upd(u);
}
int query(int u,int l,int r,int L,int R){
if (l >= L && r <= R) return sum[u];
pd(u,l,r);
int mid = l + r >> 1;
if (mid >= R) return query(ls,l,mid,L,R);
if (mid < L) return query(rs,mid + 1,r,L,R);
return query(ls,l,mid,L,R) + query(rs,mid + 1,r,L,R);
}
bool check(int x){
M = x; int l,r,s;
build(1,1,n);
REP(i,m){
l = ql[i]; r = qr[i];
s = query(1,1,n,l,r);
if (!opt[i]){
if (s) modify(1,1,n,r - s + 1,r,1);
if (s != r - l + 1) modify(1,1,n,l,r - s,0);
}
else {
if (s) modify(1,1,n,l,l + s - 1,1);
if (s != r - l + 1) modify(1,1,n,l + s,r,0);
}
}
return query(1,1,n,q,q) == 1;
}
int main(){