結果

問題 No.789 範囲の合計
ユーザー kyo1kyo1
提出日時 2024-03-10 00:31:19
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 183 ms / 1,000 ms
コード長 2,859 bytes
コンパイル時間 1,212 ms
コンパイル使用メモリ 98,864 KB
実行使用メモリ 34,816 KB
最終ジャッジ日時 2024-03-10 00:31:23
合計ジャッジ時間 3,823 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 1 ms
6,676 KB
testcase_02 AC 170 ms
28,672 KB
testcase_03 AC 51 ms
6,676 KB
testcase_04 AC 183 ms
31,872 KB
testcase_05 AC 166 ms
27,520 KB
testcase_06 AC 142 ms
28,800 KB
testcase_07 AC 45 ms
6,676 KB
testcase_08 AC 170 ms
34,816 KB
testcase_09 AC 159 ms
31,872 KB
testcase_10 AC 134 ms
20,096 KB
testcase_11 AC 113 ms
28,288 KB
testcase_12 AC 108 ms
28,288 KB
testcase_13 AC 1 ms
6,676 KB
testcase_14 AC 2 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bit>
#include <cassert>
#include <iostream>
#include <memory>

template <typename F>
class FixPoint : private F {
 public:
  template <typename G>
  explicit constexpr FixPoint(G&& g) : F(std::forward<G>(g)) {}

  template <typename... Args>
  constexpr decltype(auto) operator()(Args&&... args) const {
    return F::operator()(*this, std::forward<Args>(args)...);
  }
};

template <typename F>
FixPoint(F&&) -> FixPoint<std::decay_t<F>>;

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);
    FixPoint([&](const auto& rec, std::unique_ptr<Node>& node, const std::size_t left,
                 const std::size_t right) {
      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) {
        rec(node->left, left, i);
      } else {
        rec(node->right, i, right);
      }
      node->value = Operate(value_of_node(node->left), value_of_node(node->right));
    })(root, 0, size);
  }

  T fold(const std::size_t left, const std::size_t right) const {  // [left, right)
    assert(left < right && right <= size);
    return FixPoint([&](const auto& rec, const std::unique_ptr<Node>& node, const std::size_t l,
                        const std::size_t r) {
      if (!node || right <= l || r <= left) return Identity;
      if (left <= l && r <= right) return node->value;
      const auto i = l + ((r - l) >> 1);
      return Operate(rec(node->left, l, i), rec(node->right, i, r));
    })(root, 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 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;
}
0