結果
| 問題 | No.789 範囲の合計 |
| コンテスト | |
| ユーザー |
kyo1
|
| 提出日時 | 2024-03-10 00:31:19 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 173 ms / 1,000 ms |
| コード長 | 2,859 bytes |
| 記録 | |
| コンパイル時間 | 885 ms |
| コンパイル使用メモリ | 99,032 KB |
| 実行使用メモリ | 34,688 KB |
| 最終ジャッジ日時 | 2024-09-29 21:24:12 |
| 合計ジャッジ時間 | 2,902 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 15 |
ソースコード
#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;
}
kyo1