链接
题解
维护:强制包含左端点的最优答案,强制包含右端点的最有答案,整个区间的最优答案,整个区间是否合法,整个区间的和
合并两个区间:
Node merge(Node a, Node b)
{
if(a.l==-1)return b;
if(b.l==-1)return a;
Node c;
c.l=a.l, c.r=b.r;
c.sum = a.sum + b.sum;
bool chigau = judge(arr[a.r],arr[b.l]);
c.ok = a.ok and b.ok and chigau;
c.lbest = chigau and a.ok ? max(a.sum+b.lbest,a.lbest) : a.lbest ;
c.rbest = chigau and b.ok ? max(b.sum+a.rbest,b.rbest) : b.rbest ;
c.best = max(a.best,b.best);
if(chigau)c.best = max(c.best,a.rbest+b.lbest);
return c;
}
代码
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 100010
#define maxe 100010
#define cl(x) memset(x,0,sizeof(x))
#define rep(i,a,b) for(i=a;i<=b;i++)
#define drep(i,a,b) for(i=a;i>=b;i--)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<<x<<endl
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{
ll c, f(1);
for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
for(;isdigit(c);c=getchar())x=x*10+c-0x30;
return f*x;
}
bool judge(ll a, ll b)
{
return (a^b)&1;
}
ll arr[maxn];
struct SegmentTree
{
struct Node
{
int l, r;
ll sum, lbest, rbest, best;
bool ok;
Node(){l=-1;}
Node(ll L, ll R, ll v)
{
l=L, r=R;
sum=lbest=rbest=best=v;
ok = true;
}
}node[maxn<<2];
Node merge(Node a, Node b)
{
if(a.l==-1)return b;
if(b.l==-1)return a;
Node c;
c.l=a.l, c.r=b.r;
c.sum = a.sum + b.sum;
bool chigau = judge(arr[a.r],arr[b.l]);
c.ok = a.ok and b.ok and chigau;
c.lbest = chigau and a.ok ? max(a.sum+b.lbest,a.lbest) : a.lbest ;
c.rbest = chigau and b.ok ? max(b.sum+a.rbest,b.rbest) : b.rbest ;
c.best = max(a.best,b.best);
if(chigau)c.best = max(c.best,a.rbest+b.lbest);
return c;
}
void pushup(ll o)
{
node[o] = merge( node[o<<1] , node[o<<1|1] );
}
void build(ll o, ll l, ll r)
{
ll mid(l+r>>1);
node[o].l=l, node[o].r=r;
if(l==r){node[o]=Node(l,l,arr[l]);return;}
build(o<<1,l,mid);
build(o<<1|1,mid+1,r);
pushup(o);
}
Node Sum(ll o, ll l, ll r)
{
ll mid(node[o].l+node[o].r>>1);
Node ans;
if(l<=node[o].l and r>=node[o].r)return node[o];
if(l<=mid)ans = merge(ans,Sum(o<<1,l,r));
if(r>mid)ans = merge(ans,Sum(o<<1|1,l,r));
return ans;
}
void chg(ll o, ll pos)
{
ll mid(node[o].l+node[o].r>>1);
if(node[o].l==node[o].r){node[o]=Node(pos,pos,arr[pos]);return;}
if(pos<=mid)chg(o<<1,pos);
else chg(o<<1|1,pos);
pushup(o);
}
}segtree;
int main()
{
ll n=read(), m=read(), i, j;
rep(i,1,n)arr[i]=read();
segtree.build(1,1,n);
while(m--)
{
ll type=read();
if(type==0)
{
ll l=read(), r=read();
auto node = segtree.Sum(1,l,r);
printf("%lld\n",node.best);
}
else
{
ll x=read(), v=read();
arr[x]=v;
segtree.chg(1,x);
}
}
return 0;
}