HDU5316-Magician

Magician

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


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
 

Author
ZSTU
 

Source
 

Recommend
wange2014
 

题意:有n个精灵,每个精灵有一个能力值,有两种操作①改变某一个精灵的能力值②查询区间[L,R]里面位置奇偶相间的能力值的最大和

解题思路:线段树,每个节点维护四个值即可,①以奇位置开始偶位置结束的奇偶序列最大和②以奇位置开始奇位置结束的奇偶序列最大和③以偶位置开始偶位置结束的奇偶序列最大和④以偶位置开始奇位置结束的奇偶序列最大和



#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>

using namespace std;

#define LL long long
const LL INF = 0x3f3f3f3f3f3f3f3f;

int n,q,p,y;
struct node
{
    LL aa,bb,ab,ba;
    void Clear(){aa=bb=ab=ba=-INF;}
}x[400009],ans;
LL a[100009],z;

void Merge(node &a,node b,node c)
{
    a.aa=max(b.ab+c.aa,b.aa+c.ba);
    a.aa=max(a.aa,max(b.aa,c.aa));
    a.bb=max(b.ba+c.bb,b.bb+c.ab);
    a.bb=max(a.bb,max(b.bb,c.bb));
    a.ab=max(b.aa+c.bb,b.ab+c.ab);
    a.ab=max(a.ab,max(b.ab,c.ab));
    a.ba=max(b.bb+c.aa,b.ba+c.ba);
    a.ba=max(a.ba,max(b.ba,c.ba));
}

void build(int k,int l,int r)
{
    if(l==r)
    {
        x[k].Clear();
        if(l&1) x[k].aa=a[l];
        else x[k].bb=a[l];
        return ;
    }
    int mid=(l+r)>>1;
    build(k<<1,l,mid),build(k<<1|1,mid+1,r);
    Merge(x[k],x[k<<1],x[k<<1|1]);
}

void update(int k,int l,int r,int p,LL val)
{
    if(l==r)
    {
        x[k].Clear();
        if(l&1) x[k].aa=val;
        else x[k].bb=val;
        return ;
    }
    int mid=(l+r)>>1;
    if(p<=mid) update(k<<1,l,mid,p,val);
    else update(k<<1|1,mid+1,r,p,val);
    Merge(x[k],x[k<<1],x[k<<1|1]);
}

void query(int k,int l,int r,int ll,LL rr)
{
    if(l>=ll&&r<=rr) {Merge(ans,ans,x[k]);return ;}
    int mid=(l+r)>>1;
    if(mid>=ll) query(k<<1,l,mid,ll,rr);
    if(rr>mid) query(k<<1|1,mid+1,r,ll,rr);
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&q);
        for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
        build(1,1,n);
        while(q--)
        {
            scanf("%d%d%lld",&p,&y,&z);
            if(p==1) update(1,1,n,y,z);
            else
            {
                ans.Clear();
                query(1,1,n,y,z);
                printf("%lld\n",max(max(ans.aa,ans.bb),max(ans.ab,ans.ba)));
            }
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值