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.

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:

官方题解:

对于线段树每个节点,我们需要用四个值:偶始偶终 、偶始奇终、奇始奇终、奇始偶终

来维护四种不同情况的最大值。

最后把区间合并,两个区间: 偶终和奇始 或者 奇终和偶始
都可以合并 。

代码如下:

#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;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-02 06:09:17

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

HDU 3308 LCIS (线段树区间合并)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3308 题目很好懂,就是单点更新,然后求区间的最长上升子序列. 线段树区间合并问题,注意合并的条件是a[mid + 1] > a[mid],写的细心点就好了. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 const int MAXN = 1

hdu5316 Magician(线段树区间合并)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5316 题意:有n个精灵,每个精灵有一个能力值,有两种操作①改变某一个精灵的能力值②查询区间[L,R]里面位置奇偶相间的能力值的最大和. 分析:这题线段树区间合并可以做.每个节点保存4个信息:①以奇位置开始偶位置结束的奇偶序列最大和②以奇位置开始奇位置结束的奇偶序列最大和③以偶位置开始偶位置结束的奇偶序列最大和④以偶位置开始奇位置结束的奇偶序列最大和 合并的时候,以奇位置开始偶位置结束的奇偶序列最大和=m

hdu 5316 Magician (线段树)

题目链接: hdu 5316 Magician 题目描述: 有n个精灵,每个精灵都有一个魔法值,现在有两个操作: (0, a, b)查询[a, b]序列中的一个完美序列的最大和,完美序列就是数组中相邻数字的下标奇偶性不同. (1, a, b)更新下标为a的精灵魔法值为b. 对的,没错就是这个意思,但是比赛的时候就是题意卡的死死的有没有啊,就是各种不懂有没有啊!!! 1 #include <cstdio> 2 #include <cstring> 3 #include <ios

HDU 1540(线段树+区间合并)学习记录

学习了线段树的新姿势,记录一下 参考blog:https://blog.csdn.net/sunyutian1998/article/details/79618316 query的时候m-ql+1和qr-m写成了m-l+1.r-m,wa了几发之后才找到bug 错误样例: 10 1Q 5 wrong answer:5 顺带一提,这个题目可以在set上面二分直接过,不过最主要还是为了练习区间合并 #include<bits/stdc++.h> using namespace std; const

hdu 1540(线段树区间合并)

题目链接:传送门 参考文章:传送门 题意:n个数字初始连在一条线上,有三种操作, D x表示x号被摧毁: R 表示恢复剩下的通路 Q表示查询标号为x所在的串的最长长度. 思路:线段树的区间合并. #include<iostream> #include<cstdio> #include<cstring> using namespace std; const int maxn = 50500; struct Node{ int l,r; int ls,rs,ms; }cur

HDU 3308 LCIS(线段树区间合并)

Problem Description Given n integers. You have two operations: U A B: replace the Ath number by B. (index counting from 0) Q A B: output the length of the longest consecutive increasing subsequence (LCIS) in [a, b]. Input T in the first line, indicat

HDU 5316 Magician(线段树区间合并入门)

本文纯属原创,转载请注明出处谢谢.http://blog.csdn.net/zip_fan. 题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5316 Time Limit: 18000/9000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Description Fantasy magicians usually gain their ability

HDU 3308 LCIS(最长连续上升子序列)(线段树区间合并)

题意:给你n个整数,有两种操作,U A B把第A个数变成B,Q A B查询区间[A,B]的最长连续上升序列. 思路:还是查询和更新操作,而且也是询问区间中满足条件的连续最长区间 ,所以是线段树区间合并类型的题,通法是开三棵线段树,一个记录此区间内的LCIS的最长长度,一个记录从左边第一个数开始的LCIS长度,另一个记录从右边最后一个数结尾的LCIS长度.然后试图找到父亲与儿子关系维护的递推关系式就好 本题中的递推关系是: 1. 左儿子最右边的值 < 右儿子最左边的值 lmx = (左儿子的lmx

hdu 3308(线段树区间合并)

LCIS Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6069    Accepted Submission(s): 2635 Problem Description Given n integers.You have two operations:U A B: replace the Ath number by B. (index