Lucifer1729's blog

By Lucifer1729, history, 13 months ago, In English

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

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

Full text and comments »

  • Vote: I like it
  • +1
  • Vote: I do not like it