Breaking Down News【HDU-6856】【线段树+单调队列/栈】

2020 Multi-University Training Contest 8 B


  有N个权值,每个权值只能是{-1, 0, 1}中的一个,然后现在分成几段区间,每一段的长度在[L, R]之间,每一个段的权值为:

  1. >0时候,权值=1
  2. <0时候,权值=-1
  3. =0时候,权值=0

  现在,要求这几个段的∑之和最大值。

可以列出dp方程,dp[i] = max(dp[j] + (s[i] > s[j]) - (s[i] < s[j]))

简单的说,我们可以看成三种情况,我们维护一个前缀和s[i],然后维护这样的三种情况:

  1. s[j] < s[i]时候,dp[i] = dp[j] + 1
  2. s[j] > s[i]时候,dp[i] = dp[j] - 1
  3. s[j] = s[i]时候,dp[i] = dp[j]

  分别讨论三种情况,然后求出对应的最大值dp[j]即可。

  所以,我们可以维护一棵权值线段树,然后我们插入这样的dp[j]到对应的s[j]权值的点上去,之后就可以用上面的方法维护了。但是要注意的是,当一个点有多个dp[j]进入的时候,我们需要进行处理,这里可以维护一个单调递减的单调队列/栈来进行这样的操作。

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef unsigned long long ull;
typedef unsigned int uit;
typedef long long ll;
const int maxN = 1e6 + 7;
int N, L, R, s[maxN];
int tree[maxN << 2];
#define pii pair
#define MP(a, b) make_pair(a, b)
#define Max_3(a, b, c) max(a, max(b, c))
vector Q[maxN];
int top[maxN];
bool used[maxN];
int dp[maxN];
void add(int rt, int l, int r, int qx, pii val)
{
    if(l == r)
    {
        while(Q[l].size() > top[l] && Q[l].back() < val)
        {
            used[Q[l].back().second] = false;
            Q[l].pop_back();
        }
        used[val.second] = true;
        Q[l].push_back(val);
        tree[rt] = Q[l][top[l]].first;
        return;
    }
    int mid = HalF;
    if(qx <= mid) add(Lson, qx, val);
    else add(Rson, qx, val);
    tree[rt] = max(tree[lsn], tree[rsn]);
}
void del(int rt, int l, int r, int qx, pii val)
{
    if(l == r)
    {
        top[l]++;
        if(top[l] == Q[l].size()) tree[rt] = -INF;
        else tree[rt] = Q[l][top[l]].first;
        return;
    }
    int mid = HalF;
    if(qx <= mid) del(Lson, qx, val);
    else del(Rson, qx, val);
    tree[rt] = max(tree[lsn], tree[rsn]);
}
int query(int rt, int l, int r, int ql, int qr)
{
    if(ql <= l && qr >= r) return tree[rt];
    int mid = HalF;
    if(qr <= mid) return query(QL);
    else if(ql > mid) return query(QR);
    else return max(query(QL), query(QR));
}
int main()
{
    int T; scanf("%d", &T);
    while(T--)
    {
        scanf("%d%d%d", &N, &L, &R);
        s[0] = 0;
        int maxx = -INF, minn = INF, _UP;
        for(int i=1, x; i<=N; i++)
        {
            scanf("%d", &x);
            s[i] = s[i - 1] + x;
            maxx = max(maxx, s[i]);
            minn = min(minn, s[i]);
            used[i] = false;
        }
        used[0] = false;
        maxx = max(maxx, 0);
        minn = min(minn, 0);
        _UP = 1 - minn; //(l, r)->(minn + _UP, maxx + _UP)
        int nL = minn + _UP, nR = maxx + _UP;
        for(int i=1; i<=(nR << 2); i++) tree[i] = -INF;
        for(int i=nL; i<=nR; i++)
        {
            top[i] = 0;
            while(!Q[i].empty()) Q[i].pop_back();
        }
        int ans[3];
        for(int i=1; i= 0 && used[ql]) del(1, nL, nR, s[ql] + _UP, MP(dp[ql], ql));
        }
        printf("%d\n", dp[N]);
    }
    return 0;
}

 

你可能感兴趣的:(线段树,单调队列/栈,线段树,单调队列,单调栈)