結果
| 問題 |
No.789 範囲の合計
|
| コンテスト | |
| ユーザー |
🍮かんプリン
|
| 提出日時 | 2021-09-08 18:27:50 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 101 ms / 1,000 ms |
| コード長 | 3,309 bytes |
| コンパイル時間 | 1,861 ms |
| コンパイル使用メモリ | 180,684 KB |
| 実行使用メモリ | 9,276 KB |
| 最終ジャッジ日時 | 2024-12-26 06:56:49 |
| 合計ジャッジ時間 | 3,700 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 15 |
ソースコード
/**
* @FileName a.cpp
* @Author kanpurin
* @Created 2021.09.08 18:23:55
**/
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
template< typename Monoid >
struct DynamicSegmentTree {
private:
struct Node;
using Func = function<Monoid(Monoid,Monoid)>;
using node_ptr = unique_ptr<Node>;
Func F;
Monoid e;
size_t n;
struct Node {
size_t index;
Monoid value,prod;
node_ptr left, right;
Node(size_t index, Monoid value) : index(index),
value(value),
prod(value),
left(nullptr),
right(nullptr) {}
};
void node_update(node_ptr &np) const {
np->prod = F(F(np->left?np->left->prod:e,np->value),
np->right?np->right->prod:e);
}
void _update(node_ptr& t, size_t a, size_t b, size_t p, Monoid &val) const {
if (!t) {
t = make_unique<Node>(p, val);
return;
}
if (t->index == p) {
t->value = val;
node_update(t);
return;
}
size_t c = (a+b)/2;
if (p < c) {
if (t->index < p) swap(t->index,p), swap(t->value,val);
_update(t->left,a,c,p,val);
}
else {
if (p < t->index) swap(p,t->index), swap(val,t->value);
_update(t->right,c,b,p,val);
}
node_update(t);
}
Monoid _get(const node_ptr& t, size_t a, size_t b, size_t l, size_t r) const {
if (!t || b <= l || r <= a) return e;
if (l <= a && b <= r) return t->prod;
size_t c = (a + b) / 2;
Monoid result = _get(t->left, a, c, l, r);
if (l <= t->index && t->index < r) result = F(result, t->value);
return F(result, _get(t->right, c, b, l, r));
}
void _print(node_ptr& t) {
if (!t) return;
cout << "[" << t->index << "] : " << t->value << endl;
_print(t->left);
_print(t->right);
}
node_ptr root;
public:
DynamicSegmentTree(size_t n, const Func f, const Monoid &e) : n(n),
F(f),
e(e),
root(nullptr) {}
void update_query(size_t x, Monoid val) {
assert(x < n);
_update(root, 0, n, x, val);
}
Monoid get_query(size_t l, size_t r) const {
assert(l <= r && r <= n);
return _get(root, 0, n, l, r);
}
Monoid operator[](int x) const {
return get_query(x,x+1);
}
void print() {
_print(root);
}
size_t size() {
return n;
}
};
int main() {
int n;scanf("%d",&n);
DynamicSegmentTree<ll> seg(1000000001,[](ll a,ll b){return a+b;},0);
unordered_map<int,int> mp;
ll ans = 0;
for (int i = 0; i < n; i++) {
int t,x,y;scanf("%d%d%d",&t,&x,&y);
if (t == 0) {
mp[x] += y;
seg.update_query(x,mp[x]);
}
else {
ans += seg.get_query(x,y+1);
}
}
printf("%lld\n",ans);
return 0;
}
🍮かんプリン