This documentation is automatically generated by online-judge-tools/verification-helper
View the Project on GitHub SSRS-cp/cp_library
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_2_B" #include <bits/stdc++.h> using namespace std; #include "../../../data_structure/sequence/binary_indexed_tree.hpp" int main(){ int n, q; cin >> n >> q; binary_indexed_tree<int> BIT(n, plus<int>(), 0); for (int i = 0; i < q; i++){ int com, x, y; cin >> com >> x >> y; if (com == 0){ x--; BIT.add(x, y); } if (com == 1){ x--; cout << BIT.sum(y) - BIT.sum(x) << endl; } } }
#line 1 "test/aoj/dsl/dsl_2_b.test.cpp" #define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_2_B" #include <bits/stdc++.h> using namespace std; #line 2 "data_structure/sequence/binary_indexed_tree.hpp" /** * @brief Binary Indexed Tree */ template <typename T> struct binary_indexed_tree{ int N; vector<T> BIT; function<T(T, T)> f; T E; binary_indexed_tree(){ } binary_indexed_tree(int N, function<T(T, T)> f, T E): N(N), BIT(N + 1, E), f(f), E(E){ } binary_indexed_tree(vector<T> &A, function<T(T, T)> f, T E): N(A.size()), BIT(N + 1), f(f), E(E){ for (int i = 0; i < N; i++){ BIT[i + 1] = A[i]; } for (int i = 1; i < N; i++){ if (i + (i & -i) <= N){ BIT[i + (i & -i)] = f(BIT[i + (i & -i)], BIT[i]); } } } void add(int i, T x){ i++; while (i <= N){ BIT[i] = f(BIT[i], x); i += i & -i; } } T sum(int i){ T ans = E; while (i > 0){ ans = f(ans, BIT[i]); i -= i & -i; } return ans; } }; #line 5 "test/aoj/dsl/dsl_2_b.test.cpp" int main(){ int n, q; cin >> n >> q; binary_indexed_tree<int> BIT(n, plus<int>(), 0); for (int i = 0; i < q; i++){ int com, x, y; cin >> com >> x >> y; if (com == 0){ x--; BIT.add(x, y); } if (com == 1){ x--; cout << BIT.sum(y) - BIT.sum(x) << endl; } } }