結果
問題 |
No.789 範囲の合計
|
ユーザー |
|
提出日時 | 2019-02-17 13:08:43 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
MLE
|
実行時間 | - |
コード長 | 2,125 bytes |
コンパイル時間 | 1,169 ms |
コンパイル使用メモリ | 92,084 KB |
最終ジャッジ日時 | 2025-01-06 21:12:51 |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 8 MLE * 7 |
ソースコード
#include <iostream> #include <memory> #define NDEBUG #include <cassert> template <class Monoid> class segtree_node { public: using value_type = typename Monoid::value_type; private: using node_t = std::unique_ptr<segtree_node<Monoid>>; node_t lch, rch; int n; value_type value; void require_children() { if (!lch) { lch = std::make_unique<segtree_node>(n / 2); rch = std::make_unique<segtree_node>(n - n / 2); } } public: segtree_node(int n): n(n), value(Monoid::zero) {} value_type get(int i) { assert(0 <= i && i < n); if (n == 1) { return value; } else if (!lch) { return Monoid::zero; } else if (i < n / 2) { return lch->get(i); } else { return rch->get(i - n / 2); } } void update(int i, value_type x) { assert(0 <= i && i < n); if (n == 1) { value = x; } else { require_children(); if (i < n / 2) lch->update(i, x); else rch->update(i - n / 2, x); value = Monoid::product(lch->value, rch->value); } } value_type product(int from, int to) { if (to <= 0 || n <= from) return Monoid::zero; if (from <= 0 && n <= to) return value; require_children(); auto lp = lch->product(from, to), rp = rch->product(from - n / 2, to - n / 2); return Monoid::product(lp, rp); } }; struct ll_sum { using value_type = long long; static constexpr value_type zero = 0; static long long product(value_type x, value_type y) { return x + y; } }; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); int n; std::cin >> n; int t, x, y; segtree_node<ll_sum> segtree(1000000001); long long sum = 0; for (int i = 0; i < n; i++) { std::cin >> t >> x >> y; if (t == 0) { auto current = segtree.get(x); segtree.update(x, current + y); } else { sum += segtree.product(x, y + 1); } } std::cout << sum << std::endl; }