/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 7ms 532.0 KiB
#3 Accepted 10ms 764.0 KiB
#4 Accepted 11ms 580.0 KiB
#5 Accepted 23ms 588.0 KiB
#6 Accepted 21ms 588.0 KiB
#7 Accepted 12ms 612.0 KiB
#8 Accepted 14ms 532.0 KiB
#9 Accepted 18ms 608.0 KiB
#10 Accepted 11ms 604.0 KiB
#11 Accepted 11ms 596.0 KiB
#12 Accepted 11ms 608.0 KiB
#13 Accepted 7ms 532.0 KiB
#14 Accepted 7ms 532.0 KiB
#15 Accepted 7ms 532.0 KiB
#16 Accepted 8ms 620.0 KiB
#17 Accepted 8ms 620.0 KiB
#18 Accepted 8ms 532.0 KiB
#19 Accepted 11ms 532.0 KiB
#20 Accepted 16ms 532.0 KiB
#21 Accepted 23ms 532.0 KiB
#22 Accepted 23ms 532.0 KiB
#23 Accepted 16ms 532.0 KiB
#24 Accepted 15ms 532.0 KiB
#25 Accepted 13ms 576.0 KiB
#26 Accepted 23ms 532.0 KiB
#27 Accepted 22ms 572.0 KiB
#28 Accepted 16ms 532.0 KiB
#29 Accepted 12ms 536.0 KiB
#30 Accepted 12ms 532.0 KiB
#31 Accepted 12ms 532.0 KiB
#32 Accepted 12ms 576.0 KiB
#33 Accepted 12ms 532.0 KiB
#34 Accepted 14ms 576.0 KiB
#35 Accepted 14ms 536.0 KiB
#36 Accepted 23ms 532.0 KiB
#37 Accepted 23ms 536.0 KiB
#38 Accepted 23ms 532.0 KiB
#39 Accepted 16ms 580.0 KiB
#40 Accepted 8ms 580.0 KiB
#41 Accepted 10ms 596.0 KiB
#42 Accepted 82ms 8.02 MiB
#43 Accepted 84ms 7.77 MiB
#44 Accepted 84ms 8.02 MiB
#45 Accepted 83ms 7.77 MiB
#46 Accepted 84ms 8.02 MiB
#47 Accepted 83ms 7.816 MiB
#48 Accepted 85ms 8.02 MiB
#49 Accepted 83ms 7.824 MiB
#50 Accepted 83ms 8.02 MiB

Code

#include <bits/stdc++.h>
using namespace std;

struct SegTree {
    struct Node {
        long long val, lazy;
    };
    vector<Node> tree;
    int n;

    SegTree(int n) {
        this->n = n;
        tree.resize(4 * n + 5, {0, 0});
    }

    void build(vector<long long>& a, int idx, int l, int r) {
        if(l == r) {
            tree[idx].val = a[l];
            return;
        }
        int mid = (l + r) / 2;
        build(a, idx*2, l, mid);
        build(a, idx*2+1, mid+1, r);
        tree[idx].val = max(tree[idx*2].val, tree[idx*2+1].val);
    }

    void push(int idx, int l, int r) {
        if(tree[idx].lazy != 0) {
            tree[idx].val += tree[idx].lazy;
            if(l != r) {
                tree[idx*2].lazy += tree[idx].lazy;
                tree[idx*2+1].lazy += tree[idx].lazy;
            }
            tree[idx].lazy = 0;
        }
    }

    void update(int idx, int l, int r, int ql, int qr, long long val) {
        push(idx, l, r);
        if(r < ql || qr < l) return;
        if(ql <= l && r <= qr) {
            tree[idx].lazy += val;
            push(idx, l, r);
            return;
        }
        int mid = (l + r) / 2;
        update(idx*2, l, mid, ql, qr, val);
        update(idx*2+1, mid+1, r, ql, qr, val);
        tree[idx].val = max(tree[idx*2].val, tree[idx*2+1].val);
    }

    long long query(int idx, int l, int r, int ql, int qr) {
        push(idx, l, r);
        if(r < ql || qr < l) return LLONG_MIN;
        if(ql <= l && r <= qr) return tree[idx].val;
        int mid = (l + r) / 2;
        return max(query(idx*2, l, mid, ql, qr),
                   query(idx*2+1, mid+1, r, ql, qr));
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int T; 
    cin >> T;
    while(T--) {
        int N; 
        cin >> N;
        vector<long long> a(N+1);
        for(int i=1; i<=N; i++) cin >> a[i];

        SegTree st(N);
        st.build(a, 1, 1, N);

        int Q; 
        cin >> Q;
        while(Q--) {
            int type, L, R;
            cin >> type >> L >> R;
            if(type == 1) {
                long long X;
                cin >> X;
                st.update(1, 1, N, L, R, X);
            }
            else {
                cout << st.query(1, 1, N, L, R) << "\n";
            }
        }
    }
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1211 Range MAX
Contest
LUCC Presents Intra LU Junior Programming Contest - Replay
Language
C++17 (G++ 13.2.0)
Submit At
2025-09-02 16:34:21
Judged At
2025-09-02 16:34:21
Judged By
Score
100
Total Time
85ms
Peak Memory
8.02 MiB