[Tjoi2016&Heoi2016] sort bisection + line tree

Keywords: REST

Description
In this paper, we give a whole permutation of 1 to n. now we sort the whole permutation sequence m times locally. The sorting can be divided into two types: 1:(0,l,r) is to sort the numbers in the interval [l,r] in ascending order; 2:(1,l,r) is to sort the numbers in the interval [l,r] in descending order, and finally ask the numbers in the q position.

Sample Input
6 3
1 6 2 5 3 4
0 1 4
1 3 6
0 2 4
3

Sample Output
5

This problem is similar to middle's thinking, which transforms the original sequence into a simple 01 sequence to operate.
First of all, consider the dichotomy answer. We mark the point of the weight of > = current dichotomy as 1, and the rest of the table bits 0. In fact, sorting is to move all 1 to the right, or all 0 to the right. At the end, we can see whether the query position is 0 or 1, and deal with dichotomy.

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

struct node {
    int opt, l, r;
} p[110000];
struct trnode {
    int l, r, lc, rc, c, lazy;
} tr[210000]; int trlen;
int n, m, a[110000];
int hh;

void bt(int l, int r) {
    int now = ++trlen;
    tr[now].l = l; tr[now].r = r; tr[now].c = 0; tr[now].lazy = -1;
    tr[now].lc = tr[now].rc = -1;
    if(l < r) {
        int mid = (l + r) / 2;
        tr[now].lc = trlen + 1; bt(l, mid);
        tr[now].rc = trlen + 1; bt(mid + 1, r);
    }
}

void Lazy(int now) {
    int lc = tr[now].lc, rc = tr[now].rc;
    tr[lc].c = (tr[lc].r - tr[lc].l + 1) * tr[now].lazy;
    tr[rc].c = (tr[rc].r - tr[rc].l + 1) * tr[now].lazy;
    tr[lc].lazy = tr[now].lazy; tr[rc].lazy = tr[now].lazy;
    tr[now].lazy = -1;
}

void change(int now, int l, int r, int c) {
    if(l == tr[now].l && r == tr[now].r) {tr[now].c = (r - l + 1) * c; tr[now].lazy = c; return ;}
    if(tr[now].lazy != -1) Lazy(now);
    int lc = tr[now].lc, rc = tr[now].rc;
    int mid = (tr[now].l + tr[now].r) / 2;
    if(r <= mid) change(lc, l, r, c);
    else if(l > mid) change(rc, l, r, c);
    else change(lc, l, mid, c), change(rc, mid + 1, r, c);
    tr[now].c = tr[lc].c + tr[rc].c;
}

int findsum(int now, int l, int r) {
    if(l == tr[now].l && r == tr[now].r) return tr[now].c;
    if(tr[now].lazy != -1) Lazy(now);
    int lc = tr[now].lc, rc = tr[now].rc;
    int mid = (tr[now].l + tr[now].r) / 2;
    if(r <= mid) return findsum(lc, l, r);
    else if(l > mid) return findsum(rc, l, r);
    else return findsum(lc, l, mid) + findsum(rc, mid + 1, r);
}

bool check(int x) {
    for(int i = 1; i <= n; i++) {
        if(x <= a[i]) change(1, i, i, 1);
        else change(1, i, i, 0);
    }
    for(int i = 1; i <= m; i++) {
        int gg = findsum(1, p[i].l, p[i].r);
        if(!p[i].opt) {
            if(p[i].l <= p[i].r - gg) change(1, p[i].l, p[i].r - gg, 0);
            if(p[i].r - gg + 1 <= p[i].r) change(1, p[i].r - gg + 1, p[i].r, 1);
        }
        else {
            if(p[i].l <= p[i].l + gg - 1) change(1, p[i].l, p[i].l + gg - 1, 1);
            if(p[i].l + gg <= p[i].r) change(1, p[i].l + gg, p[i].r, 0);
        }
    }
    return findsum(1, hh, hh) == 1;
}

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", &p[i].opt, &p[i].l, &p[i].r);
    scanf("%d", &hh);
    bt(1, n);
    int l = 1, r = n, ans;
    while(l <= r) {
        int mid = (l + r) / 2;
        if(check(mid)) l = mid + 1, ans = mid;
        else r = mid - 1;
    }
    printf("%d\n", ans);
    return 0;
}

Posted by yonta on Wed, 01 Apr 2020 00:56:30 -0700