HDU 5316 Magician(线段树)

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

1
1 1
1
0 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 100005

inline 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;
}

/*

5
5 3
-6 -8 3 4 5
0 1 5
1 4 -2
0 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 100005

inline 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;
}

/*

5
5 3
-6 -8 3 4 5
0 1 5
1 4 -2
0 1 5

*/

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-14 05:57:54

HDU 5316 Magician(线段树)的相关文章

hdu 5316 Magician (线段树)

题目链接: hdu 5316 Magician 题目描述: 有n个精灵,每个精灵都有一个魔法值,现在有两个操作: (0, a, b)查询[a, b]序列中的一个完美序列的最大和,完美序列就是数组中相邻数字的下标奇偶性不同. (1, a, b)更新下标为a的精灵魔法值为b. 对的,没错就是这个意思,但是比赛的时候就是题意卡的死死的有没有啊,就是各种不懂有没有啊!!! 1 #include <cstdio> 2 #include <cstring> 3 #include <ios

HDU 5316 Magician(线段树区间合并, 子序列最值 多校2015啊)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5316 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 an

HDU 1542 Atlantis 线段树+离散化+扫描线

题意:给出一些矩形的最上角坐标和右下角坐标,求这些矩形的面积并. NotOnlySuccess 线段树专辑中扫描线模板题,弱智的我对着大大的代码看了一下午才搞懂. 具体见思路见注释=.= #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #define lson rt<<1,l,mid #define rson rt<<1|1,mid

HDU 1828 Picture 线段树+扫描线

题意:给你一些矩形的左上角点的坐标和右下角点的坐标,求周长并 最显而易见的思路就是对于x轴和y轴做两次扫描线,对于负数的坐标进行离散化.每次增加的值是线段变化量的绝对值.具体写法和求面积并的差不多. #include <cstdio> #include <algorithm> #include <cstring> #include <vector> using namespace std; #define lson rt << 1 , l , m

HDU 1542 Atlantis(线段树扫描线)

http://acm.hdu.edu.cn/showproblem.php?pid=1542 Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6788    Accepted Submission(s): 2970 Problem Description There are several ancient Greek

POJ 1177/HDU 1828 picture 线段树+离散化+扫描线 轮廓周长计算

求n个图矩形放下来,有的重合有些重合一部分有些没重合,求最后总的不规则图型的轮廓长度. 我的做法是对x进行一遍扫描线,再对y做一遍同样的扫描线,相加即可.因为最后的轮廓必定是由不重合的线段长度组成的,这样理论上是对的 要注意处理高度相同的线段,把底边优先处理(在代码里就是f标记为1的线段),因为若是一个矩形的底边和另一个矩形的上边重合,则这个轮廓肯定不能算 不过POJ和HDU的数据好像都比较弱,我没进行上面的细节处理也AC了,不过一个很简单的数据就会不对,所以还是要处理一下才是真正正确的代码 我

hdu 1542 Atlantis(线段树&amp;扫描线&amp;面积并)

Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6386    Accepted Submission(s): 2814 Problem Description There are several ancient Greek texts that contain descriptions of the fabled i

hdu 1828 Picture(线段树&amp;扫描线&amp;周长并)

Picture Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2578    Accepted Submission(s): 1363 Problem Description A number of rectangular posters, photographs and other pictures of the same shap

hdu 1542 Atlantis(线段树)

Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6899    Accepted Submission(s): 3022 Problem Description There are several ancient Greek texts that contain descriptions of the fabled i

hdu 4864(2) 线段树

对task和machine的yi由小到大进行排序,然后对machine来跟task配对.当machine[].yi >= task[].yi时,就更新线段树,在1-1440上做线段树,线段树存的是task[].xi,同时用用优先队列保存task[].yi:当machine[].yi < task[].yi时,就查找 1到machine[].xi最大的值.如果存在最大值的话,把优先队列里的task[].yi取出来..这样一个machine就匹配到了一个最优的任务.还是看代码好好意会吧,细节挺多的