Bzoj4552: [Tjoi2016&Heoi2016]排序

Posted Cyhlnj

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Bzoj4552: [Tjoi2016&Heoi2016]排序相关的知识,希望对你有一定的参考价值。

题目

传送门

Sol

二分+线段树
巧妙啊我怎么就没想到
二分答案,把数分类,大于等于\(mid\)的为\(1\),小于的为\(0\)
相当于给\(01\)序列排序,最后判断询问位置上是不是\(1\)
线段树+lazy覆盖

# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(1e5 + 5);

IL int Input(){
    RG int x = 0, z = 1; RG char c = getchar();
    for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
    for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    return x * z;
}

int n, m, q, a[_], ql[_], qr[_], qo[_];
int sum[_ << 2], cov[_ << 2];

IL void Build(RG int x, RG int l, RG int r, RG int v){
    sum[x] = 0, cov[x] = -1;
    if(l == r){
        sum[x] = a[l] >= v;
        return;
    }
    RG int mid = (l + r) >> 1, ls = x << 1, rs = x << 1 | 1;
    Build(ls, l, mid, v), Build(rs, mid + 1, r, v);
    sum[x] = sum[ls] + sum[rs];
}

IL void Pushdown(RG int x, RG int l, RG int mid, RG int r){
    RG int ls = x << 1, rs = x << 1 | 1;
    cov[ls] = cov[rs] = cov[x];
    sum[ls] = (mid - l + 1) * cov[x];
    sum[rs] = (r - mid) * cov[x];
    cov[x] = -1;
}

IL void Modify(RG int x, RG int l, RG int r, RG int L, RG int R, RG int v){
    if(L <= l && R >= r){
        sum[x] = (r - l + 1) * v;
        cov[x] = v;
        return;
    }
    RG int mid = (l + r) >> 1, ls = x << 1, rs = x << 1 | 1;
    if(cov[x] != -1) Pushdown(x, l, mid, r);
    if(L <= mid) Modify(ls, l, mid, L, R, v);
    if(R > mid) Modify(rs, mid + 1, r, L, R, v);
    sum[x] = sum[ls] + sum[rs];
}

IL int Query(RG int x, RG int l, RG int r, RG int L, RG int R){
    if(cov[x] != -1) return cov[x] * (R - L + 1);
    if(L <= l && R >= r) return sum[x];
    RG int mid = (l + r) >> 1;
    if(R <= mid) return Query(x << 1, l, mid, L, R);
    if(L > mid) return Query(x << 1 | 1, mid + 1, r, L, R);
    return Query(x << 1, l, mid, L, mid) + Query(x << 1 | 1, mid + 1, r, mid + 1, R);
}

IL bool Check(RG int mid){
    Build(1, 1, n, mid);
    for(RG int i = 1; i <= m; ++i){
        RG int cnt = Query(1, 1, n, ql[i], qr[i]);
        if(!cnt || cnt == qr[i] - ql[i] + 1) continue;
        if(qo[i]){
            Modify(1, 1, n, ql[i], ql[i] + cnt - 1, 1);
            Modify(1, 1, n, ql[i] + cnt, qr[i], 0);
        }
        else{
            Modify(1, 1, n, ql[i], qr[i] - cnt, 0);
            Modify(1, 1, n, qr[i] - cnt + 1, qr[i], 1);
        }
    }
    return Query(1, 1, n, q, q);
}

int main(RG int argc, RG char* argv[]){
    n = Input(), m = Input();
    for(RG int i = 1; i <= n; ++i) a[i] = Input();
    for(RG int i = 1; i <= m; ++i) qo[i] = Input(), ql[i] = Input(), qr[i] = Input();
    q = Input();
    RG int l = 1, r = n, ans = 0;
    while(l <= r){
        RG int mid = (l + r) >> 1;
        if(Check(mid)) ans = mid, l = mid + 1;
        else r = mid - 1;
    }
    printf("%d\n", ans);
    return 0;
}

以上是关于Bzoj4552: [Tjoi2016&Heoi2016]排序的主要内容,如果未能解决你的问题,请参考以下文章

BZOJ 4552: [Tjoi2016&Heoi2016]排序

[BZOJ4552][TJOI2016&&HEOI2016]排序

BZOJ4552:[Tjoi2016&Heoi2016]排序

[BZOJ] 4552: [Tjoi2016&Heoi2016]排序 #二分+线段树+算法设计策略

bzoj 4552 [Tjoi2016&Heoi2016]排序——二分答案

bzoj千题计划128:bzoj4552: [Tjoi2016&Heoi2016]排序