HDU 5316 Magician(2015多校第三场 线段树)

Magician

Time Limit: 18000/9000 MS (Java/Others)    Memory
Limit: 65536/65536 K (Java/Others)

Total Submission(s): 1700    Accepted Submission(s): 503

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

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <algorithm>
#define LL long long
using namespace std;
const int MAXN = 100000 + 10;
const long long INF = 0x3f3f3f3f3f3f3f3fll;
LL a[MAXN], ans;
struct Tree
{
    int l, r;
    LL ee, eo, oe, oo;
}tree[MAXN<<2];
LL max4(LL A, LL B, LL C, LL D)
{
    return max(max(A, B), max(C, D));
}
void push_up(int rt)
{
    tree[rt].ee = max(-INF, max(tree[rt<<1].ee + tree[rt<<1|1].oe, tree[rt<<1].eo + tree[rt<<1|1].ee));
    tree[rt].ee = max(tree[rt].ee, tree[rt<<1].ee);
    tree[rt].ee = max(tree[rt].ee, tree[rt<<1|1].ee);

    tree[rt].eo = max(-INF, max(tree[rt<<1].ee + tree[rt<<1|1].oo, tree[rt<<1].eo + tree[rt<<1|1].eo));
    tree[rt].eo = max(tree[rt].eo, tree[rt<<1].eo);
    tree[rt].eo = max(tree[rt].eo, tree[rt<<1|1].eo);

    tree[rt].oe = max(-INF, max(tree[rt<<1].oo + tree[rt<<1|1].ee, tree[rt<<1].oe + tree[rt<<1|1].oe));
    tree[rt].oe = max(tree[rt].oe, tree[rt<<1].oe);
    tree[rt].oe = max(tree[rt].oe, tree[rt<<1|1].oe);

    tree[rt].oo = max(-INF, max(tree[rt<<1].oe + tree[rt<<1|1].oo, tree[rt<<1].oo + tree[rt<<1|1].eo));
    tree[rt].oo = max(tree[rt].oo, tree[rt<<1].oo);
    tree[rt].oo = max(tree[rt].oo, tree[rt<<1|1].oo);
}
void build(int l, int r, int rt)
{
    tree[rt].l = l; tree[rt].r = r;
    if(l == r)
    {
        if(l & 1)
        {
            tree[rt].oo = a[l];
            tree[rt].eo = tree[rt].ee = tree[rt].oe = -INF;
        }
        else
        {
            tree[rt].ee = a[l];
            tree[rt].eo = tree[rt].oo = tree[rt].oe = -INF;
        }
        return ;
    }
    int m = (l + r) >> 1;
    build(l, m, rt<<1);
    build(m + 1, r, rt<<1|1);
    push_up(rt);
}
void update(int l, int x, int rt)
{
    if(tree[rt].l == l && tree[rt].r == l)
    {
        if(tree[rt].l & 1)
        {
            tree[rt].oo = x;
            tree[rt].oe = tree[rt].ee = tree[rt].eo = -INF;
        }
        else
        {
            tree[rt].ee = x;
            tree[rt].oe = tree[rt].oo = tree[rt].eo = -INF;
        }
        return ;
    }
    if(l <= tree[rt<<1].r) update(l, x, rt<<1);
    else update(l, x, rt<<1|1);
    push_up(rt);
}
Tree query(int l, int r, int rt)
{
    Tree res;
    if(tree[rt].l == l && tree[rt].r == r)
    {
        res.ee = tree[rt].ee;
        res.eo = tree[rt].eo;
        res.oo = tree[rt].oo;
        res.oe = tree[rt].oe;
        return res;
    }
    int m = (tree[rt].l + tree[rt].r) >> 1;
    if(r <= m) return query(l, r, rt<<1);
    else if(l > m) return query(l, r, rt<<1|1);
    else
    {
        Tree t1 = query(l, m, rt<<1);
        Tree t2 = query(m + 1, r, rt<<1|1);
        res.ee = max(max4(-INF, t1.ee + t2.oe, t1.eo + t2.ee, t1.ee), t2.ee);
        res.oo = max(max4(-INF, t1.oe + t2.oo, t1.oo + t2.eo, t1.oo), t2.oo);
        res.eo = max(max4(-INF, t1.ee + t2.oo, t1.eo + t2.eo, t1.eo), t2.eo);
        res.oe = max(max4(-INF, t1.oo + t2.ee, t1.oe + t2.oe, t1.oe), t2.oe);
        return res;
    }
}
int main()
{
    int T, n, m;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d%d", &n, &m);
        for(int i=1;i<=n;i++) scanf("%I64d", &a[i]);
        build(1, n, 1);
        int op, l, r;
        while(m--)
        {
            scanf("%d%d%d", &op, &l, &r);
            if(op == 0)
            {
                ans = -INF;
                Tree res = query(l, r, 1);
                ans = max(ans, max4(res.ee, res.oo, res.eo, res.oe));
                printf("%I64d\n", ans);
            }
            else update(l, r, 1);
        }
    }
    return 0;
}

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

