HDU 5316 Magician(2015 Multi-University Training Contest 3 1001)

题面:

Magician

Time Limit: 18000/9000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 696    Accepted Submission(s): 192


Problem Description
Fantasy magicians usually gain their ability through one of three usual methods: possessing it as an innate talent, gaining it through study and practice, or receiving it from another being, often a god, spirit, or demon of some sort. Some wizards are depicted as having a special gift which sets them apart from the vast majority of characters in fantasy worlds who are unable to learn magic.

Magicians, sorcerers, wizards, magi, and practitioners of magic by other titles have appeared in myths, folktales, and literature throughout recorded history, with fantasy works drawing from this background.

In medieval chivalric romance, the wizard often appears as a wise old man and acts as a mentor, with Merlin from the King Arthur stories representing a prime example. Other magicians can appear as villains, hostile to the hero.

Mr. Zstu is a magician, he has many elves like dobby, each of which has a magic power (maybe negative). One day, Mr. Zstu want to test his ability of doing some magic. He made the elves stand in a straight line, from position 1 to position n, and he used two kinds of magic, Change magic and Query Magic, the first is to change an elf’s power, the second is get the maximum sum of beautiful subsequence of a given interval. A beautiful subsequence is a subsequence that all the adjacent pairs of elves in the sequence have a different parity of position. Can you do the same thing as Mr. Zstu ?

 

Input
The first line is an integer T represent the number of test cases.
Each of the test case begins with two integers n, m represent the number of elves and the number of time that Mr. Zstu used his magic.
(n,m <= 100000)
The next line has n integers represent elves’ magic power, magic power is between -1000000000 and 1000000000.
Followed m lines, each line has three integers like 
type a b describe a magic.
If type equals 0, you should output the maximum sum of beautiful subsequence of interval [a,b].(1 <= a <= b <= n)
If type equals 1, you should change the magic power of the elf at position a to b.(1 <= a <= n, 1 <= b <= 1e9)
 

Output
For each 0 type query, output the corresponding answer.
 

Sample Input
  
  
1 1 1 1 0 1 1
 

Sample Output
  
  
1
 
题意:有n个数组成的串以及m个操作。第一种操作是把a位置的数修改成b,第二种操作是查询[a, b]区间内某种子序列的和的最大值。这种子序列满足:相邻两个在子序列内的元素在原串中位置的奇偶性不同。
很容易想到线段树做,记录每个子区间中偶数位置开头偶数位置结尾/奇数开头偶数结尾/偶数开头奇数结尾/奇数开头奇数结尾的子序列的和的最大值,两个子区间讨论奇偶性可以合并到整个区间。

//然而队友一开始理解偏了题意,交上各种OLE……但是程序的输出部分没有循环或者递归,把输出语句去掉之后交上是WA……一直在奇怪是不是有什么诡异的bug导致输出了奇怪的东西。
比完之后用近似于恶意提交代码的方式测试了一下HDU的最大输出大小,在8225KB左右。(至少这个题是这样……)标程的输出在7400KB左右。由于线段树写挫了,输出的很多结果都明显大于正解,位数也比较多。赛后测了一下OLE的程序,输出了10000KB左右的数据……难怪会OLE。被狠狠坑过一次之后总算涨了一点姿势。

贴代码:


#include <iostream>
#include <cstdio>
using namespace std;
typedef long long int LL;
#define N 1000100
const LL inf = 10000000000ll;
int a[N];
class node{
public:
    LL a00, a01, a10, a11;
};
LL max(LL a, LL b){
    return a > b ? a : b;
}
class segtree{
public:
    node t[N * 4];

    node merge(node l, node r){
        node ans;
        ans.a01 = max(l.a00 + r.a11, l.a01 + r.a01);
        ans.a01 = max(ans.a01, l.a01); ans.a01 = max(ans.a01, r.a01);

        ans.a00 = max(l.a00 + r.a10, l.a01 + r.a00);
        ans.a00 = max(ans.a00, l.a00); ans.a00 = max(ans.a00, r.a00);

        ans.a10 = max(l.a10 + r.a10, l.a11 + r.a00);
        ans.a10 = max(ans.a10, l.a10); ans.a10 = max(ans.a10, r.a10);

        ans.a11 = max(l.a10 + r.a11, l.a11 + r.a01);
        ans.a11 = max(ans.a11, l.a11); ans.a11 = max(ans.a11, r.a11);
        return ans;
    }

    void pushup(int rt){
        t[rt] = merge(t[rt << 1], t[(rt << 1) | 1]);
    }

    void build(int l, int r, int rt){
        if (l == r){
            t[rt].a00 = t[rt].a01 = t[rt].a10 = t[rt].a11 = -inf;
            if (l & 1)
                t[rt].a11 = a[l];
            else
                t[rt].a00 = a[l];
        }
        else {
            int m = (l + r) >> 1;
            build(l, m, rt << 1);
            build(m + 1, r, (rt << 1) | 1);
            pushup(rt);
        }
    }

    void change(int l, int r, int rt, int p, int x){
        if (l == r){
            t[rt].a00 = t[rt].a01 = t[rt].a10 = t[rt].a11 = -inf;
            if (p & 1)
                t[rt].a11 = x;
            else
                t[rt].a00 = x;
        }
        else{
            int m = (l + r) >> 1;
            if (p <= m)
                change(l, m, rt << 1, p, x);
            else
                change(m + 1, r, (rt << 1) | 1, p, x);
            pushup(rt);
        }
    }

    node query(int rl, int rr, int l, int r, int rt){
        if (l >= rl&&r <= rr)
            return t[rt];
        int m = (l + r) >> 1;
        if (rr <= m)
            return query(rl, rr, l, m, rt << 1);
        else if (rl > m)
            return query(rl, rr, m + 1, r, (rt << 1) | 1);
        else
            return merge(query(rl, rr, l, m, rt << 1), query(rl, rr, m + 1, r, (rt << 1) | 1));
    }
}st;

int main()
{
    LL t, n, m;
    scanf("%I64d", &t);
    for (long long int i = 0; i<t; i++)
    {
        scanf("%I64d%I64d", &n, &m);
        const long long int mm = m;
        for (long long int j = 1; j <= n; j++)
        {
            scanf("%I64d", a + j);
        }
        st.build(1, n, 1);
        for (long long int j = 1; j <= mm; j++)
        {
            long long int x, y, z;
            scanf("%I64d%I64d%I64d", &x, &y, &z);
            if (x == 0)
            {
                node tmp = st.query(y, z, 1, n, 1);
                LL ans = max(max(tmp.a00, tmp.a01), max(tmp.a10, tmp.a11));
                cout << ans << endl;
            }
            else
            {
                st.change(1, n, 1, y, z);
            }
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值