维护一个左区间和右区间,然后合并
题目链接https://www.acwing.com/problem/content/description/246/
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
typedef long double ld;
const ll maxn = 1e6 + 7;
const ll mod = 1e9 + 7;
ll a[maxn];
struct node
{
ll l,r,dat,sum,lmax,rmax;
}
t[maxn * 4];
void bulid(ll p,ll l,ll r)
{
t[p].l = l,t[p].r = r;
if(t[p].l == t[p].r)
{
t[p].dat = t[p].sum = t[p].lmax = t[p].rmax = a[l];
return ;
}
ll mid = (l + r) / 2;
bulid(p * 2,l,mid);
bulid(p * 2 + 1,mid + 1,r);
t[p].sum = t[p * 2].sum + t[p * 2 + 1].sum;
t[p].lmax = max(t[p * 2].lmax,t[p * 2 + 1].lmax + t[p * 2].sum);
t[p].rmax = max(t[p * 2 + 1].rmax,t[p * 2 + 1].sum + t[p * 2].rmax);
t[p].dat = max(max(t[p * 2].dat,t[p * 2 + 1].dat),t[p * 2].rmax + t[p * 2 + 1].lmax);
}
void change(ll p,ll x,ll y)
{
if(t[p].l == t[p].r)
{
t[p].dat = t[p].sum = t[p].lmax = t[p].rmax = y;
return ;
}
ll mid = (t[p].l + t[p].r) / 2;
if(x <= mid)change(p * 2,x,y);
else change(p * 2 + 1,x,y);
t[p].sum = t[p * 2].sum + t[p * 2 + 1].sum;
t[p].lmax = max(t[p * 2].lmax,t[p * 2 + 1].lmax + t[p * 2].sum);
t[p].rmax = max(t[p * 2 + 1].rmax,t[p * 2 + 1].sum + t[p * 2].rmax);
t[p].dat = max(max(t[p * 2].dat,t[p * 2 + 1].dat),t[p * 2].rmax + t[p * 2 + 1].lmax);
}
node ask(ll p,ll l,ll r)
{
if(t[p].l >= l && t[p].r <= r)return t[p];
ll mid = (t[p].l + t[p].r) / 2;
node a,b,c;
ll val = -1e18;
a.sum = a.lmax = a.dat = a.rmax = val;
b.sum = b.lmax = b.dat = b.rmax = val;
c.sum = 0;
if(l <= mid)
{
a = ask(p * 2,l,r);
c.sum += a.sum;
}
if(r > mid)
{
b = ask(p * 2 + 1,l,r);
c.sum += b.sum;
}
c.dat = max(max(a.dat,b.dat),a.rmax + b.lmax);
c.lmax = max(a.lmax,a.sum + b.lmax);
c.rmax = max(b.rmax,b.sum + a.rmax);
if(l > mid)
{
c.lmax = max(c.lmax,a.lmax);
}
if(r <= mid)
{
c.rmax = max(c.rmax,b.rmax);
}
return c;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
ll n,m;
cin >> n >> m;
for(int i = 1; i <= n; i ++)
{
cin >> a[i];
}
bulid(1,1,n);
for(int i = 1; i <= m; i ++)
{
ll k,x,y;
cin >> k >> x >> y;
if(k == 1)
{
if(x > y)swap(x,y);
cout << ask(1,x,y).dat << endl;
}
else
{
change(1,x,y);
}
}
return 0;
}