結果
| 問題 |
No.789 範囲の合計
|
| コンテスト | |
| ユーザー |
kyo1
|
| 提出日時 | 2024-03-08 15:35:42 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 300 ms / 1,000 ms |
| コード長 | 2,315 bytes |
| コンパイル時間 | 3,737 ms |
| コンパイル使用メモリ | 181,852 KB |
| 実行使用メモリ | 77,164 KB |
| 最終ジャッジ日時 | 2024-09-29 18:48:29 |
| 合計ジャッジ時間 | 6,287 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 15 |
ソースコード
#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;
}
kyo1