MAXBTY - Editorial

#include<bits/stdc++.h>
#include<unordered_map>
#define M 1000000007
#define T 998244353
#define PI 3.142
#define ll long long
#define MAXN 100001


using namespace std;
ll tx[4*MAXN], tn[4*MAXN], lx[4*MAXN], ln[4*MAXN];
ll maxi(ll a, ll b)
{
    return (a>b)?a:b;
}
ll mini(ll a, ll b)
{
    return (a<b)?a:b;
}
void buildx(ll pre[], ll v, ll tl, ll tr)
{
    if(tl==tr){
        tx[v] = pre[tl];
    }
    else {
        ll tm = (tl+tr)/2;
        buildx(pre, 2*v, tl, tm);
        buildx(pre, 2*v+1, tm+1, tr);
        tx[v] = maxi(tx[2*v], tx[2*v+1]);
    }
}
void buildn(ll pre[], ll v, ll tl, ll tr)
{
    if(tl==tr){
        tn[v] = pre[tl];
    }
    else {
        ll tm = (tl+tr)/2;
        buildn(pre, 2*v, tl, tm);
        buildn(pre, 2*v+1, tm+1, tr);
        tn[v] = mini(tn[2*v], tn[2*v+1]);
    }
}
void pushx(ll v)
{
    tx[2*v] += lx[v];
    lx[2*v] += lx[v];
    tx[2*v+1] += lx[v];
    lx[2*v+1] += lx[v];
    lx[v]=0;
}
void pushn(ll v)
{
    tn[2*v] += ln[v];
    ln[2*v] += ln[v];
    tn[2*v+1] += ln[v];
    ln[2*v+1] += ln[v];
    ln[v]=0;
}
void updatex(ll v, ll tl, ll tr, ll l, ll r, ll add){
    if(l>r){
        return;
    }
    if(l==tl && tr==r){
        tx[v] += add;
        lx[v] += add;
    }
    else {
        pushx(v);
        ll tm = (tl+tr)/2;
        updatex(2*v, tl, tm, l, mini(r, tm), add);
        updatex(2*v+1, tm+1, tr, maxi(l, tm+1), r, add);
        tx[v] = maxi(tx[2*v], tx[2*v+1]);
    }
}
void updaten(ll v, ll tl, ll tr, ll l, ll r, ll add){
    if(l>r){
        return;
    }
    if(l==tl && tr==r){
        tn[v] += add;
        ln[v] += add;
    }
    else {
        pushn(v);
        ll tm = (tl+tr)/2;
        updaten(2*v, tl, tm, l, mini(r, tm), add);
        updaten(2*v+1, tm+1, tr, maxi(l, tm+1), r, add);
        tn[v] = mini(tn[2*v], tn[2*v+1]);
    }
}
ll qux(ll v, ll tl, ll tr, ll l, ll r)
{
    if(l>r){
        return INT64_MIN;
    }
    if(l==tl && r==tr){
        return tx[v];
    }
    pushx(v);
    ll tm = (tl+tr)/2;
    return maxi(qux(2*v, tl, tm, l, mini(r, tm)), qux(2*v+1, tm+1, tr, maxi(l, tm+1), r));
}
ll qun(ll v, ll tl, ll tr, ll l, ll r)
{
    if(l>r){
        return INT64_MAX;
    }
    if(l==tl && r==tr){
        return tn[v];
    }
    pushn(v);
    ll tm = (tl+tr)/2;
    return mini(qun(2*v, tl, tm, l, mini(r, tm)), qun(2*v+1, tm+1, tr, maxi(l, tm+1), r));
}
void solve()
{
    ll n, q, i, x, y, ans1, ans2, v, dif;
    cin >> n >> q;
    ll b[n], pre[n];
    for(i=0; i<n; i++){
        cin >> b[i];
    }
    pre[0]=b[0];
    for(i=1; i<n; i++){
        pre[i] = pre[i-1]+b[i];
    }
    buildx(pre, 1, 0, n-1);
    buildn(pre, 1, 0, n-1);
    for(i=0; i<q; i++){
        char ch;
        cin >> ch;
        if(ch=='Q'){
            cin >> x >> y;
            x--;
            y--;
            ans1 = qux(1, 0, n-1, y, n-1);
            ans2 = qun(1, 0, n-1, 0, x-1);
            if(ans2>0){
                ans2 = 0;
            }
            cout << ans1 - ans2 << "\n";
        }
        else {
            cin >> x >> v;
            x--;
            dif = v - b[x];
            b[x]=v;
            updatex(1, 0, n-1, x, n-1, dif);
            updaten(1, 0, n-1, x, n-1, dif);
        }
    }
}
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif

    int _t;cin >> _t; while(_t--)
    solve();

    return 0;
}

