HDU 5316 Magician(线段树区间合并, 子序列最值 多校2015啊)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5316


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.

HDU 5316 Magician(线段树区间合并, 子序列最值 多校2015啊)_第1张图片

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
 

Source
2015 Multi-University Training Contest 3

题意:

给出 n 个点和 m 个操作;

1:1 a b 将 a 点的值改成 b 

2: 0 a b 查询子区间 a 到 b 之间最大的子序列和(这个子序列(可以不连续)中的相邻的元素的原来的下标奇偶性都不同)

PS:

官方题解:

HDU 5316 Magician(线段树区间合并, 子序列最值 多校2015啊)_第2张图片

对于线段树每个节点,我们需要用四个值: 偶始偶终  、 偶始奇终、 奇始奇终、奇始偶终 
来维护四种不同情况的最大值。
最后把区间合并,两个区间:  偶终和奇始 或者  奇终和偶始 都可以合并 。
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define LL long long
#define lson l , mid , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
//lson和rson分辨表示结点的左儿子和右儿子
//rt表示当前子树的根(root),也就是当前所在的结点
const int maxn = 100000+7;
struct node
{
    int has[2][2];//是否有这种情况
    LL val[2][2];//该情况的最大值
} tre[maxn<<2];

int max(int x,int y)
{
    if(x > y)
        return x;
    return y;
}

//合并
node UNION(node a,node b)
{
    node c;
    //四种起始和终止情况可以直接继承于左儿子或右儿子的对应情况
    for(int i = 0; i <= 1; i++)
    {
        for(int j = 0; j <= 1; j++)
        {
            c.has[i][j] = a.has[i][j] + b.has[i][j];
            if(a.has[i][j] && b.has[i][j])
                c.val[i][j] = max(a.val[i][j], b.val[i][j]);
            else if(a.has[i][j])
                c.val[i][j] = a.val[i][j];
            else if(b.has[i][j])
                c.val[i][j] = b.val[i][j];
        }
    }

    //四种情况由左儿子和右儿子的情况合并。
    //如奇始奇终可以由左儿子的奇始奇终和右儿子的偶始奇终合并
    //也可以由左儿子的奇始偶终和右儿子的奇始奇终合并,,以此类推
    for(int i = 0; i <= 1; i++)
    {
        for(int j = 0; j <= 1; j++)
        {
            for(int k = 0; k <= 1; k++)
            {
                if(a.has[i][j] && b.has[!j][k])
                {
                    if(c.has[i][k])
                    {
                        c.val[i][k] = max(c.val[i][k], a.val[i][j]+b.val[!j][k]);
                    }
                    else
                    {
                        c.has[i][k] = 1;
                        c.val[i][k]=a.val[i][j]+b.val[!j][k];
                    }
                }
            }
        }
    }
    return c;
}

void PushUP(int rt)
{
    tre[rt] = UNION(tre[rt<<1],tre[rt<<1|1]);
}

void build(int l,int r,int rt)
{
    memset(tre[rt].has,0,sizeof(tre[rt].has));
    if (l == r)
    {
        int tt;
        scanf("%d",&tt);
        tre[rt].has[l%2][l%2] = 1;
        tre[rt].val[l%2][l%2] = tt;
        return ;
    }
    int mid = (l + r) >> 1;
    build(lson);
    build(rson);
    PushUP(rt);
}
void update(int p,int sc,int l,int r,int rt)
{
    if (l == r) //叶节点
    {
        memset(tre[rt].has,0,sizeof(tre[rt].has));
        tre[rt].has[l%2][l%2] = 1;
        tre[rt].val[l%2][l%2] = sc;
        return ;
    }
    int mid = (l + r) >> 1;
    if (p <= mid)//递归更新左子树或者右子树
        update(p , sc , lson);
    else
        update(p , sc , rson);
    PushUP(rt);
}
node query(int L,int R,int l,int r,int rt)
{
    if (L <= l && r <= R)
    {
        return tre[rt];
    }
    int flag1 = 0, flag2 = 0;
    node ans1, ans2;
    int mid = (l + r) >> 1;
    if (L <= mid) //往左走
    {
        ans1 = query(L , R , lson);
        flag1 = 1;
    }
    if (mid < R)//往右走
    {
        ans2 = query(L , R , rson);
        flag2 = 1;
    }
    if(!flag1)
    {
        return ans2;
    }
    if(!flag2)
    {
        return ans1;
    }
    return UNION(ans1,ans2);
}
int main()
{
    int t;
    int N , M;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&N,&M);
        build(1 , N , 1); //建树
        while(M--)
        {
            int op, a , b;
            scanf("%d%d%d",&op,&a,&b);
            if(op == 1)
            {
                update(a , b , 1 , N , 1);
            }
            else if (op == 0)
            {
                node t = query(a , b , 1 , N , 1);
                LL ans;
                int flag = 0;
                for(int i = 0; i <= 1; i++)
                {
                    for(int j = 0; j <= 1; j++)
                    {
                        if(t.has[i][j])
                        {
                            if(flag == 0)
                            {
                                ans = t.val[i][j];
                                flag=1;
                            }
                            else
                                ans = max(ans, t.val[i][j]);
                        }
                    }
                }
                printf("%lld\n",ans);
            }
        }
    }
    return 0;
}



你可能感兴趣的:(线段树,HDU,区间合并,多校2015)