#2235984

Solution for DSL_2_B: Range Sum Query (RSQ) by Tsuta_J

Source Code Status Test Cases
    Policy: public     Reviewed: 33    
00.11 sec    4072 KB    49 lines     1145 bytes    2017-03-29 16:15
#include <bits/stdc++.h>
using namespace std;

struct SegmentTree {
private:
    int n;
    vector<int> node;
public:
    SegmentTree(vector<int> v) {
        int sz = (int)v.size();
        n = 1; while(n < sz) n *= 2;
        node.resize(2*n-1, 0);

        for(int i=0; i<sz; i++) node[i+n-1] = v[i];
        for(int i=n-2; i>=0; i--) node[i] = node[i*2+1] + node[i*2+2];
    }

    void add(int k, int val) {
        k += (n - 1);
        node[k] += val;

        while(k > 0) {
            k = (k - 1) / 2;
            node[k] = node[2*k+1] + node[2*k+2];
        }
    }

    int getsum(int a, int b, int k=0, int l=0, int r=-1) {
        if(r < 0) r = n;
        if(b <= l || r <= a) return 0;
        if(a <= l && r <= b) return node[k];

        int vl = getsum(a, b, 2*k+1, l, (l+r)/2);
        int vr = getsum(a, b, 2*k+2, (l+r)/2, r);
        return vl + vr;
    }
};

int main() {
    int n, q; cin >> n >> q;
    SegmentTree seg( vector<int>(n, 0) );
    for(int i=0; i<q; i++) {
        int c, x, y; cin >> c >> x >> y;
        if(c == 0) seg.add(x-1, y);
        else cout << seg.getsum(x-1, y) << endl;
    }
    return 0;    
}

Compile Error Logs:
You are not authorized to see the message.

Status
Judge: 20/20 C++ CPU: 00.11 sec Memory: 4072 KB Length: 1145 B 2017-03-29 16:15 2017-03-29 16:15
Results for testcases
Case # Verdict CPU Time Memory In Out Case Name
Case #1: : Accepted 00.00 sec 3092 KB
Case #2: : Accepted 00.00 sec 3088 KB
Case #3: : Accepted 00.00 sec 3000 KB
Case #4: : Accepted 00.00 sec 3072 KB
Case #5: : Accepted 00.00 sec 3096 KB
Case #6: : Accepted 00.00 sec 3072 KB
Case #7: : Accepted 00.00 sec 3088 KB
Case #8: : Accepted 00.00 sec 3012 KB
Case #9: : Accepted 00.00 sec 3096 KB
Case #10: : Accepted 00.00 sec 3056 KB
Case #11: : Accepted 00.00 sec 3020 KB
Case #12: : Accepted 00.01 sec 3128 KB
Case #13: : Accepted 00.00 sec 3028 KB
Case #14: : Accepted 00.07 sec 3544 KB
Case #15: : Accepted 00.07 sec 3628 KB
Case #16: : Accepted 00.08 sec 3584 KB
Case #17: : Accepted 00.10 sec 4032 KB
Case #18: : Accepted 00.11 sec 4052 KB
Case #19: : Accepted 00.10 sec 4008 KB
Case #20: : Accepted 00.10 sec 4072 KB
< prev | / | next >  
 
Judge Input #  ( | ) Judge Output #  ( | )


Comments
 
 Under Construction.
 
Categories
 
 
Free Tags