【jzoj 4605】【luogu P2824】排序(二分)(线段树)
作者:互联网
排序(线段树合并与分裂板题)
题目链接:jzoj 4605 / luogu P2824
题目大意
给你一个全排列,然后每次操作会将数列中的某个区间按升序或降序排序。
然后问你最后第 i 个位置上的数是什么。
思路
我们考虑二分,二分这个答案,然后大于等于它的是
1
1
1,小于的是
0
0
0。
那就变成看这个位置是
1
1
1 是
0
0
0,排序就先找到这个区间
0
,
1
0,1
0,1 的个数,然后再按要求区间修改放下去,这个用线段树搞就可以了。
然后二分出来答案就可以了。
代码
#include<cstdio>
using namespace std;
struct ask {
int op, l, r;
}q[200001];
int n, m, a[200001], pl;
int ans, l, r, mid, nm0, nm1;
bool s[200001];
struct Tree {
int num0, num1;
bool all0, all1;
}t[800001];
void up(int now) {//标记上传下传
t[now].num0 = t[now << 1].num0 + t[now << 1 | 1].num0;
t[now].num1 = t[now << 1].num1 + t[now << 1 | 1].num1;
if (t[now << 1].all0 && t[now << 1 | 1].all0) t[now].all0 = 1;
else t[now].all0 = 0;
if (t[now << 1].all1 && t[now << 1 | 1].all1) t[now].all1 = 1;
else t[now].all1 = 0;
}
void down(int now, int l, int r) {
int mid = (l + r) >> 1;
if (t[now].all0) {
t[now << 1].all0 = t[now << 1 | 1].all0 = 1;
t[now << 1].num0 = mid - l + 1;
t[now << 1 | 1].num0 = t[now].num0 - t[now << 1].num0;
t[now << 1].all1 = t[now << 1 | 1].all1 = 0;
t[now << 1].num1 = t[now << 1 | 1].num1 = 0;
}
if (t[now].all1) {
t[now << 1].all1 = t[now << 1 | 1].all1 = 1;
t[now << 1].num1 = mid - l + 1;
t[now << 1 | 1].num1 = t[now].num1 - t[now << 1].num1;
t[now << 1].all0 = t[now << 1 | 1].all0 = 0;
t[now << 1].num0 = t[now << 1 | 1].num0 = 0;
}
}
void build(int now, int l, int r) {
if (l == r) {
t[now].num0 = t[now].num1 = 0;
t[now].all0 = t[now].all1 = 0;
if (s[l]) t[now].num1++, t[now].all1 = 1;
else t[now].num0++, t[now].all0 = 1;
return ;
}
int mid = (l + r) >> 1;
build(now << 1, l, mid);
build(now << 1 | 1, mid + 1, r);
up(now);
}
int get0(int now, int l, int r, int L, int R) {//找到这个区间 0/1 的个数
if (L <= l && r <= R) {
return t[now].num0;
}
down(now, l, r);
int mid = (l + r) >> 1, re = 0;
if (L <= mid) re += get0(now << 1, l, mid, L, R);
if (mid < R) re += get0(now << 1 | 1, mid + 1, r, L, R);
up(now);
return re;
}
int get1(int now, int l, int r, int L, int R) {
if (L <= l && r <= R) {
return t[now].num1;
}
down(now, l, r);
int mid = (l + r) >> 1, re = 0;
if (L <= mid) re += get1(now << 1, l, mid, L, R);
if (mid < R) re += get1(now << 1 | 1, mid + 1, r, L, R);
up(now);
return re;
}
void change0(int now, int l, int r, int L, int R) {//把这个区间全部改成 0/1
if (L <= l && r <= R) {
t[now].all0 = 1; t[now].num0 = r - l + 1;
t[now].all1 = 0; t[now].num1 = 0;
return ;
}
down(now, l, r);
int mid = (l + r) >> 1;
if (L <= mid) change0(now << 1, l, mid, L, R);
if (mid < R) change0(now << 1 | 1, mid + 1, r, L, R);
up(now);
}
void change1(int now, int l, int r, int L, int R) {
if (L <= l && r <= R) {
t[now].all1 = 1; t[now].num1 = r - l + 1;
t[now].all0 = 0; t[now].num0 = 0;
return ;
}
down(now, l, r);
int mid = (l + r) >> 1;
if (L <= mid) change1(now << 1, l, mid, L, R);
if (mid < R) change1(now << 1 | 1, mid + 1, r, L, R);
up(now);
}
bool find(int now, int l, int r, int pl) {//判断一个位置是 0/1
if (l == r) {
return t[now].num1;
}
down(now, l, r);
int mid = (l + r) >> 1;
if (pl <= mid) return find(now << 1, l, mid, pl);
else return find(now << 1 | 1, mid + 1, r, pl);
}
bool ck(int mid) {//二分中的判断
for (int i = 1; i <= n; i++)
if (a[i] >= mid) s[i] = 1;
else s[i] = 0;
build(1, 1, n);
for (int i = 1; i <= m; i++) {
nm0 = get0(1, 1, n, q[i].l, q[i].r);
nm1 = get1(1, 1, n, q[i].l, q[i].r);
if (q[i].op == 0) {
if (nm0) change0(1, 1, n, q[i].l, q[i].l + nm0 - 1);
if (nm1) change1(1, 1, n, q[i].l + nm0, q[i].r);
}
else {
if (nm1) change1(1, 1, n, q[i].l, q[i].l + nm1 - 1);
if (nm0) change0(1, 1, n, q[i].l + nm1, q[i].r);
}
}
return find(1, 1, n, pl);
}
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", &q[i].op, &q[i].l, &q[i].r);
}
scanf("%d", &pl);
l = 1; r = n;
while (l <= r) {//二分
mid = (l + r) >> 1;
if (ck(mid)) ans = mid, l = mid + 1;
else r = mid - 1;
}
printf("%d", ans);
return 0;
}
标签:4605,return,num1,jzoj,int,luogu,mid,num0,now 来源: https://blog.csdn.net/weixin_43346722/article/details/119193840