#2236726

Solution for DSL_2_G: RSQ and RAQ by Tsuta_J

Source Code Status Test Cases
    Policy: public     Reviewed: 86    
00.15 sec    7600 KB    74 lines     1853 bytes    2017-03-29 23:58
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

int N, Q;
struct LazySegmentTree {
private:
    int n;
    vector<ll> node, lazy;

public:
    LazySegmentTree(vector<ll> v) {
        int sz = (int)v.size();
        n = 1; while(n < sz) n *= 2;
        node.resize(2*n-1);
        lazy.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 eval(int k, int l, int r) {
        if(lazy[k] != 0) {
            node[k] += lazy[k];
            if(r - l > 1) {
                lazy[2*k+1] += lazy[k] / 2;
                lazy[2*k+2] += lazy[k] / 2;
            }
            lazy[k] = 0;
        }
    }

    void add(int a, int b, ll x, int k=0, int l=0, int r=-1) {
        if(r < 0) r = n;
        eval(k, l, r);
        if(b <= l || r <= a) return;
        if(a <= l && r <= b) {
            lazy[k] += (r - l) * x;
            eval(k, l, r);
        }
        else {
            add(a, b, x, 2*k+1, l, (l+r)/2);
            add(a, b, x, 2*k+2, (l+r)/2, r);
            node[k] = node[2*k+1] + node[2*k+2];
        }
    }

    ll getsum(int a, int b, int k=0, int l=0, int r=-1) {
        if(r < 0) r = n;
        eval(k, l, r);
        if(b <= l || r <= a) return 0;
        if(a <= l && r <= b) return node[k];
        ll vl = getsum(a, b, 2*k+1, l, (l+r)/2);
        ll vr = getsum(a, b, 2*k+2, (l+r)/2, r);
        return vl + vr;
    }
};
int main() {
    cin >> N >> Q;
    LazySegmentTree seg( vector<ll>(N, 0) );
    for(int i=0; i<Q; i++) {
        int query; cin >> query;
        if(query == 0) {
            int s, t, x; cin >> s >> t >> x;
            seg.add(s-1, t, x);
        }
        else {
            int s, t; cin >> s >> t;
            cout << seg.getsum(s-1, t) << endl;
        }
    }
    return 0;
}

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

Status
Judge: 22/22 C++ CPU: 00.15 sec Memory: 7600 KB Length: 1853 B 2017-03-29 23:58 2017-03-29 23:58
Results for testcases
Case # Verdict CPU Time Memory In Out Case Name
Case #1: : Accepted 00.00 sec 3008 KB
Case #2: : Accepted 00.00 sec 3108 KB
Case #3: : Accepted 00.00 sec 3044 KB
Case #4: : Accepted 00.00 sec 3092 KB
Case #5: : Accepted 00.00 sec 3012 KB
Case #6: : Accepted 00.00 sec 2996 KB
Case #7: : Accepted 00.00 sec 3092 KB
Case #8: : Accepted 00.00 sec 3104 KB
Case #9: : Accepted 00.00 sec 3120 KB
Case #10: : Accepted 00.00 sec 3064 KB
Case #11: : Accepted 00.01 sec 3332 KB
Case #12: : Accepted 00.01 sec 3236 KB
Case #13: : Accepted 00.01 sec 3304 KB
Case #14: : Accepted 00.01 sec 3376 KB
Case #15: : Accepted 00.13 sec 7504 KB
Case #16: : Accepted 00.14 sec 7556 KB
Case #17: : Accepted 00.15 sec 7556 KB
Case #18: : Accepted 00.13 sec 7428 KB
Case #19: : Accepted 00.10 sec 7500 KB
Case #20: : Accepted 00.13 sec 7504 KB
Case #21: : Accepted 00.07 sec 7524 KB
Case #22: : Accepted 00.13 sec 7600 KB
< prev | / | next >  
 
Judge Input #  ( | ) Judge Output #  ( | )


Comments
 
 Under Construction.
 
Categories
 
 
Free Tags