結果
| 問題 |
No.789 範囲の合計
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-09-09 00:32:37 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,422 bytes |
| コンパイル時間 | 1,811 ms |
| コンパイル使用メモリ | 179,848 KB |
| 実行使用メモリ | 87,196 KB |
| 最終ジャッジ日時 | 2024-06-27 15:19:09 |
| 合計ジャッジ時間 | 4,973 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 2 TLE * 1 -- * 12 |
ソースコード
#include"bits/stdc++.h"
using namespace std;
#define REP(k,m,n) for(int (k)=(m);(k)<(n);(k)++)
#define rep(i,n) REP((i),0,(n))
using ll = long long;
template<typename T>
class DynamicSegmentTree {
private:
using F = function<T(T, T)>; // モノイド型
ll n; // 横幅
F f; // モノイド
T e; // モノイド単位元
map<ll, T> data;
// 存在しないindexではeを返します
T& at(ll idx) {
if (data.find(idx) == data.end())data[idx] = e;
return data[idx];
}
public:
// init忘れに注意
DynamicSegmentTree() {}
DynamicSegmentTree(F f, T e) :f(f), e(e) {}
void init(ll n_) {
n = 1;
while (n < n_)n <<= 1;
}
void set_val(ll idx, T val) {
idx += n;
at(idx) = val;
while (idx >>= 1) {
at(idx) = f(
at((idx << 1) | 0),
at((idx << 1) | 1)
);
}
}
T query(ll a, ll b) {
// [a,b)
T vl = e, vr = e;
for (int l = a + n, r = b + n; l < r; l >>= 1, r >>= 1) {
if (l & 1)vl = f(vl, at(l++)); // unknown
if (r & 1)vr = f(at(--r), vr); // unknown
}
return f(vl, vr);
}
};
int main()
{
int n;
cin >> n;
function<ll(ll, ll)> f = [](ll a, ll b) {return a + b; };
DynamicSegmentTree<ll> dst(f, 0);
dst.init(1e9 + 10);
ll res = 0;
while (n--) {
int com, x, y;
cin >> com >> x >> y;
if (com == 0) {
ll now = dst.query(x, x + 1);
dst.set_val(x, now + y);
}
else {
res += dst.query(x, y + 1);
}
}
cout << res << endl;
return 0;
}