Segment With Maximum Sum

Revision en1, by Lucifer1729, 2023-04-04 08:40:10

I tried to code solution for the Segment With Maximum Sum problem from the Segment Trees Section in ITMO pilot course.

class Sgtree{
private:
    vl seg,pref,suff,sum;
    int _n;
 
    void leaf_update(ll node,ll val) {
        pref[node] = val;
        suff[node] = val;
        seg[node] = val;
        sum[node] = val;
    }
 
    void nonLeaf_update(ll node) {
        sum[node] = sum[2*node] + sum[2*node+1];
        pref[node] = max(pref[2*node],sum[2*node]+pref[2*node+1]);
        suff[node] = max(pref[2*node+1],sum[2*node+1]+suff[2*node]);
        seg[node] = max({seg[2*node],seg[2*node+1],suff[2*node]+pref[2*node+1]});
    }
public:
    Sgtree(vl& arr){
        _n = arr.size();
        // increasing size of array untill it becomes power of 2
        while((_n&(_n-1)) != 0) {
            arr.pb(0ll);
            _n++;
        }
 
        seg.resize(2*_n);
        pref.resize(2*_n);
        suff.resize(2*_n);
        sum.resize(2*_n);
 
        build(1,0,_n-1,arr);
    }
 
    void build(ll node,ll nl,ll nr,vl &arr){
        if(nl == nr){
            leaf_update(node,arr[nl]);
            return;
        }
        ll mid = (nl+nr) >> 1;
        build(2*node,nl,mid,arr);
        build(2*node+1,mid+1,nr,arr);
 
        nonLeaf_update(node);
    }
 
    // iterative version, O(logN)
    void point_update_itr(ll i,ll val) {
        leaf_update(_n+i,val);
 
        for(int j = (_n + i) / 2; j>=1; j /= 2)
            nonLeaf_update(j);
    }
 
    ll get_ans() {
        return max(seg[1],0ll);
    }
};

This is the code of my segment tree and i'm not able to find any mistake in this but this is giving WA on TC23.

Can anyone please tell me where am i doing wrong

Tags segment tree, doubt

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English Lucifer1729 2023-04-04 08:41:21 106
en1 English Lucifer1729 2023-04-04 08:40:10 1786 Initial revision (published)