HDU 5316 Magician(线段树区间合并)

Magician

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


 

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

题意:给你一段包含n(n<=1e5)个数的序列,q(q<=1e5)次操作

每次操作三个数

id x y

如果id=0 则查询区间[x,y]中最大的美丽序列和。(一个美丽序列是指这个序列中任意相邻的两个数的数组下标奇偶性均不同)

如果id=1 则把下标为x的数组元素值更新为y。

思路:线段树区间合并。貌似这是区间合并的入门题?但是我错了无数发。。。

最主要的是 a表示奇数下标,b表示偶数下标 则合并的公式为

k.aa=max(x.aa+y.ba,x.ab+y.aa);
k.aa=max(k.aa,max(x.aa,y.aa));
k.ab=max(x.aa+y.bb,x.ab+y.ab);
k.ab=max(k.ab,max(x.ab,y.ab));
k.ba=max(x.ba+y.ba,x.bb+y.aa);
k.ba=max(k.ba,max(x.ba,y.ba));
k.bb=max(x.ba+y.bb,x.bb+y.ab);
k.bb=max(k.bb,max(x.bb,y.bb));

相邻的下标必须奇偶性不同。刚开始前两个写错了,再加上一些细节的错误不知道WA多少发。。。但是代码很容易理解。

代码:

#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f3f3f3f3f
using namespace std;
const int maxn=5e5+10;
int n,m,k,q,ql,qr;
struct node
{
    ll aa,bb,ab,ba;
    void clear(){aa=bb=ba=ab=-inf;}
    ll get(){return max(max(aa,bb),max(ab,ba));}
}a[maxn],ans;
node pushup(node x,node y)
{
    node k;
    k.aa=max(x.aa+y.ba,x.ab+y.aa);
    k.aa=max(k.aa,max(x.aa,y.aa));
    k.ab=max(x.aa+y.bb,x.ab+y.ab);
    k.ab=max(k.ab,max(x.ab,y.ab));
    k.ba=max(x.ba+y.ba,x.bb+y.aa);
    k.ba=max(k.ba,max(x.ba,y.ba));
    k.bb=max(x.ba+y.bb,x.bb+y.ab);
    k.bb=max(k.bb,max(x.bb,y.bb));
    return k;
}
void build(int i,int l,int r)
{
    if(l==r)
    {
        a[i].clear();// 1
        if(l&1) scanf("%lld",&a[i].aa);// 2
        else scanf("%lld",&a[i].bb);
        return;
    }
    int mid=(l+r)>>1;
    build(i*2,l,mid);
    build(i*2+1,mid+1,r);
    a[i]=pushup(a[i*2],a[i*2+1]);
}
void update(int i,int l,int r,int pos,ll v)
{
    if(l==r)
    {
        a[i].clear();
        if(l&1) a[i].aa=v;// 3
        else a[i].bb=v;
        return;
    }
    int mid=(l+r)>>1;
    if(pos<=mid) update(i*2,l,mid,pos,v);
    else update(i*2+1,mid+1,r,pos,v);
    a[i]=pushup(a[i*2],a[i*2+1]);
}
void query(int i,int l,int r)
{
    if(ql<=l&&qr>=r) {ans=pushup(ans,a[i]);return;}
    int mid=(l+r)>>1;
    if(ql<=mid) query(i*2,l,mid);
    if(qr>mid)  query(i*2+1,mid+1,r);
}
int main()
{
    int T,cas=1;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        build(1,1,n);
        while(m--)
        {
            int id;
            scanf("%d",&id);
            if(id)
            {
                ll v;int x;
                scanf("%d%lld",&x,&v);
                update(1,1,n,x,v);
            }
            else
            {
                scanf("%d%d",&ql,&qr);
                ans.clear();
                query(1,1,n);
                printf("%lld\n",ans.get());
            }
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/lsd20164388/article/details/81155155