HDU 5316 Magician(线段树)

来源:互联网 发布:js实现加载的图片转动 编辑:程序博客网 时间:2024/06/09 19:10

Magician

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


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 


/*题意:在一个区间取数,取完的数按标号大小排序,如果标号为奇偶交替则为满足条件的序列,求满足条件序列的数的和的最大值思路:线段树 ,a[0][0]代表这个偶开头,偶结尾,0,1,代表偶开头奇结尾                 1  0         奇      偶      1  0     奇    偶转化方程  在pushup 函数中,具体看该函数注释非常可惜的地方是 比赛没有做出来,因为我的 query函数返回的是 ___int64 一直超时, 后来看别人博客,发现别人返回结构体,*/#pragma comment(linker, "/STACK:1024000000,1024000000")#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<string>#include<iostream>#include<queue>#include<cmath>#include<map>#include<stack>#include<bitset>using namespace std;#define L(x) (x<<1)#define R(x) (x<<1|1)#define MID(x,y) ((x+y)>>1)typedef long long ll;#define bug printf("hi\n")#define INF -1000000005#define N 100005inline ll Max(ll x,ll y){    return x>y?x:y;}struct stud{  int le,ri;  ll a[2][2];  stud operator =(stud b)  {      for(int i=0;i<2;i++)        for(int j=0;j<2;j++)           b.a[i][j]=a[i][j];      return *this;  }}f[N*4];ll a[N];int n,m;void pushup(int pos){   f[pos].a[0][0]=Max(f[L(pos)].a[0][0],f[R(pos)].a[0][0]); //左端点,右端点取较大   f[pos].a[0][0]=Max(f[pos].a[0][0],f[L(pos)].a[0][0]+f[R(pos)].a[1][0]);//应该挺好理解   f[pos].a[0][0]=Max(f[pos].a[0][0],f[L(pos)].a[0][1]+f[R(pos)].a[0][0]);   f[pos].a[0][1]=Max(f[L(pos)].a[0][1],f[R(pos)].a[0][1]);   f[pos].a[0][1]=Max(f[pos].a[0][1],f[L(pos)].a[0][0]+f[R(pos)].a[1][1]);   f[pos].a[0][1]=Max(f[pos].a[0][1],f[L(pos)].a[0][1]+f[R(pos)].a[0][1]);   f[pos].a[1][0]=Max(f[L(pos)].a[1][0],f[R(pos)].a[1][0]);   f[pos].a[1][0]=Max(f[pos].a[1][0],f[L(pos)].a[1][0]+f[R(pos)].a[1][0]);   f[pos].a[1][0]=Max(f[pos].a[1][0],f[L(pos)].a[1][1]+f[R(pos)].a[0][0]);   f[pos].a[1][1]=Max(f[L(pos)].a[1][1],f[R(pos)].a[1][1]);   f[pos].a[1][1]=Max(f[pos].a[1][1],f[L(pos)].a[1][0]+f[R(pos)].a[1][1]);   f[pos].a[1][1]=Max(f[pos].a[1][1],f[L(pos)].a[1][1]+f[R(pos)].a[0][1]);}void update(int pos,int le,int ri){    if(f[pos].le==le&&f[pos].ri==le)    {        if(le&1)        {            f[pos].a[1][1]=ri;            f[pos].a[0][0]=INF;            f[pos].a[0][1]=f[pos].a[1][0]=INF;        }        else        {            f[pos].a[0][0]=ri;            f[pos].a[0][1]=f[pos].a[1][0]=f[pos].a[1][1]=INF;        }        return ;    }    int mid=MID(f[pos].le,f[pos].ri);    if(mid>=le)        update(L(pos),le,ri);    else        update(R(pos),le,ri);   pushup(pos);}stud query(int pos,int le,int ri){    if(f[pos].le==le&&f[pos].ri==ri)    {       return f[pos];    }    int mid=MID(f[pos].le,f[pos].ri);    if(mid>=ri)        return query(L(pos),le,ri);    else        if(mid<le)        return query(R(pos),le,ri);    else    {        stud te=query(L(pos),le,mid);        stud he=query(R(pos),mid+1,ri);        stud ans;        ans.a[0][0]=Max(Max(Max(te.a[0][0],he.a[0][0]),te.a[0][1]+he.a[0][0]),te.a[0][0]+he.a[1][0]);        ans.a[0][1]=Max(Max(Max(te.a[0][1],he.a[0][1]),te.a[0][1]+he.a[0][1]),te.a[0][0]+he.a[1][1]);        ans.a[1][0]=Max(Max(Max(te.a[1][0],he.a[1][0]),te.a[1][1]+he.a[0][0]),te.a[1][0]+he.a[1][0]);        ans.a[1][1]=Max(Max(Max(te.a[1][1],he.a[1][1]),te.a[1][1]+he.a[0][1]),te.a[1][0]+he.a[1][1]);        return ans;    }}void build(int pos,int le,int ri){    f[pos].le=le;    f[pos].ri=ri;    if(le==ri)    {        if(le&1)        {            f[pos].a[1][1]=a[le];            f[pos].a[0][0]=INF;            f[pos].a[0][1]=f[pos].a[1][0]=INF;        }        else        {            f[pos].a[0][0]=a[le];            f[pos].a[0][1]=f[pos].a[1][0]=f[pos].a[1][1]=INF;        }        return ;    }    int mid=MID(le,ri);    build(L(pos),le,mid);    build(R(pos),mid+1,ri);    pushup(pos);}int main(){    int i,j,t;    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&n,&m);        for(i=1;i<=n;i++)            scanf("%lld",&a[i]);        build(1,1,n);        int op,le,ri;        while(m--)        {            scanf("%d%d%d",&op,&le,&ri);            if(op==0)            {                stud temp=query(1,le,ri);                ll ans=temp.a[0][0];                for(i=0;i<2;i++)                    for(j=0;j<2;j++)                      ans=Max(ans,temp.a[i][j]);                printf("%lld\n",ans);            }            else            {                update(1,le,ri);            }        }    }    return 0;}/*55 3-6 -8 3 4 50 1 51 4 -20 1 5*/

