Cod sursa(job #3200999)

Utilizator Mihai_OctMihai Octavian Mihai_Oct Data 6 februarie 2024 14:38:36
Problema SequenceQuery Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.42 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("sequencequery.in");
ofstream fout("sequencequery.out");
struct Nod {
    int suf, pref, secv, sum;
} a[400002];
int n, m, i, x, y;

/// ca sa nu complic treaba primesc nodurile, nu index
static inline Nod Calc(Nod a, Nod b) {
    Nod c;
    c.sum  = a.sum + b.sum;
    c.pref = max(a.pref, a.sum + b.pref);
    c.suf  = max(b.suf,  b.sum + a.suf);
    c.secv = max(max(a.secv, b.secv), a.suf + b.pref);
    return c;
}

static inline void Build(int nod, int st, int dr) {
    if(st == dr) {
        fin >> a[nod].sum;
        a[nod].pref = a[nod].sum;
        a[nod].suf = a[nod].sum;
        a[nod].secv = a[nod].sum;
    }
    else {
        int mij = st + (dr - st) / 2;
        Build(2 * nod, st, mij);
        Build(2 * nod + 1, mij + 1, dr);
        Calc(a[2 * nod], a[2 * nod + 1]);
    }
}

static inline Nod Query(int nod, int st, int dr) {
    if(x <= st && dr <= y) return a[nod];
    else {
        int mij = st + (dr - st) / 2;
        if(y <= mij) return Query(nod * 2, st, mij);
        else if(x > mij) return Query(nod * 2 + 1, mij + 1, dr);
        else return Calc(Query(nod * 2, st, mij),
                         Query(nod * 2 + 1, mij + 1, dr));
    }
}

int main()  {
    fin >> n >> m;
    Build(1, 1, n);
    while(m--) {
        fin >> x >> y;
        fout << Query(1, 1, n).secv << "\n";
    }

    return 0;
}