結果

問題 No.789 範囲の合計
ユーザー kyo1kyo1
提出日時 2024-03-08 15:35:42
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 288 ms / 1,000 ms
コード長 2,315 bytes
コンパイル時間 3,485 ms
コンパイル使用メモリ 183,172 KB
実行使用メモリ 77,156 KB
最終ジャッジ日時 2024-03-08 15:35:50
合計ジャッジ時間 8,024 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 288 ms
77,156 KB
testcase_03 AC 45 ms
6,676 KB
testcase_04 AC 273 ms
77,156 KB
testcase_05 AC 237 ms
77,156 KB
testcase_06 AC 236 ms
77,156 KB
testcase_07 AC 48 ms
6,676 KB
testcase_08 AC 264 ms
77,156 KB
testcase_09 AC 225 ms
77,156 KB
testcase_10 AC 229 ms
77,156 KB
testcase_11 AC 237 ms
77,156 KB
testcase_12 AC 277 ms
77,156 KB
testcase_13 AC 2 ms
6,676 KB
testcase_14 AC 2 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bit>
#include <cassert>
#include <cstdint>
#include <ext/pb_ds/assoc_container.hpp>
#include <iostream>
#include <optional>
#include <random>
#include <unordered_map>

template <typename T, T identity, auto Operate>
class DynamicSegmentTree {
 public:
  explicit DynamicSegmentTree(const std::size_t size) : size(std::bit_ceil(size) << 1) {}

  void update(const std::size_t index, const T value) {
    assert(index < size);
    nodes[index + size] = value;
    for (std::size_t i = (index + size) >> 1; i > 0; i >>= 1) {
      nodes[i] = Operate(value_of_index(i << 1), value_of_index((i << 1) | 1));
    }
  }

  T fold(const std::size_t left, const std::size_t right) const {  // [left, right)
    assert(left < right && right <= size);
    T x = identity;
    T y = identity;
    for (std::size_t l = left + size, r = right + size; l < r; l >>= 1, r >>= 1) {
      if (l % 2 != 0) x = Operate(x, value_of_index(l++));
      if (r % 2 != 0) y = Operate(value_of_index(--r), y);
    }
    return Operate(x, y);
  }

 private:
  class Hash {
   public:
    std::size_t operator()(const std::size_t x) const {
      std::uint64_t res = static_cast<std::uint64_t>(x) + seed + 0x9e3779b97f4a7c15;
      res = (res ^ (res >> 30)) * 0xbf58476d1ce4e5b9;
      res = (res ^ (res >> 27)) * 0x94d049bb133111eb;
      return static_cast<std::size_t>(res ^ (res >> 31));
    }

   private:
    static inline const std::uint64_t seed = static_cast<std::uint64_t>(std::random_device()());
  };

  T value_of_index(std::size_t index) const {
    if (const auto it = nodes.find(index); it != nodes.end()) return it->second;
    return identity;
  }

  const std::size_t size;

  __gnu_pbds::gp_hash_table<std::size_t, T, Hash> nodes;
};

using namespace std;

int main() {
  std::cin.tie(nullptr)->sync_with_stdio(false);
  int N;
  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;
    cin >> q;
    switch (q) {
      case 0: {
        int x, y;
        cin >> x >> y;
        dst.update(x, dst.fold(x, x + 1) + y);
        break;
      }
      case 1: {
        int l, r;
        cin >> l >> r;
        r++;
        res += dst.fold(l, r);
      }
    }
  }
  cout << res << '\n';
  return 0;
}
0