[BZOJ2006][NOI2010][RMQ/主席树][二叉堆]超级钢琴

来源:互联网 发布:剑三萝莉捏脸数据南风 编辑:程序博客网 时间:2024/04/29 22:22
[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);}


0 0
原创粉丝点击