#line 1 "test/yuki/No789.test.cpp" #define PROBLEM "https://yukicoder.me/problems/no/789" #include #line 2 "src/data-structure/dynamic_segtree.hpp" #include #include #include #include #include namespace kyopro { template class dynamic_segtree { struct Node { S val; Node *l, *r, *parent; constexpr Node(const S& v = e(), Node* pt = nullptr) : val(), l(nullptr), r(nullptr), parent(pt) {} }; using uptr = std::unique_ptr; std::vector nodes; Node* root; Node* make_ptr(const S& v = e(), Node* pt = nullptr) { nodes.push_back(std::make_unique(v, pt)); return nodes.back().get(); }; const std::size_t n; Node* find(std::size_t i) { assert(0 <= i && i < n); std::size_t l = 0, r = n; Node* p = root; while (r - l > 1) { std::size_t md = (r + l) >> 1; if (i < md) { if (!p->l) { p->l = make_ptr(e(), p); } p = p->l; r = md; } else { if (!p->r) { p->r = make_ptr(e(), p); } p = p->r; l = md; } } return p; } void push(Node* p) { while (p = p->parent) { p->val = op((p->l ? p->l->val : e()), (p->r ? p->r->val : e())); } } public: explicit dynamic_segtree(std::size_t n = 0) : n(n), root(nullptr) { root = make_ptr(); } void apply(std::size_t i, const S& x) { assert(0 <= i && i < n); auto p = find(i); p->val = op(p->val, x); push(p); } void update(std::size_t i, const S& x) { assert(0 <= i && i < n); auto p = find(i); p->val = x; push(p); } S at(std::size_t i) { assert(0 <= i && i < n); return find(i)->val; } S fold(std::size_t l, std::size_t r) const { assert(0 <= l && l <= r && r <= n); if (l == r) { return e(); } return internal_fold(root, 0, n, l, r); } private: S internal_fold(const Node* p, std::size_t l, std::size_t r, std::size_t L, std::size_t R) const { if (!p || r <= L || R <= l) { return e(); } if (L <= l && r <= R) { return p->val; } std::size_t mid = (l + r) >> 1; return op(internal_fold(p->l, l, mid, L, R), internal_fold(p->r, mid, r, L, R)); } }; }; // namespace kyopro /** * @brief Dynamic Segment Tree * @see https://lorent-kyopro.hatenablog.com/entry/2021/03/12/025644 */ #line 2 "src/stream.hpp" #include #include #include #line 3 "src/internal/type_traits.hpp" #include #include #include #line 7 "src/internal/type_traits.hpp" namespace kyopro { namespace internal { template struct first_enabled {}; template struct first_enabled, Args...> { using type = T; }; template struct first_enabled, Args...> : first_enabled {}; template struct first_enabled { using type = T; }; template using first_enabled_t = typename first_enabled::type; template * = nullptr> struct int_least { using type = first_enabled_t, std::enable_if, std::enable_if, std::enable_if, std::enable_if>; }; template * = nullptr> struct uint_least { using type = first_enabled_t, std::enable_if, std::enable_if, std::enable_if, std::enable_if>; }; template using int_least_t = typename int_least::type; template using uint_least_t = typename uint_least::type; template using double_size_uint_t = uint_least_t<2 * std::numeric_limits::digits>; template using double_size_int_t = int_least_t<2 * std::numeric_limits::digits>; struct modint_base {}; template using is_modint = std::is_base_of; template using is_modint_t = std::enable_if_t::value>; // is_integral template using is_integral_t = std::enable_if_t || std::is_same_v || std::is_same_v>; }; // namespace internal }; // namespace kyopro /* * @ref https://qiita.com/kazatsuyu/items/f8c3b304e7f8b35263d8 */ #line 6 "src/stream.hpp" namespace kyopro { inline void single_read(char& c) { c = getchar_unlocked(); while (isspace(c)) c = getchar_unlocked(); } template * = nullptr> inline void single_read(T& a) { a = 0; bool is_negative = false; char c = getchar_unlocked(); while (isspace(c)) { c = getchar_unlocked(); } if (c == '-') is_negative = true, c = getchar_unlocked(); while (isdigit(c)) { a = 10 * a + (c - '0'); c = getchar_unlocked(); } if (is_negative) a *= -1; } template * = nullptr> inline void single_read(T& a) { long long x; single_read(x); a = T(x); } inline void single_read(std::string& str) noexcept { char c = getchar_unlocked(); while (isspace(c)) c = getchar_unlocked(); while (!isspace(c)) { str += c; c = getchar_unlocked(); } } template inline void read(T& x) noexcept {single_read(x);} template inline void read(Head& head, Tail&... tail) noexcept { single_read(head), read(tail...); } inline void single_write(char c) noexcept { putchar_unlocked(c); } template * = nullptr> inline void single_write(T a) noexcept { if (!a) { putchar_unlocked('0'); return; } if constexpr (std::is_signed_v) { if (a < 0) putchar_unlocked('-'), a *= -1; } constexpr int d = std::numeric_limits::digits10; char s[d + 1]; int now = d + 1; while (a) { s[--now] = (char)'0' + a % 10; a /= 10; } while (now <= d) putchar_unlocked(s[now++]); } template * = nullptr> inline void single_write(T a) noexcept { single_write(a.val()); } inline void single_write(const std::string& str) noexcept { for (auto c : str) { putchar_unlocked(c); } } template inline void write(T x) noexcept { single_write(x); } template inline void write(Head head, Tail... tail) noexcept { single_write(head); putchar_unlocked(' '); write(tail...); } template inline void put(Args... x) noexcept { write(x...); putchar_unlocked('\n'); } }; // namespace kyopro /** * @brief 高速入出力 */ #line 5 "test/yuki/No789.test.cpp" constexpr inline int op(int x, int y) { return x + y; } constexpr inline int e() { return 0; } using namespace std; using namespace kyopro; int main() { const size_t n = 1000000001; dynamic_segtree seg(n); int q; read(q); long long ans = 0; while (q--) { int type; read(type); if (!type) { size_t x; long long y; read(x, y); seg.apply(x, y); } else { size_t l, r; read(l, r); ans += seg.fold(l, r + 1); } } put(ans); }