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/invertible_binary_indexed_tree.hpp" int main(){ int n, q; cin >> n >> q; invertible_binary_indexed_tree<int> BIT(n, plus<int>(), negate<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(x, y) << endl; } } }
#line 1 "test/aoj/dsl/dsl_2_b_2.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/invertible_binary_indexed_tree.hpp" /** * @brief 可逆 Binary Indexed Tree */ template <typename T> struct invertible_binary_indexed_tree{ int N; vector<T> BIT; function<T(T, T)> f; function<T(T)> inv; T E; invertible_binary_indexed_tree(){ } invertible_binary_indexed_tree(int N, function<T(T, T)> f, function<T(T)> inv, T E): N(N), BIT(N + 1, E), f(f), inv(inv), E(E){ } invertible_binary_indexed_tree(vector<T> &A, function<T(T, T)> f, function<T(T)> inv, T E): N(A.size()), BIT(N + 1), f(f), inv(inv), 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; } T sum(int l, int r){ return f(sum(r), inv(sum(l))); } }; #line 5 "test/aoj/dsl/dsl_2_b_2.test.cpp" int main(){ int n, q; cin >> n >> q; invertible_binary_indexed_tree<int> BIT(n, plus<int>(), negate<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(x, y) << endl; } } }