Cod sursa(job #3376257)

Utilizator Andrei_GAndreiG Andrei_G Data 17 noiembrie 2026 08:53:40
Problema SequenceQuery Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.17 kb
#include <iostream>
#pragma GCC optimize("O3,unroll-loops")
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <iomanip>
#include <numeric>
#include <cstdio>
#include <bitset>
#include <string>
#include <vector>
#include <cmath>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#define int long long
//#define int short
using namespace std;

const int nmax = 1e5;
const int nodemax = 4 * nmax;
const int inf = 1e18;

int n, m, v[nmax + 5];

struct Aint{
    struct Node{
        int pref;
        int suf;
        int mx;
        int sum;
    };

    Node merge(Node& node1, Node& node2){
        int prefix = max(node1.pref, node1.sum + node2.pref);
        int sufix = max(node2.suf, node2.sum + node1.suf);
        int mx = max(max(node1.mx, node2.mx), node1.suf + node2.pref);
        int sum = node1.sum + node2.sum;
        return {prefix, sufix, mx, sum};
    }

    vector<Node> tree;

    Aint(){
        tree.assign(nodemax + 1, {0, 0, 0, 0});
    }

    void build(int node, int l, int r){
        if (l == r){
            tree[node] = {v[l], v[l], v[l], v[l]};
            return;
        }
        int mid = (l + r) / 2;
        build(2 * node, l, mid);
        build(2 * node + 1, mid + 1, r);
        tree[node] = merge(tree[2 * node], tree[2 * node + 1]);
    }

    Node query(int node, int l, int r, int ql, int qr){
        if (l > qr || r < ql){
            return {-inf, -inf, -inf, 0};
        }
        if (ql <= l && r <= qr){
            return tree[node];
        }
        int mid = (l + r) / 2;
        Node x = query(2 * node, l, mid, ql, qr);
        Node y = query(2 * node + 1, mid + 1, r, ql, qr);
        return merge(x, y);
    }
}aint;

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    freopen("sequencequery.in", "r", stdin);
    freopen("sequencequery.out", "w", stdout);
    cin>>n>>m;
    for (int i = 1; i <= n; i++){
        cin>>v[i];
    }
    aint.build(1, 1, n);
    while (m--){
        int x, y; cin>>x>>y;
        cout<<aint.query(1, 1, n, x, y).mx<<"\n";
    }
}