时间: 2024-09-27 14:48:33

HDU 5316 Magician(2015多校第三场 线段树)的相关文章

[Noi2016十连测第三场]线段树

1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <cmath> 6 using namespace std; 7 #define maxn 100005 8 #define maxk 4000005 9 int n,m,q,tot,t1,t2,ans,L[maxn][18],R[maxn][18]; 1

hdu 5316 Magician(2015多校第三场第1题)线段树单点更新+区间合并

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5316 题意:给你n个点,m个操作,每次操作有3个整数t,a,b,t表示操作类型,当t=1时讲a点的值改成b:当t=0时,查询区间a,b之间最大的子序列和,这个子序列中的相邻的元素的原来的下标奇偶性都不同. 思路:这道题难点就在查询,其余都是模板,而根据查询,你只要分别把下一个区间的奇偶最大的情况分别比较,合并到上一个区间这样可以构建一个每个节点存有区间中奇开头偶开头,奇结尾,偶结尾这些区间情况的树.

hdu 5317 RGCDQ (2015多校第三场第2题)素数打表+前缀和相减求后缀(DP)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5317 题意:F(x) 表示x的不同质因子的个数结果是求L,R区间中最大的gcd( F(i) , F(j) ),i.j在L,R区间内. 思路:因为2<=L < R<=1000000,所以他们的质因子最多的个数也就7个,也就是说1<=F(x)<=7,因为要求最大的GCD,所以只要知道在L,R区间内每个F(x)的情况就可以知道结果. 代码: 1 #include <stdio.h

HDU 5355 Cake(2015多校第六场,搜索 + 剪枝)

Cake Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 965    Accepted Submission(s): 119 Special Judge Problem Description There are m soda and today is their birthday. The 1-st soda has prepa

HDU 5371 (2015多校联合训练赛第七场1003)Hotaru&#39;s problem(manacher+二分/枚举)

HDU 5371 题意: 定义一个序列为N序列:这个序列按分作三部分,第一部分与第三部分相同,第一部分与第二部分对称. 现在给你一个长为n(n<10^5)的序列,求出该序列中N序列的最大长度. 思路: 来自官方题解:修正了一些题解错别字(误 先用求回文串的Manacher算法,求出以第i个点为中心的回文串长度,记录到数组p中 要满足题目所要求的内容,需要使得两个相邻的回文串,共享中间的一部分,也就是说,左边的回文串长度的一半,要大于等于共享部分的长度,右边回文串也是一样. 因为我们已经记录下来以

HDU 5371 (2015多校联合训练赛第七场1003)Hotaru&amp;#39;s problem(manacher+二分/枚举)

pid=5371">HDU 5371 题意: 定义一个序列为N序列:这个序列按分作三部分,第一部分与第三部分同样,第一部分与第二部分对称. 如今给你一个长为n(n<10^5)的序列,求出该序列中N序列的最大长度. 思路: 来自官方题解:修正了一些题解错别字(误 先用求回文串的Manacher算法.求出以第i个点为中心的回文串长度.记录到数组p中 要满足题目所要求的内容.须要使得两个相邻的回文串,共享中间的一部分,也就是说.左边的回文串长度的一半,要大于等于共享部分的长度,右边回文串也

hdu 5288||2015多校联合第一场1001题

http://acm.hdu.edu.cn/showproblem.php?pid=5288 Problem Description OO has got a array A of size n ,defined a function f(l,r) represent the number of i (l<=i<=r) , that there's no j(l<=j<=r,j<>i) satisfy ai mod aj=0,now OO want to know ∑i

2014多校第三场1005 || HDU 4891 The Great Pan(模拟)

题目链接 题意 : 给你n行字符串,问你有多少种理解方式.有两大类的理解 (1){A|B|C|D|...}代表着理解方式可以是A,可以是B或C或者D. (2)$blah blah$,在$$这两个符号中间,如果是不连续的空格的那个位置就有2种理解方式,可以理解为没有空格也可以理解为有空格.如果有连续N个空格的位置,那里就有N+1种理解方式. 最后所有的理解方式相乘,数据保证$一定与$匹配,{一定与匹配},不会有任何嵌套,类似{$$}或者{{}}或者${}$这种情况都不会出现,也不会有{$}这种情况

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