cp-library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub Hoshinonono/cp-library

:heavy_check_mark: Test/Library Checker/Data Structure/point_add_range_sum.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/point_add_range_sum"

#include <bits/stdc++.h>
#include "DataStructure/fenwick_tree.hpp"
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int N, Q, cmd, l, r, v;
    cin >> N >> Q;
    fenwick_tree<long long> fw(N);
    for(int i = 0; i < N; i++){
        cin >> v;
        fw.add(i, v);
    }
    while(Q--){
        cin >> cmd >> l >> r;
        if(cmd == 0){
            fw.add(l, r);
        }else{
            cout << fw.sum(l, r) << '\n';
        }
    }
}
#line 1 "Test/Library Checker/Data Structure/point_add_range_sum.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/point_add_range_sum"

#include <bits/stdc++.h>
#line 1 "DataStructure/fenwick_tree.hpp"
template <class T> struct fenwick_tree {
    using U = T;

    public:
    fenwick_tree() : _n(0) {}
    fenwick_tree(int n) : _n(n), data(n) {}

    void add(int p, T x) {
        assert(0 <= p && p < _n);
        p++;
        while (p <= _n) {
            data[p - 1] += U(x);
            p += p & -p;
        }
    }

    T sum(int l, int r) {
        assert(0 <= l && l <= r && r <= _n);
        return sum(r) - sum(l);
    }

    private:
    int _n;
    std::vector<U> data;

    U sum(int r) {
        U s = 0;
        while (r > 0) {
            s += data[r - 1];
            r -= r & -r;
        }
        return s;
    }
};
#line 5 "Test/Library Checker/Data Structure/point_add_range_sum.test.cpp"
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int N, Q, cmd, l, r, v;
    cin >> N >> Q;
    fenwick_tree<long long> fw(N);
    for(int i = 0; i < N; i++){
        cin >> v;
        fw.add(i, v);
    }
    while(Q--){
        cin >> cmd >> l >> r;
        if(cmd == 0){
            fw.add(l, r);
        }else{
            cout << fw.sum(l, r) << '\n';
        }
    }
}
Back to top page