Codeforces 484E Sign on Fence

Codeforces 484E Sign on Fence
题意:给n个高度为h的木板,连续放置。然后询问l,r区间内,连续w个最小高度的最大值是多少。


做法:首先询问能够用二分答案。然后对于每一个二分的值我们须要建主席树来验证是否可行。主席树主要实现功能,显然,假设把高度大于等于二分值ans

位置都置为1,其它位置置为0。那么我们仅仅须要验证区间l,r中是否有超过w个连续的1。对于这个问题用线段树显然可解。于是我们就能够用主席树来维护。这种话就能够log查询了。

建主席树:首先把高度从大到小分别更新到主席树中,查询的时候仅仅须要查询T[mid]那颗树就能够了。


//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<string>
#include<queue>
#include<cmath>
#include<stack>
#include<set>
#include<map>
#define FIR first
#define SEC second
#define LL long long
#define MP make_pair
#define INF 0x3f3f3f3f
#define ULL unsigned long long
#define CLR(a, b) memset(a, b, sizeof(a))

using namespace std;

const int maxn = 2002000;

int lson[maxn], rson[maxn], T[maxn], tot = 1;

struct Qry {
    int v, lv, rv, len;
    Qry(int v = 0, int lv = 0, int rv = 0, int len = 0)
        :v(v), lv(lv), rv(rv), len(len) {}
} val[maxn];

Qry add(Qry a, Qry b) {
    Qry ret;
    ret.v = max(a.v, b.v);
    ret.v = max(ret.v, a.rv + b.lv);
    ret.lv = a.lv;
    if(a.lv == a.len) ret.lv += b.lv;
    ret.rv = b.rv;
    if(b.rv == b.len) ret.rv += a.rv;
    ret.len = a.len + b.len;
    return ret;
}

void PushUp(int rt) {
    val[rt] = add(val[lson[rt]], val[rson[rt]]);
}

int build(int l, int r) {
    int rt = tot ++;
    if(l == r) {
        val[rt] = Qry(0, 0, 0, 1);
        return rt;
    }
    int m = (l + r) >> 1;
    lson[rt] = build(l, m);
    rson[rt] = build(m + 1, r);
    PushUp(rt);
    return rt;
}

int update(int rt, int l, int r, int pos) {
    int nrt = tot ++;
    if(l == r) {
        val[nrt] = Qry(1, 1, 1, 1);
        return nrt;
    }
    int m = (l + r) >> 1;
    if(pos <= m) {
        lson[nrt] = update(lson[rt], l, m, pos);
        rson[nrt] = rson[rt];
    } else {
        lson[nrt] = lson[rt];
        rson[nrt] = update(rson[rt], m + 1, r, pos);
    }
    PushUp(nrt);
    return nrt;
}

Qry query(int rt, int l, int r, int L, int R) {
    if(l >= L && r <= R) {
        return val[rt];
    }
    int m = (l + r) >> 1;
    Qry ls(0, 0, 0, m - l + 1), rs(0, 0, 0, r - m);
    if(m >= L) ls = query(lson[rt], l, m, L, R);
    if(m < R) rs = query(rson[rt], m + 1, r, L, R);
    return add(ls, rs);
}

int h[maxn], idx[maxn];

bool cmp(int i, int j) {
    return h[i] > h[j];
}

int main() {
    int n, m;
    while(scanf("%d", &n) != EOF) {
        tot = 1;
        T[0] = build(1, n);
        for(int i = 1; i <= n; i ++) {
            scanf("%d", &h[i]);
            idx[i] = i;
        }
        sort(idx + 1, idx + n + 1, cmp);
        for(int i = 1; i <= n; i ++) {
            T[i] = update(T[i - 1], 1, n, idx[i]);
        }
        scanf("%d", &m);
        while(m --) {
            int l, r, w;
            scanf("%d%d%d", &l, &r, &w);
            int L = 1, R = n;
            while(L <= R) {
                int mid = (L + R) >> 1;
                Qry ret = query(T[mid], 1, n, l, r);
                if(ret.v >= w) R = mid - 1;
                else L = mid + 1;
            }
            printf("%d\n", h[idx[L]]);
        }
    }
}


posted @ 2017-08-20 11:29  jzdwajue  阅读(203)  评论(0编辑  收藏  举报