zoukankan      html  css  js  c++  java
  • Codeforces Round #276 (Div. 1) E. Sign on Fence 二分+主席树

    E. Sign on Fence
     

    Bizon the Champion has recently finished painting his wood fence. The fence consists of a sequence of n panels of 1 meter width and of arbitrary height. The i-th panel's height is hi meters. The adjacent planks follow without a gap between them.

    After Bizon painted the fence he decided to put a "for sale" sign on it. The sign will be drawn on a rectangular piece of paper and placed on the fence so that the sides of the sign are parallel to the fence panels and are also aligned with the edges of some panels. Bizon the Champion introduced the following constraints for the sign position:

    1. The width of the sign should be exactly w meters.
    2. The sign must fit into the segment of the fence from the l-th to the r-th panels, inclusive (also, it can't exceed the fence's bound in vertical direction).

    The sign will be really pretty, So Bizon the Champion wants the sign's height to be as large as possible.

    You are given the description of the fence and several queries for placing sign. For each query print the maximum possible height of the sign that can be placed on the corresponding segment of the fence with the given fixed width of the sign.

    Input

    The first line of the input contains integer n — the number of panels in the fence (1 ≤ n ≤ 105).

    The second line contains n space-separated integers hi, — the heights of the panels (1 ≤ hi ≤ 109).

    The third line contains an integer m — the number of the queries (1 ≤ m ≤ 105).

    The next m lines contain the descriptions of the queries, each query is represented by three integers lr and w (1 ≤ l ≤ r ≤ n1 ≤ w ≤ r - l + 1) — the segment of the fence and the width of the sign respectively.

    Output

    For each query print the answer on a separate line — the maximum height of the sign that can be put in the corresponding segment of the fence with all the conditions being satisfied.

    Examples
    input
    5
    1 2 2 3 3
    3
    2 5 3
    2 5 2
    1 5 5
    output
    2
    3
    1
    Note

    The fence described in the sample looks as follows:

    The possible positions for the signs for all queries are given below.

    The optimal position of the sign for the first query.
    The optimal position of the sign for the second query.
    The optimal position of the sign for the third query.

    题意:

      给你n个数,每个数表示一个高度。

      m个询问,每次询问你l,r内连续w个数的最低高度的最大值

      note解释样例很详细

    题解:

      主席树的技巧

      按照高度排序,倒着插入每一颗线段树中

      查询的话,二分历史版本线段树的位置,在l,r这段区间内至少存在连续w个位置存在有值,很明显的线段树的区间合并,区间查询了

    #include<bits/stdc++.h>
    using namespace std;
    #pragma comment(linker, "/STACK:102400000,102400000")
    #define ls i<<1
    #define rs ls | 1
    #define mid ((ll+rr)>>1)
    #define pii pair<int,int>
    #define MP make_pair
    typedef long long LL;
    const long long INF = 1e18;
    const double Pi = acos(-1.0);
    const int N = 1e5+10, M = 1e6, mod = 1e9+7, inf = 2e9;
    int n,root[N],m,l[N*36],r[N*36],rmx[N*36],lmx[N*36],mx[N*36],sz,v[N*36];
    pair<int ,int > a[N];
    void push_up(int i,int ll,int rr) {
            lmx[i] = lmx[l[i]];
            if(lmx[i] == mid - ll + 1) lmx[i] += lmx[r[i]];
            rmx[i] = rmx[r[i]];
            if(rmx[i] == rr - mid) rmx[i] += rmx[l[i]];
            mx[i] = max(lmx[r[i]]+rmx[l[i]],max(mx[l[i]],mx[r[i]]));
    }
    void update(int x,int &y,int ll,int rr,int k,int c) {
            v[y = ++sz] = v[x] + 1;
            l[y] = l[x];
            r[y] = r[x];
            if(ll == rr)  {
                mx[y] = lmx[y] = rmx[y] = c;
                l[y] = 0; r[y] = 0;
                return ;
            }
            if(k <= mid) update(l[x],l[y],ll,mid,k,c);
            else update(r[x],r[y],mid+1,rr,k,c);
            push_up(y,ll,rr);
    }
    int query(int i,int ll,int rr,int s,int t) {
            if(s > t) return 0;
            if(s == ll && rr == t) return mx[i];
            int ret = 0;
            if(t <= mid) ret = query(l[i],ll,mid,s,t);
            else if(s > mid) ret = query(r[i],mid+1,rr,s,t);
            else {
                ret = max(query(l[i],ll,mid,s,mid),query(r[i],mid+1,rr,mid+1,t));
                int lx = min(rmx[l[i]],mid - s + 1);
                int rx = min(lmx[r[i]],t - mid);
                ret = max(ret, lx + rx);
            }
            return ret;
    }
    int main() {
            scanf("%d",&n);
            for(int i = 1; i <= n; ++i) scanf("%d",&a[i].first),a[i].second = i;
            sort(a+1,a+n+1);
            for(int i = n; i >= 1; --i) update(root[i+1],root[i],1,n,a[i].second,1);
            scanf("%d",&m);
            for(int i = 1; i <= m; ++i) {
                int x,y,w;
                scanf("%d%d%d",&x,&y,&w);
                int l = 1, r = n, ans = n;
                while(l <= r) {
                    int md = (l+r)>>1;
                    int ss = query(root[md],1,n,x,y);
                    if(ss >= w) l = md+1,ans=md;
                    else r = md - 1;
                }
                printf("%d
    ",a[ans].first);
            }
            return 0;
    }
  • 相关阅读:
    洛谷 P4484
    洛谷 P4900
    Codeforces 1500D
    Codeforces 1322D
    2021.9.30 Codeforces 中档题四道
    BZOJ 3729
    洛谷 P6276
    Codeforces 1511G
    C语言 typedef
    C语言 回调函数 callback
  • 原文地址:https://www.cnblogs.com/zxhl/p/5945646.html
Copyright © 2011-2022 走看看