HTML & JS
#include <bits/stdc++.h>

    using namespace std;
    //#include "testlib.h"
    #define ff first
    #define ss second
    #define mp make_pair
    #define all(v) v.begin(),v.end()
    #define int long long
    #define ll long long
    #define M 1000000007
    #define inputarr(a,n) for(int i=0;i<n;++i) cin>>a[i]
    #define GCD(m,n) __gcd(m,n)
    #define LCM(m,n) m*(n/GCD(m,n))
    #define mii  map<ll ,ll >
    #define sz(a) (int)a.size()
    #define msi  map<string,ll >
    #define mis  map<ll int, string>
    #define rep(a,b)    for(ll i=a;i<b;i++)
    #define rep0(n)    for(ll i=0;i<n;i++)
    #define repi(i,a,b) for(ll i=a;i<b;i++)
    #define pb push_back
    #define vi vector<ll>
    #define mp make_pair
    #define vs vector<string>
    #define ppb pop_back
    #define endl '\n'
    #define asdf ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    #define r0 return 0;
    #define FORD(i, a, b) for (int i = (int) (a); i >= (int) (b); --i)
    #define FORE(it, c) for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
    #define inputoutput freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
    #define input freopen("input.txt", "r", stdin);
    #define Set(a, s) 4(a, s, sizeof (a))
    #define FOR repi
    #define pii pair<int,int>
    #define REVERSE(v) reverse(ALL(v))
    #define display(x) trav(a,x) cout<<a<<" ";cout<<endl
    #define debug cerr<<"bhau"<<endl
    #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template <typename Arg1>
    void __f(const char* name, Arg1&& arg1){
        std::cerr << name << " : " << arg1 << endl;
    }
    template <typename Arg1, typename... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args){
        const char* comma = strchr(names + 1, ',');std::cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
    }
    ll max(ll a, ll b) { return (a > b)? a : b;}
    int min(int a, int b) { return (a < b)? a : b;}
    void amax(int &a,int b){a=max(a,b)%M; }
    void amin(int &a,int b){ a=min(a,b)%M;}
    void inc(int &a,int b){a=(a+b)%M;}
    void dec(int &a,int b){a=(a-b+M)%M;}
    const int N=2e5+100,inf=-1e17;
    int total[2*N];
    int pre[2*N];
    int suff[2*N];
    int ans[2*N];
    int n;
    void build(){
      for(int i=n-1;i>=0;i--) {total[i]=total[i<<1]+total[i<<1|1];pre[i]=max(pre[i<<1],total[i<<1]+pre[i<<1|1]);suff[i]=max(suff[i<<1|1],suff[i<<1]+total[i<<1|1]);ans[i]=max({ans[i<<1],ans[i<<1|1],pre[i<<1|1]+suff[i<<1]});}
    }
    void modify(int p,int x){
      p+=n;
      for(total[p]=suff[p]=pre[p]=ans[p]=x;p>1;p>>=1) {
        
        total[p>>1]=total[p]+total[p^1];
        int right=max(p,p^1),left=min(p,p^1);
        pre[p>>1]=max(pre[left],total[left]+pre[right]);
        suff[p>>1]=max(suff[right],suff[left]+total[right]);
        ans[p>>1]=max({ans[left],ans[right],pre[right]+suff[left]});
        // trace(p>>1,p,p^1,total[p>>1],pre[p>>1],suff[p>>1],ans[p>>1]);
      }
    }
    int query(int l,int r){
      int tl,tr;tl=tr=0;
      int pl,pr;
      pl=pr=inf;
      pr=inf;
      int al,ar;
      al=ar=inf;
      int sl=inf,sr=inf;
      for(l+=n,r+=n;l<r;l>>=1,r>>=1){
        // trace(l,r);
        if(l&1) {
          al=max({al,ans[l],sl+pre[l]});
          pl=max(pl,tl+pre[l]);
          sl=max(suff[l],total[l]+sl);
 
          tl+=total[l];
          l++;
        }
        if(r&1) {
          --r;
          ar=max({ar,ans[r],sr+pre[r]});
          pr=max(pre[r],total[r]+pr);
          sr=max(sr,tr+suff[r]);
          tr+=total[r];
        }
        // trace(pl,pr,sl,sr,al,ar,tl,tr);
      }
      int res;
      res=max({al,ar,pr+sl});
      return res;
    }
 
    
     int solve()
     {  
        int q;cin>>n>>q;
        rep0(n) cin>>total[i+n];
        rep0(n) {pre[i]=suff[i]=ans[i]=inf;pre[n+i]=suff[n+i]=ans[n+i]=total[n+i];}
        build();
        while(q--){
          int k,x;cin>>k>>x;k--;
          modify(k,x);
          cout<<query(0,n)<<endl;
        }
        r0   
     }
    signed main()
    {   
        asdf
        
        int t=1;
        //cin>>t;
        while(t--)
        {
            solve();
        }     
        
}

Generated URL:

Output:

29
24
18
18
18
27
27
33
27
30
Report Bug