/*超时代码*/#pragma comment(linker, "/STACK:1024000000,1024000000")#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<string>#include<iostream>#include<queue>#include<cmath>#include<map>#include<stack>#include<bitset>using namespace std;#define L(x) (x<<1)#define R(x) (x<<1|1)#define MID(x,y) ((x+y)>>1)typedef long long ll;#define bug printf("hi\n")#define INF -1000000005#define N 100005inline ll Max(ll x,ll y){    return x>y?x:y;}struct stud{  int le,ri;  ll a[2][2];}f[N*4];ll a[N];int n,m;void pushup(int pos){   f[pos].a[0][0]=Max(f[L(pos)].a[0][0],f[R(pos)].a[0][0]);   f[pos].a[0][0]=Max(f[pos].a[0][0],f[L(pos)].a[0][0]+f[R(pos)].a[1][0]);   f[pos].a[0][0]=Max(f[pos].a[0][0],f[L(pos)].a[0][1]+f[R(pos)].a[0][0]);   f[pos].a[0][1]=Max(f[L(pos)].a[0][1],f[R(pos)].a[0][1]);   f[pos].a[0][1]=Max(f[pos].a[0][1],f[L(pos)].a[0][0]+f[R(pos)].a[1][1]);   f[pos].a[0][1]=Max(f[pos].a[0][1],f[L(pos)].a[0][1]+f[R(pos)].a[0][1]);   f[pos].a[1][0]=Max(f[L(pos)].a[1][0],f[R(pos)].a[1][0]);   f[pos].a[1][0]=Max(f[pos].a[1][0],f[L(pos)].a[1][0]+f[R(pos)].a[1][0]);   f[pos].a[1][0]=Max(f[pos].a[1][0],f[L(pos)].a[1][1]+f[R(pos)].a[0][0]);   f[pos].a[1][1]=Max(f[L(pos)].a[1][1],f[R(pos)].a[1][1]);   f[pos].a[1][1]=Max(f[pos].a[1][1],f[L(pos)].a[1][0]+f[R(pos)].a[1][1]);   f[pos].a[1][1]=Max(f[pos].a[1][1],f[L(pos)].a[1][1]+f[R(pos)].a[0][1]);}void update(int pos,int le,int ri){    if(f[pos].le==le&&f[pos].ri==le)    {        if(le&1)        {            f[pos].a[1][1]=ri;            f[pos].a[0][0]=INF;            f[pos].a[0][1]=f[pos].a[1][0]=INF;        }        else        {            f[pos].a[0][0]=ri;            f[pos].a[0][1]=f[pos].a[1][0]=f[pos].a[1][1]=INF;        }        return ;    }    int mid=MID(f[pos].le,f[pos].ri);    if(mid>=le)        update(L(pos),le,ri);    else        update(R(pos),le,ri);   pushup(pos);}ll query(int pos,int le,int ri,int s,int e){    if(f[pos].le==le&&f[pos].ri==ri)    {       return f[pos].a[s][e];    }    int mid=MID(f[pos].le,f[pos].ri);    if(mid>=ri)        return query(L(pos),le,ri,s,e);    else        if(mid<le)        return query(R(pos),le,ri,s,e);    else    {        ll temp=query(L(pos),le,mid,s,s)+query(R(pos),mid+1,ri,s^1,e);        ll hh=query(L(pos),le,mid,s,s^1)+query(R(pos),mid+1,ri,s,e);        return Max(temp,hh);    }}void build(int pos,int le,int ri){    f[pos].le=le;    f[pos].ri=ri;    if(le==ri)    {        if(le&1)        {            f[pos].a[1][1]=a[le];            f[pos].a[0][0]=INF;            f[pos].a[0][1]=f[pos].a[1][0]=INF;        }        else        {            f[pos].a[0][0]=a[le];            f[pos].a[0][1]=f[pos].a[1][0]=f[pos].a[1][1]=INF;        }        return ;    }    int mid=MID(le,ri);    build(L(pos),le,mid);    build(R(pos),mid+1,ri);    pushup(pos);}ll dp[105][2];ll solve(int le,int ri){  int i,j;  for(i=0;i+le<=ri;i++) dp[i][0]=dp[i][1]=INF;  if((le%2)==0)  {      dp[0][0]=a[le];      dp[0][1]=INF;  }  else  {     dp[0][0]=INF;     dp[0][1]=a[le];  }  for(i=le+1;i<=ri;i++)  {     if(i&1)     {         dp[i-le][0]=dp[i-1-le][0];         dp[i-le][1]=max(max(dp[i-le-1][0]+a[i],dp[i-le-1][1]),a[i]);     }     else     {         dp[i-le][1]=dp[i-le-1][1];         dp[i-le][0]=max(max(dp[i-le-1][1]+a[i],dp[i-le-1][0]),a[i]);     }  }  return max(dp[ri-le][0],dp[ri-le][1]);}int main(){    int i,j,t;    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&n,&m);        for(i=1;i<=n;i++)            scanf("%lld",&a[i]);        build(1,1,n);        int op,le,ri;        while(m--)        {            scanf("%d%d%d",&op,&le,&ri);            ri=min(n,ri);            if(op==0)            {                int len=ri-le+1;                if(len<=100)               {                printf("%lld\n",solve(le,ri));                continue;               }                ll temp=Max(query(1,le,ri,0,0),query(1,le,ri,0,1));                ll hh=Max(query(1,le,ri,1,0),query(1,le,ri,1,1));                printf("%I64d\n",Max(temp,hh));            }            else            {                update(1,le,ri);                a[le]=ri;            }        }    }    return 0;}/*55 3-6 -8 3 4 50 1 51 4 -20 1 5*/




0 0
原创粉丝点击