[HDU:5316]Magician

来源:互联网 发布:夏亚大魔数据 编辑:程序博客网 时间:2024/05/16 08:51

Magician

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


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
11 110 1 1
 

Sample Output
1
 

Source
2015 Multi-University Training Contest 3 
 

Recommend
wange2014   |   We have carefully selected several similar problems for you:  5326 5325 5324 5323 5322 
 


http://acm.hdu.edu.cn/showproblem.php?pid=5316


解答:

这题明显是一个单点更新,区段询问的一维线段树,由于询问的内容比较特殊,所以维护的量比较特别,一时间不太容易想到。

在每个节点维护奇数位开始奇数位结束的,奇数位开始偶数位结束,偶数位开始奇数位结束,偶数位开始偶数位结束的子序列的最大和(sum[4])。

在茎节点通过子结点信息维护本节点信息时,可以根据子结点的四个最大和来拼接出本节点子序列的四个最大和。根据奇偶交错的原理,每个最大和有两种拼接方式。也可以直接继承子节点的最大和,在这些和中取最大值。

要注意的一个是初始化,要避免全负数的状况由于未初始化输出0;还有一个是SUM可能超过int范围,还有HDU输出的时候要用lld(我也不很清楚原因)

 

代码:

#define LL long long#include<iostream>#include<cstdio>#include<cstring>//#include"TestTool.h"using namespace std;const int maxn = 100050;const LL INF = 100000000000000000LL;LL lib[maxn];//11 12 21 22//SUM的LongLong很重要LL ll[maxn<<2],rr[maxn<<2],sum[maxn<<2][4];LL max(LL a,LL b){    if(a>b) return a;    return b;}void build(LL l,LL r,LL i){    //其他sum的-INF值    ll[i]=l;rr[i]=r;    if(l==r){        if(l&1){            sum[i][0]=lib[l];            sum[i][1]=sum[i][2]=sum[i][3]=-INF;        }        else{            sum[i][3]=lib[l];            sum[i][0]=sum[i][1]=sum[i][2]=-INF;        }        return;    }    LL m=(l+r)>>1,ls=i<<1,rs=ls|1;    build(l,m,ls);    build(m+1,r,rs);    //还要计入序列不合并的状况    sum[i][0]=max(max(sum[ls][0]+sum[rs][2],sum[ls][1]+sum[rs][0]),max(sum[ls][0],sum[rs][0]));    sum[i][1]=max(max(sum[ls][0]+sum[rs][3],sum[ls][1]+sum[rs][1]),max(sum[ls][1],sum[rs][1]));    sum[i][2]=max(max(sum[ls][2]+sum[rs][2],sum[ls][3]+sum[rs][0]),max(sum[ls][2],sum[rs][2]));    sum[i][3]=max(max(sum[ls][2]+sum[rs][3],sum[ls][3]+sum[rs][1]),max(sum[ls][3],sum[rs][3]));}//Modify the kth element to v, use i as the root No.void update(LL k,LL v,LL i){    if(ll[i]==rr[i]){        if(ll[i]&1)            sum[i][0]=v;        else            sum[i][3]=v;        return;    }    LL m=(ll[i]+rr[i])>>1,ls=i<<1,rs=ls|1;    if(k<=m)        update(k,v,ls);    else update(k,v,rs);    sum[i][0]=max(max(sum[ls][0]+sum[rs][2],sum[ls][1]+sum[rs][0]),max(sum[ls][0],sum[rs][0]));    sum[i][1]=max(max(sum[ls][0]+sum[rs][3],sum[ls][1]+sum[rs][1]),max(sum[ls][1],sum[rs][1]));    sum[i][2]=max(max(sum[ls][2]+sum[rs][2],sum[ls][3]+sum[rs][0]),max(sum[ls][2],sum[rs][2]));    sum[i][3]=max(max(sum[ls][2]+sum[rs][3],sum[ls][3]+sum[rs][1]),max(sum[ls][3],sum[rs][3]));}struct BOX{    LL sss[4];    BOX(){}    BOX(LL *s){        for(int i=0;i<4;i++)            sss[i]=s[i];    }    LL mx(){        LL t=sss[0];        for(int i=1;i<4;i++)            if(sss[i]>t)                t=sss[i];        return t;    }};BOX fi(LL l,LL r,LL i){    if(ll[i]==l&&rr[i]==r)        return BOX(sum[i]);    LL m=(ll[i]+rr[i])>>1,ls=i<<1,rs=ls|1;    if(r<=m)        return fi(l,r,ls);    else if(l>m)        return fi(l,r,rs);    BOX l1,l2;    l1=fi(l,m,ls);    l2=fi(m+1,r,rs);    LL ss[4];    ss[0]=max(max(l1.sss[0]+l2.sss[2],l1.sss[1]+l2.sss[0]),max(l1.sss[0],l2.sss[0]));    ss[1]=max(max(l1.sss[0]+l2.sss[3],l1.sss[1]+l2.sss[1]),max(l1.sss[1],l2.sss[1]));    ss[2]=max(max(l1.sss[2]+l2.sss[2],l1.sss[3]+l2.sss[0]),max(l1.sss[2],l2.sss[2]));    ss[3]=max(max(l1.sss[2]+l2.sss[3],l1.sss[3]+l2.sss[1]),max(l1.sss[3],l2.sss[3]));    return BOX(ss);}int main(){    int T;    scanf("%d",&T);    while(T--){        int n,m;        scanf("%d%d",&n,&m);        for(int i=1;i<=n;i++)            scanf("%lld",&lib[i]);        build(1,n,1);        LL t,a,b;        for(int i=0;i<m;i++){            scanf("%lld%lld%lld",&t,&a,&b);            if(t==0)                printf("%lld\n",fi(a,b,1).mx());            else if(t==1) update(a,b,1);        }    }}


0 0
原创粉丝点击