Magician

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

2015 Multi-University Training Contest 3

Recommend

wange2014 | We have carefully selected several similar problems for you: 6297 6296 6295 6294 6293

仍然是简单线段树,单点修改,要求从区间中选出一个非空子数列并保证相邻元素在原数组中下标奇偶性不同,求数列总和最大值 。

so" role="presentation" style="position: relative;">soso how" role="presentation" style="position: relative;">howhow to" role="presentation" style="position: relative;">toto solve" role="presentation" style="position: relative;">solvesolve this" role="presentation" style="position: relative;">thisthis easy" role="presentation" style="position: relative;">easyeasy problem?" role="presentation" style="position: relative;">problem?problem?

n≤50000" role="presentation" style="position: relative;">n≤50000n≤50000,我会分块!

n≤100000" role="presentation" style="position: relative;">n≤100000n≤100000+多组数据,线段树?

线段树!

对于每个节点,我们用ll,lr,rl,rr" role="presentation" style="position: relative;">ll,lr,rl,rrll,lr,rl,rr来记录区间内以奇(偶)下标作为左端点,奇(偶)下标作为右端点时区间中满足条件的最大和,对于每种情况稍(du)微(liu)讨论一下就行了。然而本蒟蒻忘记输出时要转成long" role="presentation" style="position: relative;">longlong long" role="presentation" style="position: relative;">longlong然后就gg" role="presentation" style="position: relative;">gggg了。

代码如下:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define inf 0xffffffffffff
#define N 100005
#define lc (p<<1)
#define rc (p<<1|1)
#define mid (T[p].l+T[p].r>>1)
using namespace std;
inline long long read(){
    long long ans=0,w=1;
    char ch=getchar();
    while(!isdigit(ch)){
        if(ch=='-')w=-1;
        ch=getchar();
    }
    while(isdigit(ch))ans=(ans<<3)+(ans<<1)+ch-'0',ch=getchar();
    return ans*w;
}
long long n,a[N],m,t;
struct Node{long long l,r,ll,rr,lr,rl;}T[N<<2];
inline void pushup(long long p){
    T[p].ll=max(T[lc].ll,T[rc].ll);
    T[p].ll=max(T[lc].ll+T[rc].rl,T[p].ll);
    T[p].ll=max(T[lc].lr+T[rc].ll,T[p].ll);
    T[p].rr=max(T[lc].rr,T[rc].rr);
    T[p].rr=max(T[lc].rr+T[rc].lr,T[p].rr);
    T[p].rr=max(T[lc].rl+T[rc].rr,T[p].rr);
    T[p].lr=max(T[lc].lr,T[rc].lr);
    T[p].lr=max(T[lc].lr+T[rc].lr,T[p].lr);
    T[p].lr=max(T[lc].ll+T[rc].rr,T[p].lr);
    T[p].rl=max(T[lc].rl,T[rc].rl);
    T[p].rl=max(T[lc].rl+T[rc].rl,T[p].rl);
    T[p].rl=max(T[lc].rr+T[rc].ll,T[p].rl);
}
inline void build(long long p,long long l,long long r){
    T[p].l=l,T[p].r=r;
    if(l==r){
        T[p].ll=T[p].lr=T[p].rr=T[p].rl=-inf;
        if(l&1)T[p].ll=a[l];
        else T[p].rr=a[l];
        return;
    }
    build(lc,l,mid);
    build(rc,mid+1,r);
    pushup(p);
}
inline void update(long long p,long long k,long long v){
    if(T[p].l==T[p].r){
        if(T[p].l&1)T[p].ll=v;
        else T[p].rr=v;
        return;
    }
    if(k<=mid)update(lc,k,v);
    else update(rc,k,v);
    pushup(p);
}
inline Node query(long long p,long long ql,long long qr){
    if(ql<=T[p].l&&T[p].r<=qr)return T[p];
    if(qr<=mid)return query(lc,ql,qr);
    if(ql>mid)return query(rc,ql,qr);
    Node ans1=query(lc,ql,mid),ans2=query(rc,mid+1,qr),ans;
    ans.l=ans1.l,ans.r=ans2.r;
    ans.ll=max(ans1.ll,ans2.ll);
    ans.ll=max(ans1.ll+ans2.rl,ans.ll);
    ans.ll=max(ans1.lr+ans2.ll,ans.ll);
    ans.rr=max(ans1.rr,ans2.rr);
    ans.rr=max(ans1.rr+ans2.lr,ans.rr);
    ans.rr=max(ans1.rl+ans2.rr,ans.rr);
    ans.lr=max(ans1.lr,ans2.lr);
    ans.lr=max(ans1.lr+ans2.lr,ans.lr);
    ans.lr=max(ans1.ll+ans2.rr,ans.lr);
    ans.rl=max(ans1.rl,ans2.rl);
    ans.rl=max(ans1.rl+ans2.rl,ans.rl);
    ans.rl=max(ans1.rr+ans2.ll,ans.rl);
    return ans;
}
int main(){
    t=read();
    while(t--){
        n=read(),m=read();
        for(long long i=1;i<=n;++i)a[i]=read();
        build(1,1,n);
        while(m--){
            long long op=read(),l=read(),r=read();
            switch(op){
                case 0:{
                    Node q=query(1,l,r);
                    printf("%lld\n",max(max(q.ll,q.lr),max(q.rl,q.rr)));
                    break;
                }
                default:{
                    update(1,l,r);
                    break;
                }
            }
        }
    }
    return 0;
}

