hdu 多校

Magician

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


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


简单的线段树区间叠加,维护区间内以奇偶开头/结尾的最大值,注意初始化时全部初始化为-INF, 在更新的时候就会选择最大的进行更新,自动的决定选取或者不选取。


#include<cstdio>
#include<cmath>
#include<stdlib.h>
#include<map>
#include<set>
#include<time.h>
#include<vector>
#include<queue>
#include<string>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define eps 1e-8
#define INF 0x3f3f3f3f
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define lson L, mid, rt<<1
#define rson mid+1, R, rt<<1|1
#define maxn 100000 + 10

long long a[maxn];
int n, m;

struct Node
{
    long long M[2][2];
}T[maxn<<2];

void pushup(int rt)
{
    int l = rt<<1, r = rt<<1|1;
    for(int i=0; i<2; i++)
    for(int j=0; j<2; j++)
    {
        T[rt].M[i][j] = max(max(T[l].M[i][j], T[r].M[i][j]), max(T[l].M[i][0]+T[r].M[1][j], T[l].M[i][1]+T[r].M[0][j]));
    }
}

void build(int L, int R, int rt)
{
    if(L == R)
    {
        scanf("%I64d", &a[L]);
        if(L & 1)
        {
            T[rt].M[1][1] = a[L];
            T[rt].M[0][0] = -INF;
        }
        else
        {
            T[rt].M[0][0] = a[L];
            T[rt].M[1][1] = -INF;
        }
            T[rt].M[1][0] = -INF;
            T[rt].M[0][1] = -INF;
        return ;
    }
    int mid = (L + R) >> 1;
    build(lson);
    build(rson);
    pushup(rt);
}

void update(int t, int v, int L, int R, int rt)
{
    if(L == R)
    {
        if(L & 1)
        {
            T[rt].M[1][1] = v;
        }
        else
        {
            T[rt].M[0][0] = v;
        }
        return ;
    }
    int mid = (L + R) >> 1;
    if(t <= mid) update(t, v, lson);
    else update(t, v, rson);
    pushup(rt);

}

Node query(int l, int r, int L, int R, int rt)
{
    Node tmp;
    if(l == L && r == R)
    {
        tmp = T[rt];
        return tmp;
    }
    int mid = (L + R) >> 1;
    if(r <= mid) return query(l, r, lson);
    else if(l > mid) return query(l, r, rson);
    else
    {
        Node ltmp = query(l, mid, lson);
        Node rtmp = query(mid+1, r, rson);
        for(int i=0; i<2; i++)
        for(int j=0; j<2; j++)
        {
            tmp.M[i][j] = max(max(ltmp.M[i][j], rtmp.M[i][j]), max(ltmp.M[i][0]+rtmp.M[1][j], ltmp.M[i][1]+rtmp.M[0][j]));
        }
        return tmp;
    }
}

int main()
{
    int kase;
    scanf("%d", &kase);
    while(kase--)
    {
        scanf("%d%d", &n, &m);
        build(1, n, 1);

        for(int i=0; i<m; i++)
        {
            int t, a, b;
            scanf("%d%d%d", &t, &a, &b);
            if(t == 0)
            {
                long long ans = -INF;
                Node tmp = query(a, b, 1, n, 1);
                for(int i=0; i<2; i++)
                for(int j=0; j<2; j++)
                {
                    ans = max(ans, tmp.M[i][j]);
                }
                printf("%I64d\n", ans);
            }
            else
            {
                update(a, b, 1, n, 1);
            }
        }
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值