DMOJ · stp1

Segment Tree Practice 1

This C++ solution uses segment tree for DMOJ stp1 Segment Tree Practice 1. Read the reasoning, inspect the code, or try your own test case below.

stp1Graphs & treesSegment treeC++52 lines
Solution182of 248
Open official problem ↗ Download C++ file ↓ Search the library → Open full Code Lab ↗ Report an issue ↗

Approach

Segment tree

Segment Tree Practice 1 matches point updates, range-sum queries, and output behavior.

Graphs & trees

Problem and code

Useful links.

Written by benbenyaojifen. Try the problem first, then compare your approach with the code.

Open official problem ↗View exact source file ↗
Implementation

Segment_Tree_Practice_1.cpp

C++

    #include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int inf = 1e9;
    const long long INF = 1e17; //❄️
    vector<ll> seg;
    void build(const vector<int> &a, int indx, int l, int r) {
        if (l == r) {
            seg[indx] = a[l];
            return;
        }
        int mid = (l + r) >> 1;
        build(a, indx * 2, l, mid);
        build(a, indx * 2 + 1, mid + 1, r);
        seg[indx] = seg[indx * 2] + seg[indx * 2 + 1];
    }
    void update(int pos, int val, int indx, int l, int r) {
        if (l == r) {
            seg[indx] = val;
            return;
        }
        int mid = (l + r) >> 1;
        if (pos <= mid) {
            update(pos, val, indx * 2, l, mid);
        } else update(pos, val, indx * 2 + 1, mid + 1, r);
        seg[indx] = seg[indx * 2] + seg[indx * 2 + 1];
    }
    ll qry(int ql, int qr, int indx, int l, int r) {
        if (r < ql || qr < l) return 0;
        if (ql <= l && r <= qr) return seg[indx];
        int mid = (l + r) >> 1;
        return qry(ql, qr, indx * 2, l, mid) + qry(ql, qr, indx * 2 + 1, mid + 1, r);  
     
    }
    int main() {
        ios::sync_with_stdio(0); cin.tie(0); 
        int n, q; cin >> n >> q;
        vector<int> v(n + 1);
        for (int i = 1; i <= n; i++) cin >> v[i];
        seg.assign(4 * n, 0);
        build(v, 1, 1, n);
        while (q--) {
            char c;
            int x, y; cin >> c >> x >> y;
            if (c == 'U') update(x, y, 1, 1, n);
            else {
                cout << qry(x, y, 1, 1, n) << " \n"[q > 0];
            }
            
        }
        return 0;
    }
        

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗Keep studying →

Test this problem

Run your code here.

Paste your code, run a test case, compare the output, or trace selected values.

Full trace, comparison & stress testing ↗
StatusReady
Output
No run yet.
Diagnostics
No diagnostics yet.

Each run is isolated and has strict limits. Passing one test does not guarantee the judge will accept the solution.