icpc_library

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

View the Project on GitHub ebi-fly13/icpc_library

:heavy_check_mark: test/data_structure/Point_Add_Range_Sum.test.cpp

Depends on

Code

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

#include "../../data_structure/segtree.hpp"
#include "../../template/template.hpp"

ll op(ll a, ll b) {
    return a + b;
}

ll e() {
    return 0;
}

int main() {
    int n, q;
    std::cin >> n >> q;
    std::vector<ll> a(n);
    rep(i, 0, n) {
        std::cin >> a[i];
    }
    lib::segtree<ll, op, e> seg(a);
    while (q--) {
        int t;
        std::cin >> t;
        if (t == 0) {
            int p;
            ll x;
            std::cin >> p >> x;
            seg.set(p, seg.get(p) + x);
        } else {
            int l, r;
            std::cin >> l >> r;
            std::cout << seg.prod(l, r) << '\n';
        }
    }
}
#line 1 "test/data_structure/Point_Add_Range_Sum.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/point_add_range_sum"

#line 2 "data_structure/segtree.hpp"

#line 2 "template/template.hpp"

#include <bits/stdc++.h>

#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--)
#define all(v) v.begin(), v.end()

using ll = long long;
using ld = long double;
using ull = unsigned long long;

template <typename T> bool chmin(T &a, const T &b) {
    if (a <= b) return false;
    a = b;
    return true;
}
template <typename T> bool chmax(T &a, const T &b) {
    if (a >= b) return false;
    a = b;
    return true;
}

namespace lib {

using namespace std;

}  // namespace lib

// using namespace lib;
#line 4 "data_structure/segtree.hpp"

namespace lib {

using namespace std;

template <class S, S (*op)(S, S), S (*e)()> struct segtree {
  private:
    int n;
    int sz;
    vector<S> data;

    void update(int i) {
        data[i] = op(data[2 * i], data[2 * i + 1]);
    }

  public:
    segtree(int n) : segtree(vector<S>(n, e())) {}
    segtree(const vector<S> &v) : n((int)v.size()), sz(1) {
        while (sz < n) sz *= 2;
        data = vector<S>(2 * sz, e());
        rep(i, 0, n) {
            data[sz + i] = v[i];
        }
        rrep(i, 1, sz) update(i);
    }

    void set(int p, S x) {
        assert(0 <= p && p < n);
        p += sz;
        data[p] = x;
        while (p > 1) {
            p >>= 1;
            update(p);
        }
    }

    S get(int p) {
        assert(0 <= p && p < n);
        return data[p + sz];
    }

    S prod(int l, int r) {
        assert(0 <= l && l <= r && r <= n);
        S sml = e(), smr = e();
        l += sz;
        r += sz;
        while (l < r) {
            if (l & 1) sml = op(sml, data[l++]);
            if (r & 1) smr = op(data[--r], smr);
            l >>= 1;
            r >>= 1;
        }
        return op(sml, smr);
    }

    S all_prod() {
        return data[1];
    }

    template <class F> int max_right(int l, F f) {
        assert(0 <= l && l <= n);
        assert(f(e()));
        if (l == n) return n;
        l += sz;
        S sm = e();
        do {
            while (l % 2 == 0) l >>= 1;
            if (!f(op(sm, data[l]))) {
                while (l < sz) {
                    l = 2 * l;
                    if (f(op(sm, data[l]))) {
                        sm = op(sm, data[l]);
                        l++;
                    }
                }
                return l - sz;
            }
            sm = op(sm, data[l]);
            l++;
        } while ((l & -l) != l);
        return n;
    }

    template <class F> int min_left(int r, F f) {
        assert(0 <= r && r <= n);
        assert(f(e()));
        if (r == 0) return 0;
        r += sz;
        S sm = e();
        do {
            r--;
            while (r > 1 && (r % 2)) r >>= 1;
            if (!f(op(data[r], sm))) {
                while (r < sz) {
                    r = 2 * r + 1;
                    if (f(op(data[r], sm))) {
                        sm = op(data[r], sm);
                        r--;
                    }
                }
                return r + 1 - sz;
            }
            sm = op(data[r], sm);
        } while ((r & -r) != r);
        return 0;
    }
};

}  // namespace lib
#line 5 "test/data_structure/Point_Add_Range_Sum.test.cpp"

ll op(ll a, ll b) {
    return a + b;
}

ll e() {
    return 0;
}

int main() {
    int n, q;
    std::cin >> n >> q;
    std::vector<ll> a(n);
    rep(i, 0, n) {
        std::cin >> a[i];
    }
    lib::segtree<ll, op, e> seg(a);
    while (q--) {
        int t;
        std::cin >> t;
        if (t == 0) {
            int p;
            ll x;
            std::cin >> p >> x;
            seg.set(p, seg.get(p) + x);
        } else {
            int l, r;
            std::cin >> l >> r;
            std::cout << seg.prod(l, r) << '\n';
        }
    }
}
Back to top page