码迷,mamicode.com
首页 > 其他好文 > 详细

hdu5316Magician 线段树

时间:2015-08-02 15:13:38      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:线段树

//1 a b 将a位置的数改为b
//0 a b 输出[a,b] 区间内的 maximum sum of beautiful subsequence
//A beautiful subsequence is a subsequence that all the adjacent pairs 
//of elves in the sequence have a different parity of position
//维护oto ,ote  , ete , eto分别表示这个区间起点和终点的奇偶情况的最大的beautiful subsequence
#include <iostream>
#include<set>
#include<map>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<string>
using namespace std;
#define maxn 100100
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define left rt<<1
#define right rt<<1|1
#define inf 0x3f3f3f3f3f3f3f
struct node
{
    long long ote,oto,ete,eto;
    int tt;
};
node T[maxn*8];
void pushup(int rt){
    int ls=rt<<1,ls2=rt<<1|1;
    T[rt].ote=max(T[ls].ote+T[ls2].ote,T[ls].oto+T[ls2].ete);
    T[rt].ote=max(max(T[rt].ote ,T[ls].ote) ,T[ls2].ote) ;
    T[rt].oto=max(T[ls].oto+T[ls2].eto,T[ls].ote+T[ls2].oto);
    T[rt].oto=max(max(T[rt].oto,T[ls].oto),T[ls2]  .oto);
    T[rt].eto=max(T[ls].eto+T[ls2].eto,T[ls].ete+T[ls2].oto);
    T[rt].eto=max(max(T[rt].eto,T[ls].eto),T[ls2].eto);
    T[rt].ete=max(T[ls].ete+T[ls2].ote,T[ls].eto+T[ls2].ete);
    T[rt].ete=max(max(T[rt].ete,T[ls].ete),T[ls2].ete);
    return ;
}
int tot;
void csh(int rt){
    T[rt].ete=-inf,T[rt].eto=-inf,T[rt].ote=-inf,T[rt].oto=-inf;
    return ;
}
void build(int l,int r ,int rt){
    if(l==r){
        long long x;
        cin>>x;
        tot++;
        csh(rt);
        T[rt].tt=tot;
        if(tot%2)T[rt].oto= x;
        else T[rt].ete= x;
        return;
    }
    int m=(l+r)>>1;
    build(lson);
    build(rson);
    csh(rt);
    pushup(rt);
    return;
}
void update(int p,long long  cha,int l,int r,int rt){
    if(l==r){
        if(T[rt].tt%2)T[rt].oto=cha;
        else T[rt].ete=cha;
        return ;
    }
    int m=(l+r)>>1;
    if(p<=m)update(p,cha,lson);
    else update(p,cha,rson);
    pushup(rt);
}
node hb(node a,node b){
    node ans;
    ans.ete=max(max(max(a.eto+b.ete,a.ete+b.ote),a.ete),b.ete);
    ans.eto=max(max(max(a.ete+b.oto,a.eto+b.eto),a.eto),b.eto);
    ans.ote=max(max(max(a.ote+b.ote,a.oto+b.ete),a.ote),b.ote);
    ans.oto=max(max(max(a.oto+b.eto,a.ote+b.oto),a.oto),b.oto);
    return ans;
}
node query(int L,int R,int l,int r,int rt){
    if(L<=l && R>=r)return T[rt];
    int m=(l+r)>>1;
    node a,b ;
    a.ete=a.eto=a.ote=a.oto = -inf ;
    b.ete=b.eto=b.ote=b.oto = -inf ;
    if(L<=m)a=query(L,R,lson);
    if(R>m)b=query(L,R,rson);
    return hb(a,b);
}
int main()
{
    //freopen("in.txt" ,"r" , stdin) ;
    ios_base::sync_with_stdio(0);
    int cas;
    cin>>cas;
    while(cas--){
        tot=0;
        int n,m;
        cin>>n>>m;
        build(1,n,1);
        while(m--){
            long long x,a,b;
            cin>>x>>a>>b;
            if(x)update(a,b,1,n,1);
            else{
                node ans=query(a,b,1,n,1);
                long long  answer= -inf;
                answer=max(max(ans.ete,ans.eto),max(ans.ote,ans.oto));
                cout<<answer<<endl;
            }
        }
    }
    return 0;
}

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

hdu5316Magician 线段树

标签:线段树

原文地址:http://blog.csdn.net/cq_pf/article/details/47207329

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!