hdu 5316 Magician

Magician

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


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

题意:
定义漂亮子序列:序列相邻的两个数下标的奇偶性不同
现在给你一组数列,m次操作
0 代表查询区间内最大的漂亮子序列
1 代表更改某个数

分析:单点更新,区间查找,可以考虑线段树,区间查找最大组合序列,线段树区间合并;

对于每个不连续的序列,序列尾部和另一个序列的头部下标不同可以合并
奇与偶只有两种状态,用0与1表示奇偶,则表示了序列的四种状态
接下来只需要区间合并取最大值即可。


#include<cstring>
#include<string>
#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<map>
#include<cstdlib>
#include<cmath>
#include<vector>

using namespace std;

#define INF 0x3f3f3f3f

struct node
{
    int l,r;
    int exist[2][2];
    long long maxi[2][2];
} T[400005];

node Merge(node A,node B)
{
    node temp;
    temp.l=A.l;
    temp.r=B.r;
    for(int i=0; i<2; i++)
        for(int j=0; j<2; j++)
        {
            temp.exist[i][j]=(A.exist[i][j]|B.exist[i][j]);
            if(A.exist[i][j]&&B.exist[i][j])
            {
                temp.maxi[i][j]=max(A.maxi[i][j],B.maxi[i][j]);
            }
            else if(A.exist[i][j])
            {
                temp.maxi[i][j]=A.maxi[i][j];
            }
            else if(B.exist[i][j])
            {
                temp.maxi[i][j]=B.maxi[i][j];
            }
        }

    for(int i=0; i<2; i++)
    {
        for(int j=0; j<2; j++)
        {
            for(int k=0; k<2; k++)
                if(A.exist[i][j]&&B.exist[!j][k])
                {
                    if(temp.exist[i][k])
                    {
                        temp.maxi[i][k]=max(A.maxi[i][j]+B.maxi[!j][k],temp.maxi[i][k]);
                    }
                    else temp.maxi[i][k]=A.maxi[i][j]+B.maxi[!j][k],temp.exist[i][k]=1;
                }
        }
    }
    return temp;
}

void init(int l,int r,int k)
{
    memset(T[k].exist,0,sizeof T[k].exist);
    T[k].l=l;
    T[k].r=r;
    if(l==r)
    {
        int temp;
        scanf("%d",&temp);
        if(l%2) T[k].exist[1][1]=1,T[k].maxi[1][1]=temp;
        else  T[k].exist[0][0]=1,T[k].maxi[0][0]=temp;
        return ;
    }

    int mid=(l+r)>>1;
    init(l,mid,2*k);
    init(mid+1,r,2*k+1);

    T[k]=Merge(T[2*k],T[2*k+1]);
}

void Insert(int d,int l,int r,int k)
{
    if(T[k].l==T[k].r)
    {
        memset(T[k].exist,0,sizeof T[k].exist);//此处一定要更新存在情况
        if(l%2) T[k].exist[1][1]=1,T[k].maxi[1][1]=d;
        else  T[k].exist[0][0]=1,T[k].maxi[0][0]=d;
        return ;
    }

    int mid=(T[k].l+T[k].r)>>1;

    if(r<=mid) Insert(d,l,r,2*k);
    else if(mid<l) Insert(d,l,r,2*k+1);
    else
    {
        Insert(d,l,mid,2*k);
        Insert(d,mid+1,r,2*k+1);
    }
    T[k]=Merge(T[2*k],T[2*k+1]);
}

node Find(int l,int r,int k)
{
    if(T[k].l==l&&T[k].r==r)
    {
        return T[k];
    }

    int mid=(T[k].l+T[k].r)>>1;

    if(r<=mid)  return Find(l,r,2*k);
    else if(l>mid) return Find(l,r,2*k+1);
    else return Merge(Find(l,mid,2*k),Find(mid+1,r,2*k+1));
}

int main()
{
    int T_T;
    scanf("%d",&T_T);

    int n,m;
    while(T_T--)
    {
        scanf("%d%d",&n,&m);
        init(1,n,1);

        for(int i=0; i<m; i++)
        {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            if(a)
            {
                Insert(c,b,b,1);
            }
            else
            {
                node t=Find(b,c,1);
                long long ans=0;
                int flag=0;

                for(int i=0; i<2; i++)
                    for(int j=0; j<2; j++)
                    {
                        if(t.exist[i][j])
                        {
                            if(!flag) ans=t.maxi[i][j],flag=1;
                            else ans=max(t.maxi[i][j],ans);
                        }
                    }

                printf("%I64d\n",ans);
            }
        }
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值