結果
| 問題 | No.789 範囲の合計 | 
| コンテスト | |
| ユーザー |  kyo1 | 
| 提出日時 | 2024-03-10 00:25:11 | 
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 179 ms / 1,000 ms | 
| コード長 | 2,615 bytes | 
| コンパイル時間 | 1,048 ms | 
| コンパイル使用メモリ | 98,364 KB | 
| 実行使用メモリ | 34,688 KB | 
| 最終ジャッジ日時 | 2024-09-29 21:24:09 | 
| 合計ジャッジ時間 | 3,278 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 15 | 
ソースコード
#include <bit>
#include <cassert>
#include <iostream>
#include <memory>
template <typename T, T Identity, auto Operate>
class DynamicSegmentTree {
 public:
  explicit DynamicSegmentTree(const std::size_t size)
      : size(std::bit_ceil(size) << 1), root(std::make_unique<Node>(Identity)) {}
  void update(const std::size_t index, const T value) {
    assert(index < size);
    update(root, 0, size, index, value);
  }
  T fold(const std::size_t left, const std::size_t right) const {  // [left, right)
    assert(left < right && right <= size);
    return fold(root, left, right, 0, size);
  }
 private:
  class Node {
   private:
    friend DynamicSegmentTree;
    friend std::unique_ptr<Node> std::make_unique<Node>(T&&);
    explicit Node(const T& value) : value(value) {}
    std::unique_ptr<Node> left;
    std::unique_ptr<Node> right;
    T value;
  };
  inline void update(std::unique_ptr<Node>& node, const std::size_t left, const std::size_t right,
                     const std::size_t index, const T& value) {
    if (!node) node = std::make_unique<Node>(Identity);
    if (left + 1 == right) {
      node->value = value;
      return;
    }
    const auto i = left + ((right - left) >> 1);
    if (index < i) {
      update(node->left, left, i, index, value);
    } else {
      update(node->right, i, right, index, value);
    }
    node->value = Operate(value_of_node(node->left), value_of_node(node->right));
  }
  inline T fold(const std::unique_ptr<Node>& node, const std::size_t left, const std::size_t right,
                const std::size_t a, const std::size_t b) const {
    if (!node || right <= a || b <= left) return Identity;
    if (left <= a && b <= right) return node->value;
    const auto i = a + ((b - a) >> 1);
    return Operate(fold(node->left, left, right, a, i), fold(node->right, left, right, i, b));
  }
  inline T value_of_node(const std::unique_ptr<Node>& node) const {
    return node ? node->value : Identity;
  }
  const std::size_t size;
  std::unique_ptr<Node> root;
};
int main() {
  std::cin.tie(nullptr)->sync_with_stdio(false);
  int N;
  std::cin >> N;
  DynamicSegmentTree<int64_t, 0, [](const auto a, const auto b) { return a + b; }> dst(1000000001);
  int64_t res = 0;
  for (int i = 0; i < N; i++) {
    int q;
    std::cin >> q;
    switch (q) {
      case 0: {
        int x, y;
        std::cin >> x >> y;
        dst.update(x, dst.fold(x, x + 1) + y);
        break;
      }
      case 1: {
        int l, r;
        std::cin >> l >> r;
        r++;
        res += dst.fold(l, r);
      }
    }
  }
  std::cout << res << '\n';
  return 0;
}
            
            
            
        