HDOJ 5316 Magician【线段树 单点更新 区间查询】

Magician

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


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

送一组样例:
5
5 5
-3 5 -2 4 1
0 2 5
1 3 2
0 2 5
1 3 -8
0 2 5

精灵有能量值 魔术师可以改变某个精灵的能量值 然后问 beautiful subsequence 的最大值  也就是说 这样的子序列 相邻元素编号的奇偶性不同 线段树记录 奇开头奇结尾 奇开头偶结尾 偶开头奇结尾 偶开头偶结尾 


#include <iostream>
#include<cstdio>
#include<cstring>
#define maxn 100010
#define inm -1e12
using namespace std;
typedef long long LL;
LL r[maxn];
struct node
{
    int l,r;
    LL oo,eo,oe,ee;
};
node tree[maxn<<2];
void pushup(int o)
{
    node ls=tree[o<<1];
    node rs=tree[o<<1|1];
    tree[o].oo=max(max(ls.oo,rs.oo),max(ls.oo+rs.eo,ls.oe+rs.oo));
    tree[o].oe=max(max(ls.oe,rs.oe),max(ls.oo+rs.ee,ls.oe+rs.oe));
    tree[o].eo=max(max(ls.eo,rs.eo),max(ls.eo+rs.eo,ls.ee+rs.oo));
    tree[o].ee=max(max(ls.ee,rs.ee),max(ls.ee+rs.oe,ls.eo+rs.ee));
}
void build(int o,int l,int r)
{
    tree[o].l=l;
    tree[o].r=r;
    tree[o].oo=tree[o].ee=tree[o].oe=tree[o].eo=inm;
    if(l==r)
        return ;
    int mid=(l+r)>>1;
    build(o<<1,l,mid);
    build(o<<1|1,mid+1,r);
    pushup(o);
}
void update(int o,int p,int v)
{
    if(tree[o].l==p&&tree[o].r==p)
    {
        if(p&1)
            tree[o].oo=v;
        else
            tree[o].ee=v;
        return ;
    }
    int mid=(tree[o].l+tree[o].r)>>1;
    if(p<=mid)
        update(o<<1,p,v);
    else
        update(o<<1|1,p,v);
    pushup(o);
}
node query(int o,int l,int r)
{
    if(tree[o].l==l&&tree[o].r==r)
        return tree[o];
    int mid=(tree[o].l+tree[o].r)>>1;
    if(l>mid)
        return query(o<<1|1,l,r);
    else if(r<=mid)
        return query(o<<1,l,r);
    else//开始这里处理不当 之后这里借鉴了别人的方法 人家用这种方法写 pushup 还是没这样写 但到这里才发现人家这样写的方便之处
    {
        node res;
        res.l=l,res.r=r;
        node ls=query(o<<1,l,mid);
        node rs=query(o<<1|1,mid+1,r);
        res.oo=max(max(ls.oo,rs.oo),max(ls.oo+rs.eo,ls.oe+rs.oo));
        res.oe=max(max(ls.oe,rs.oe),max(ls.oo+rs.ee,ls.oe+rs.oe));
        res.eo=max(max(ls.eo,rs.eo),max(ls.eo+rs.eo,ls.ee+rs.oo));
        res.ee=max(max(ls.ee,rs.ee),max(ls.ee+rs.oe,ls.eo+rs.ee));
        return res;
    }
}
int main()
{
    int t,n,m,op,a,b;
    LL v;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        build(1,1,n);
        for(int i=1;i<=n;++i)
        {
            scanf("%d",&v);
            update(1,i,v);
        }
        while(m--)
        {
            scanf("%d%d%d",&op,&a,&b);
            if(op)
                update(1,a,b);
            else
            {
                node res=query(1,a,b);
                LL ans=max(max(res.oo,res.ee),max(res.oe,res.eo));
                printf("%lld\n",ans);
            }
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值