HDU5316 Magician (线段树区间合并)



一个漂亮的子序列指的是:一个子序列,它的每一对相邻元素在原数组中的下标的奇偶性不同。

这个题就是求给定的LR区间内的若干个漂亮的子序列他们各自的和最大是多少。

对于线段树每个节点,用四个值  奇始奇终、奇始偶终、偶始奇终、偶始偶终  去分别存四种不同情况的最大值。

然后向上更新就好了!













#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<vector>
#include<iostream>
#include<string>
#include<set>
#include<map>
#include<algorithm>
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define nn 100010
#define LL long long
#define ULL unsiged long long
#define mod 0x7fffffff
#define inf 0x3f3f3f3f
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
struct node
{
    LL eo,ee,oo,oe;
}tree[nn<<2];
LL a[nn];
LL max4(LL A, LL B, LL C, LL D)
{
    return max(max(A, B), max(C, D));
}
void pushup(int rt)
{
    tree[rt].ee=max4(tree[rt<<1].ee,tree[rt<<1|1].ee,tree[rt<<1].ee+tree[rt<<1|1].oe,tree[rt<<1].eo+tree[rt<<1|1].ee);
    tree[rt].oo=max4(tree[rt<<1].oo,tree[rt<<1|1].oo,tree[rt<<1].oo+tree[rt<<1|1].eo,tree[rt<<1].oe+tree[rt<<1|1].oo);
    tree[rt].eo=max4(tree[rt<<1].eo,tree[rt<<1|1].eo,tree[rt<<1].ee+tree[rt<<1|1].oo,tree[rt<<1].eo+tree[rt<<1|1].eo);
    tree[rt].oe=max4(tree[rt<<1].oe,tree[rt<<1|1].oe,tree[rt<<1].oo+tree[rt<<1|1].ee,tree[rt<<1].oe+tree[rt<<1|1].oe);
}
void build(int l,int r,int rt)
{
    if(l==r)
    {
        if(l%2==0)
        {
            tree[rt].ee=a[l];
            tree[rt].oo=-inf;
        }
        else
        {
            tree[rt].oo=a[l];
            tree[rt].ee=-inf;
        }
        tree[rt].eo=tree[rt].oe=-inf;
        return;
    }
    int mid=(l+r)>>1;
    build(lson);
    build(rson);
    pushup(rt);
}
node query(int ll,int rr,int l,int r,int rt)//查找时好像只能用这种姿势,用上面那种姿势会WA的可能是更新的问题吧!
{
    node ans;
    if(ll<=l && r<=rr)
    {
        return tree[rt];
    }
    ans.ee=ans.eo=ans.oe=ans.oo=-inf;
    int mid=(l+r)>>1;

<pre class="sh-cpp sh-sourceCode" style="font-family: Courier New,Courier,monospace;"><span class="sh-comment">/*if(ll<=mid)//这样写会WA的,感觉需要考虑的问题可能有点多吧!还是老实按照最原始的方法更新吧!</span>
<span class="sh-comment">    {</span>
<span class="sh-comment">        node k=query(ll,rr,lson);</span>
<span class="sh-comment">        ans.ee=max(-inf,max(max(ans.ee,k.ee),max(k.eo+ans.ee,k.ee+ans.oe)));</span>
<span class="sh-comment">        ans.eo=max(-inf,max(max(ans.eo,k.eo),max(k.eo+ans.eo,k.ee+ans.oo)));</span>
<span class="sh-comment">        ans.oo=max(-inf,max(max(ans.oo,k.oo),max(k.oo+ans.eo,k.oe+ans.oo)));</span>
<span class="sh-comment">        ans.oe=max(-inf,max(max(ans.oe,k.oe),max(k.oe+ans.oe,k.oo+ans.ee)));</span>
<span class="sh-comment">    }</span>
<span class="sh-comment">    if(mid<rr)</span>
<span class="sh-comment">    {</span>
<span class="sh-comment">        node k=query(ll,rr,rson);</span>
<span class="sh-comment">        ans.ee=max(-inf,max(max(ans.ee,k.ee),max(k.eo+ans.ee,k.ee+ans.oe)));</span>
<span class="sh-comment">        ans.eo=max(-inf,max(max(ans.eo,k.eo),max(k.eo+ans.eo,k.ee+ans.oo)));</span>
<span class="sh-comment">        ans.oo=max(-inf,max(max(ans.oo,k.oo),max(k.oo+ans.eo,k.oe+ans.oo)));</span>
<span class="sh-comment">        ans.oe=max(-inf,max(max(ans.oe,k.oe),max(k.oe+ans.oe,k.oo+ans.ee)));</span>
<span class="sh-comment">    }</span>
<span class="sh-comment">    return ans;*/</span>
    <span class="sh-comment">/*if(ll<=mid)</span>
<span class="sh-comment">    {</span>
<span class="sh-comment">        node k=query(ll,rr,lson);</span>
<span class="sh-comment">        ans.ee=max(-inf,max(max(ans.ee,k.ee),max(ans.eo+k.ee,ans.ee+k.oe)));</span>
<span class="sh-comment">        ans.eo=max(-inf,max(max(ans.eo,k.eo),max(ans.eo+k.eo,ans.ee+k.oo)));</span>
<span class="sh-comment">        ans.oo=max(-inf,max(max(ans.oo,k.oo),max(ans.oo+k.eo,ans.oe+k.oo)));</span>
<span class="sh-comment">        ans.oe=max(-inf,max(max(ans.oe,k.oe),max(ans.oe+k.oe,ans.oo+k.ee)));</span>
<span class="sh-comment">    }</span>
<span class="sh-comment">    if(mid<rr)</span>
<span class="sh-comment">    {</span>
<span class="sh-comment">        node k=query(ll,rr,rson);</span>
<span class="sh-comment">        ans.ee=max(-inf,max(max(ans.ee,k.ee),max(ans.eo+k.ee,ans.ee+k.oe)));</span>
<span class="sh-comment">        ans.eo=max(-inf,max(max(ans.eo,k.eo),max(ans.eo+k.eo,ans.ee+k.oo)));</span>
<span class="sh-comment">        ans.oo=max(-inf,max(max(ans.oo,k.oo),max(ans.oo+k.eo,ans.oe+k.oo)));</span>
<span class="sh-comment">        ans.oe=max(-inf,max(max(ans.oe,k.oe),max(ans.oe+k.oe,ans.oo+k.ee)));</span>
<span class="sh-comment">    }</span>
<span class="sh-comment">    return ans;*/</span>

if(rr<=mid) return query(ll,rr,lson); else if(ll>mid) return query(ll,rr,rson); else { node t1=query(ll,mid,lson); node t2=query(mid+1,rr,rson); ans.ee=max4(t1.ee+t2.oe,t1.eo+t2.ee,t1.ee,t2.ee); ans.oo=max4(t1.oe+t2.oo,t1.oo+t2.eo,t1.oo,t2.oo); ans.eo=max4(t1.ee+t2.oo,t1.eo+t2.eo,t1.eo,t2.eo); ans.oe=max4(t1.oo+t2.ee,t1.oe+t2.oe,t1.oe,t2.oe); return ans; }}void updata(int ll,int v,int l,int r,int rt){ if(l==r) { if(l%2==0) { tree[rt].ee=v; tree[rt].oo=-inf; } else { tree[rt].oo=v; tree[rt].ee=-inf; } tree[rt].eo=tree[rt].oe=-inf; return; } int mid=(l+r)>>1; if(ll<=mid) updata(ll,v,lson); else updata(ll,v,rson); pushup(rt);}int main(){ int t,n,m; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) scanf("%lld",&a[i]); build(1,n,1); while(m--) { int q,x,y; LL yy; scanf("%d",&q); if(q==0) { scanf("%d%d",&x,&y); node ans=query(x,y,1,n,1); LL ma; ma=max4(ans.ee,ans.eo,ans.oe,ans.oo); printf("%lld\n",ma); } else { scanf("%d%lld",&x,&yy); updata(x,yy,1,n,1); } } } return 0;}
 














  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值