码迷,mamicode.com
首页 > 编程语言 > 详细

Bzoj4552: [Tjoi2016&Heoi2016]排序

时间:2018-02-25 17:26:51      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:reg   排序   []   题目   二分答案   space   typedef   for   div   

题目

传送门

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]排序

标签:reg   排序   []   题目   二分答案   space   typedef   for   div   

原文地址:https://www.cnblogs.com/cjoieryl/p/8469487.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!