2018.07.08 hdu5316 Magician(线段树)的更多相关文章

  1. 2018.07.22 codeforces750E(线段树维护状态转移)

    传送门 给出一个数字字串,给出若干个询问,询问在字串的一段区间保证出现2017" role="presentation" style="position: re ...

  2. 2018.07.08 hdu4521 小明系列问题——小明序列(线段树+简单dp)

    小明系列问题--小明序列 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Proble ...

  3. 2018.07.08 hdu1394 Minimum Inversion Number(线段树)

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...

  4. 2018.07.08 hdu6183 Color it(线段树)

    Color it Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others) Proble ...

  5. 2015 多校联赛 ——HDU5316(线段树)

    Fantasy magicians usually gain their ability through one of three usual methods: possessing it as an ...

  6. hdu 5316 Magician 线段树维护最大值

    题目链接:Magician 题意: 给你一个长度为n的序列v,你需要对这个序列进行m次操作,操作一共有两种,输入格式为 type a b 1.如果type==0,你就需要输出[a,b]区间内的美丽序列 ...

  7. HDU 6356 Glad You Came 2018 Multi-University Training Contest 5 (线段树)

    题目中没有明说会爆int和longlong 的精度,但是在RNG函数中不用unsigned int 会报精度,导致队友debug了很久... 根据每次生成的l,r,v对区间更新m次,然后求 i*ai的 ...

  8. 2018网络预选赛 徐州H 线段树+树状数组

    设读入的数组是a,树状数组用来维护a数组区间和sum,线段树用来维护一个另一个数组ssum的区间和,区间每个点a[i]*(n-i+1),那么l-r的答案是l-r的ssum-(n-r)*(sum[r]- ...

  9. 2018网络预选赛 徐州G 线段树

    线段树,假设求(x1,y1)点的贡献,就找所有比该点出现时间晚,且x坐标大于x1的点中y最大的,贡献就是Y-y1,由于题目条件限制,不可能有x坐标大于(x1,y1)且y坐标大于y1的点,所以贡献肯定为 ...

随机推荐

  1. 5.mybatis实战教程(mybatis in action)之五:与spring3集成(附源码)

    转自:https://blog.csdn.net/nnn9223643/article/details/41962097 在 这一系列文章中,前面讲到纯粹用mybatis 连接数据库, 然后 进行增删 ...

  2. springboot web项目的单元测试

    不废话,直接上代码. //// SpringJUnit支持,由此引入Spring-Test框架支持! @RunWith(SpringJUnit4ClassRunner.class) //// 指定我们 ...

  3. stl-stack+括号配对问题

    栈:stl的一种容器,遵循先进后出原则,,只能在栈的顶部操作,就像放盘子一样,洗好的盘子叠在上面,需要用时也是先从顶部拿.不允许被遍历,没有迭代器 基本操作: 1.头文件#include<sta ...

  4. mysql物理备份

    原本以为直接将data文件夹下每个数据库对应的文件夹拷贝到新的MySQL的data文件夹就可以了,其实不然. 这样做有几个问题: 1.如果是用了引擎的表,还需要复制ibdata文件,并且frm文件所在 ...

  5. update from

    update table1 set table1.column1 =(select table2.column1 from table2  where 关联条件) where exists(selec ...

  6. 检测到有潜在危险的 Request.Form 值——ValidateRequest的使用

    1.aspx中 在 Web 应用程序中,要阻止依赖于恶意输入字符串的黑客攻击,约束和验证用户输入是必不可少的.跨站点脚本攻击就是此类攻击的一个示例. 当请求验证检测到潜在的恶意客户端输入时,会引发此异 ...

  7. Cookie的Domain属性

    Cookie 加了Domain后就写不进去了(不加domain就可以写进去了) 本地测试的时候需要把domain换成localhost cookie跨域的问题,意思就是说A.com下能访问B.com域 ...

  8. 第六章 图(b1)邻接矩阵

  9. C++ define与const

    C++中不但可以用define定义常量还可以用const定义常量,它们的区别如下: 用#define MAX 255定义的常量是没有类型的,所给出的是一个立即数,编译器只是把所定义的常量值与所定义的常 ...

  10. Notepad++正则表达式格式 Editplus使用正则表达式[转]

          使用正则表达式可以很好地完成很多繁琐耗时的工作,以下抄录editplus正则表达式的使用,同样适用于notepad++:表达式 说明 \t 制表符. \n 新行. . 匹配任意字符. | ...