The approach is just like the setter’s solution. I tried this code against my custom tests and it gives the correct answer yet I am getting WA. It seems I am missing an edge case. Please help me find out the mistake. And sorry if I have broken any guidelines it’s my first time on discuss.

Can Someone please help me out on this.
I’m not sure if my approach is wrong or i did some mistake in writing code. The Code seems clear and understandable for everyone. So please give it a look .
Link : CodeChef: Practical coding for everyone

Just set lazy arrays to zero for each test case.

2 Likes

Thanks got AC now. I feel stupid

can we do this question without segment tree i have used dp in this program but it give me tle

Hello,my code with ID 30677920 for MAXBTY is showig wrong ,can anyone help me find where I am
going wrong?

@tmwilliamlin Write down this kind of editorial for mysara question as well of march cookoff 2020 please

Can We use Fenwick trees for this problem?
I have some doubts in Update —>
U x v: Chef reevaluates the beauty of the letter from the x-th fan. The new value of Bx becomes v.

according to this statement: after U 1 1 result for query Q 2 4 must be 5

we have to replace current value of given array with v, right? in Fenwick tree at index x value will be updated as:

update(int *binaryIndexedTree,int value,int index,int n) //here x=index and v=value and value is passed as value=v-arr[x]; { while(index<=n) { binaryIndexedTree[index]+=value;
index+=index&(-index); }

1 Like

I tried solving this with Binary Indexed Tree. If someone has solved this using BIT then please let me know.

This question is quite similar to this problem. If one carefully notes
problem link:GSS1 spoj
Tutorial link:- A classical segment tree problem
Classical segment tree for maximum sum in a range.

My code for which I solved the MAXBTY problem:-
https://www.codechef.com/viewsolution/30686539

6 Likes

Core Idea is to use x=maximum suffix subarray sum in range 1 to x-1
and y=maximum prefix subarray sum in range y+1 to n and
z=sum of subarray x to y
by using these three values
output max(z,x+z,y+z,x+y+z) as answer.
For obtaining this value use segment tree nodes.

1 Like

CodeChef: Practical coding for everyone Hey can anyone tell me what I am doing wrong. My approach is same as given in editorial maintaining two segment trees for positive and negative prefix sums . It really means a lot if anyone can help. Thanks for your time.

https://www.codechef.com/viewsolution/30687934
Can anyone help me with it?
My logic is same as tester’s soltution the only difference is 0based and 1 based index

https://www.codechef.com/viewsolution/30687418

Can someone tell me what is wrong with this solution?
I’m unable to figure it out.

can we do this question with help of binary indexed tree ( used in LAZER of March 2020 long challenge) instead of segment tree?

link of LAZER : https://www.codechef.com/MARCH20B/problems/LAZER

Definitely you can if you preserve prefix suffix and sum in a range as per above shared article by me.
In-fact The second question of div1 of previous cook off can also be done the same way. This cook offs and previous cook offs second question were almost similar quickly check both the questions.

1 Like

@anon31329220 Can you explain lines 106-109?

LIne 106 :-node y1=query(1,0,n-1,0,l-2);
Querying range 1 to x-1 and getting a node.
Line 107: - node y2=query(1,0,n-1,r,n-1);
Querying range y+1 to n and getting a node
in each node y , y1 and y2 we have maximum prefix subarray sum , maximum suffix subarray sum , total sum of subarray
thus now we got all the things for all the three ranges that is 1 to x-1 x to y and y+1 to n
now

x=maximum suffix subarray sum in range 1 to x-1 obtain this from node y1
and y=maximum prefix subarray sum in range y+1 to n and obtain this from node y2
z=sum of subarray x to y and obtain this from node y
by using these three values
output max(z,x+z,y+z,x+y+z) as answer.

i tried that way,my query working fine but update is not,shall we discuss more on BIT for this problem?