[BZOJ2006][NOI2010][RMQ/主席樹][二叉堆]超級鋼琴

[Problem Description]
小Z是一個小有名氣的鋼琴家,最近C博士送給了小Z一架超級鋼琴,小Z希望能夠用這架
鋼琴創作出世界上最美妙的音樂。 這架超級鋼琴可以彈奏出n個音符,編號爲1至n。第
i個音符的美妙度爲Ai,其中Ai可正可負。 一個“超級和絃”由若干個編號連續的音符
組成,包含的音符個數不少於L且不多於R。我們定義超級和絃的美妙度爲其包含的所有
音符的美妙度之和。兩個超級和絃被認爲是相同的,當且僅當這兩個超級和絃所包含的
音符集合是相同的。 小Z決定創作一首由k個超級和絃組成的樂曲,爲了使得樂曲更加
動聽,小Z要求該樂曲由k個不同的超級和絃組成。我們定義一首樂曲的美妙度爲其所
包含的所有超級和絃的美妙度之和。小Z想知道他能夠創作出來的樂曲美妙度最大值是
多少。
[Algorithm]
RMQ/主席樹 二叉堆
[Analysis]
超級鋼琴?聽着很霸氣的題目呵……其實題意是說取前k大連續子序列和(當然還有一個L和R的限
制)。求一個前綴和sum,對於一個固定的序列結尾,我們可以用rmq或主席樹求出它可以選擇
的開頭範圍內sum最小的,這樣就可以求出這個結尾的最大的連續子序列和。然後將所有的結尾
的最大和用堆維護。當這個結尾用過了以後,下一次就要求它第2小、第3小的開頭,然後計算出
新的最大和放入堆中(這是主席樹的做法)。當然也可以用rmq,以用過的那個開頭爲界,將原
來的選擇區間分爲兩部分,分別進行rmq。感覺主席樹自己寫的更得心應手一些,我就選擇了主
席樹……
[Pay Attention]
因爲把堆中的編號和序列中的編號弄混了,調試了好長時間……
[Code]

/**************************************************************
    Problem: 2006
    User: gaotianyu1350
    Language: C++
    Result: Accepted
    Time:3080 ms
    Memory:143884 kb
****************************************************************/
 
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
using namespace std;
 
#define MAXN    500100
#define MAXLOG  20
 
struct heapNode
{
    int p, maxx, k, left, right;
    heapNode operator = (const heapNode b) 
    {
        p = b.p;
        maxx = b.maxx;
        k = b.k;
        left = b.left;
        right = b.right;
        return (*this);
    }
};
 
heapNode h[MAXN * 2];
int tot = 0;
int lisan[MAXN], num[MAXN] = {0}, totlisan;
int tree[MAXN * MAXLOG] = {0}, root[MAXN] = {0}, l[MAXN * MAXLOG] = {0}, r[MAXN * MAXLOG] = {0}, size = 0;
int n, k, L, R;
long long ans = 0;
 
inline void update(int now)
{
    tree[now] = tree[l[now]] + tree[r[now]];
}
 
void insert(int pre, int &now, int left, int right, int value)
{
    now = ++size;
    if (left == right)
    {
        tree[now] = tree[pre] + 1;
        return;
    }
    int mid = (left + right) >> 1;
    if(value <= lisan[mid])
    {
        insert(l[pre], l[now], left, mid, value);
        r[now] = r[pre];
        update(now);
    }
    else
    {
        insert(r[pre], r[now], mid + 1, right, value);
        l[now] = l[pre];
        update(now);
    }
}
 
int query(int pre, int now, int left, int right, int k)
{
    if (left == right)
        return lisan[left];
    int mid = (left + right) >> 1;
    int temp = tree[l[now]] - tree[l[pre]];
    if (k <= temp)
        return query(l[pre], l[now], left, mid, k);
    else
        return query(r[pre], r[now], mid + 1, right, k - temp);
}
 
inline void swap(heapNode &a, heapNode &b)
{
    heapNode temp = a; a = b; b = temp;
}
 
void pushdown(int now)
{
    int child;
    while ((now << 1) <= tot)
    {
        child = now << 1;
        if (child < tot && h[child + 1].maxx > h[child].maxx) child++;
        if (h[now].maxx < h[child].maxx)
        {
            swap(h[now], h[child]);
            now = child;
        }
        else
            break;
    }
}
 
void pushup(int now)
{
    int father;
    while (now / 2 >= 1)
    {
        father = now / 2;
        if (h[now].maxx > h[father].maxx)
        {
            swap(h[now], h[father]);
            now = father;
        }
        else
            break;
    }
}
 
inline void solve()
{
    ans += h[1].maxx;
    heapNode temp = h[1];
    h[1] = h[tot--];
    pushdown(1);
    temp.k++;
    if (temp.k > temp.right - temp.left + 1) return;
    temp.maxx = num[temp.p] - query(root[temp.left - 1], root[temp.right], 1, totlisan, temp.k);
    h[++tot] = temp;
    pushup(tot);
}
 
int main()
{
    //freopen("input.txt", "r", stdin);
    scanf("%d%d%d%d", &n, &k, &L, &R);
    for (int i = 1; i <= n; i++)
    {
        int x;
        scanf("%d", &x);
        lisan[i] = num[i] = num[i - 1] + x;
    }
    lisan[n + 1] = 0;
    sort(lisan + 1, lisan + 1 + n + 1);
    totlisan = unique(lisan + 1, lisan + 1 + n + 1) - (lisan + 1);
    insert(0, root[0], 1, totlisan, 0);
    for (int i = 1; i <= n; i++)
        insert(root[i - 1], root[i], 1, totlisan, num[i]);
    for (int i = L; i <= n; i++)
    {
        tot++;
        h[tot].p = i; h[tot].k = 1;
        h[tot].right = i - L;
        h[tot].left = max(i - R, 0);
        h[tot].maxx = num[i] - query(root[h[tot].left - 1], root[h[tot].right], 1, totlisan, 1);
    }
    for (int i = tot / 2; i >= 1; i--)
        pushdown(i);
    for (int i = 1; i <= k; i++)
        solve();
    printf("%lld\n", ans);
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章