#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_2_B" #include <iostream> #include "Mylib/AlgebraicStructure/Group/sum.cpp" #include "Mylib/DataStructure/FenwickTree/fenwick_tree.cpp" #include "Mylib/IO/input_tuples.cpp" namespace hl = haar_lib; int main() { int n, q; std::cin >> n >> q; auto fen = hl::fenwick_tree<hl::sum_group<int>>(n); for (auto [type, x, y] : hl::input_tuples<int, int, int>(q)) { if (type == 0) { fen.update(x - 1, y); } else { std::cout << fen.fold(x - 1, y) << std::endl; } } return 0; }
#line 1 "test/aoj/DSL_2_B/main.fenwick_tree.test.cpp" #define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_2_B" #include <iostream> #line 2 "Mylib/AlgebraicStructure/Group/sum.cpp" namespace haar_lib { template <typename T> struct sum_group { using value_type = T; value_type operator()() const { return 0; } value_type operator()(const value_type &a, const value_type &b) const { return a + b; } value_type inv(const value_type &a) const { return -a; } }; } // namespace haar_lib #line 2 "Mylib/DataStructure/FenwickTree/fenwick_tree.cpp" #include <cassert> #include <vector> namespace haar_lib { template <typename AbelianGroup> class fenwick_tree { public: using value_type = typename AbelianGroup::value_type; private: AbelianGroup G_; int size_; std::vector<value_type> data_; public: fenwick_tree() {} fenwick_tree(int size) : size_(size), data_(size + 1, G_()) {} void update(int i, const value_type &val) { assert(0 <= i and i < size_); i += 1; // 1-index while (i <= size_) { data_[i] = G_(data_[i], val); i += i & (-i); } } value_type fold(int i) const { // [0, i) assert(0 <= i and i <= size_); value_type ret = G_(); while (i > 0) { ret = G_(ret, data_[i]); i -= i & (-i); } return ret; } value_type fold(int l, int r) const { // [l, r) assert(0 <= l and l <= r and r <= size_); return G_(fold(r), G_.inv(fold(l))); } value_type operator[](int x) const { return fold(x, x + 1); } }; } // namespace haar_lib #line 2 "Mylib/IO/input_tuples.cpp" #include <initializer_list> #line 4 "Mylib/IO/input_tuples.cpp" #include <tuple> #include <utility> #line 6 "Mylib/IO/input_tuple.cpp" namespace haar_lib { template <typename T, size_t... I> static void input_tuple_helper(std::istream &s, T &val, std::index_sequence<I...>) { (void) std::initializer_list<int>{(void(s >> std::get<I>(val)), 0)...}; } template <typename T, typename U> std::istream &operator>>(std::istream &s, std::pair<T, U> &value) { s >> value.first >> value.second; return s; } template <typename... Args> std::istream &operator>>(std::istream &s, std::tuple<Args...> &value) { input_tuple_helper(s, value, std::make_index_sequence<sizeof...(Args)>()); return s; } } // namespace haar_lib #line 8 "Mylib/IO/input_tuples.cpp" namespace haar_lib { template <typename... Args> class InputTuples { struct iter { using value_type = std::tuple<Args...>; value_type value; bool fetched = false; int N, c = 0; value_type operator*() { if (not fetched) { std::cin >> value; } return value; } void operator++() { ++c; fetched = false; } bool operator!=(iter &) const { return c < N; } iter(int N) : N(N) {} }; int N; public: InputTuples(int N) : N(N) {} iter begin() const { return iter(N); } iter end() const { return iter(N); } }; template <typename... Args> auto input_tuples(int N) { return InputTuples<Args...>(N); } } // namespace haar_lib #line 7 "test/aoj/DSL_2_B/main.fenwick_tree.test.cpp" namespace hl = haar_lib; int main() { int n, q; std::cin >> n >> q; auto fen = hl::fenwick_tree<hl::sum_group<int>>(n); for (auto [type, x, y] : hl::input_tuples<int, int, int>(q)) { if (type == 0) { fen.update(x - 1, y); } else { std::cout << fen.fold(x - 1, y) << std::endl; } } return 0; }