【bzoj4149】[AMPPZ2014]Global Warming

一开始以为一个单调队列就搞定了。。。后来发现好像根本不是那么一回事QAQ

这题其实还是挺有意思的。。。

参考了一下claris的题解>.<
http://www.cnblogs.com/clrs97/p/4582835.html

首先要求出每个点的最大值区间和最小值区间,然后可以得到答案区间之间的不等关系,再枚举左端点,根据不等式在线段树里面查询。

#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
#define For(i,a,b) for(int i=a;i< b;i++) 
#define pii pair<int , int>
#define mp make_pair
#define pb push_back
#define pos first
#define rgt second
#define maxn 500003
#define maxm 3000003
#define inf 0x7fffffff

inline int rd() {
    char c = getchar();
    while (!isdigit(c) && c != '-') c = getchar() ;
    int x = 0 , f = 1;
    if (c == '-') f = -1 ; else x = c - '0';
    while (isdigit(c = getchar())) x = x * 10 + c - '0';
    return x * f;
}

inline void upmax(int&a , int b) { if (a < b) a = b ; }

typedef int arr[maxn];
typedef int seg[maxm];

int n , ans_len , ans_st;

arr a , lmx , rmx , lmn , rmn;
vector<pii> G[maxn] , H[maxn];

struct SegTree{

    #define T int u = 1 , int l = 1 , int r = n
    #define L lc , l , m
    #define R rc , m + 1 , r
    #define lc (u << 1)
    #define rc (u << 1 | 1)

    seg val;
    int ql , qr , v;

    inline void C() { memset(val , 0 , sizeof val) ; }

    void modi(T) {
        if (l == r)
            { val[u] = v ; return ; }
        int m = (l + r) >> 1;
        if (ql <= m) modi(L);
        else modi(R);
        val[u] = max(val[lc] , val[rc]);
    }

    int que(T) {
        if (ql <= l && r <= qr)
            return val[u];
        int m = (l + r) >> 1 , ret = -inf;
        if (ql <= m) upmax(ret , que(L));
        if (qr >  m) upmax(ret , que(R));
        return ret;
    }

    inline void M(int l , int v) {
        ql = l , this -> v = v;
        modi();
    }

    inline int Q(int l , int r) {
        ql = l , qr = r;
        return que();
    }

}num;

void input() {
    n = rd();
    rep(i , 1 , n) a[i] = rd();
}

void get_lr() {
    static arr q;
    static int t;
    t = 0 , q[0] = 0;
    rep(i , 1 , n) {
        while (t && a[q[t]] > a[i]) t --;
        lmn[i] = q[t] + 1 , q[++ t] = i;
    }
    t = 0 , q[0] = 0;
    rep(i , 1 , n) {
        while (t && a[q[t]] < a[i]) t --;
        lmx[i] = q[t] + 1 , q[++ t] = i;
    }
    t = 0 , q[0] = n + 1;
    per(i , n , 1) {
        while (t && a[q[t]] > a[i]) t --;
        rmn[i] = q[t] - 1 , q[++ t] = i;
    }
    t = 0 , q[0] = n + 1;
    per(i , n , 1) {
        while (t && a[q[t]] < a[i]) t --;
        rmx[i] = q[t] - 1 , q[++ t] = i;
    }
}

void work() {
    rep(i , 1 , n) G[i].clear();
    rep(i , 1 , n) H[i].clear();
    rep(i , 1 , n) G[lmn[i]].pb(mp(i , rmn[i]));
    rep(i , 1 , n) H[lmx[i]].pb(mp(i , rmx[i]));
    rep(i , 1 , n) {
        For(j , 0 , G[i].size()) num.M(G[i][j].pos , G[i][j].rgt);
        For(j , 0 , H[i].size()) {
            int p = H[i][j].pos , r = H[i][j].rgt;
            int t = num.Q(i , r);
            if (t < p) continue;
            int l = min(r , t) - i + 1;
            if ((l > ans_len) || (l == ans_len && i < ans_st))
                ans_len = l , ans_st = i;
        }
    }
}

void solve() {
    get_lr();
    work();
    rep(i , 1 , n) swap(lmx[i] , lmn[i]);
    rep(i , 1 , n) swap(rmx[i] , rmn[i]);
    num.C();
    work();
    printf("%d %d\n" , ans_len , ans_st);
}

int main() {
    #ifndef ONLINE_JUDGE
        freopen("data.txt" , "r" , stdin);
        freopen("data.out" , "w" , stdout);
    #endif
    input();
    solve();
    return 0;
}

你可能感兴趣的:(【bzoj4149】[AMPPZ2014]Global Warming)