結果
| 問題 |
No.789 範囲の合計
|
| コンテスト | |
| ユーザー |
le_panda_noir
|
| 提出日時 | 2020-04-12 15:58:46 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 160 ms / 1,000 ms |
| コード長 | 2,009 bytes |
| コンパイル時間 | 1,309 ms |
| コンパイル使用メモリ | 101,124 KB |
| 最終ジャッジ日時 | 2025-01-09 17:44:41 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 15 |
ソースコード
#include <iostream>
#include <algorithm>
#include <vector>
#include <functional>
using namespace std;
#define in(v) v; cin >> v;
#define rep(i,n) for(int i=0;i<(n);++i)
#define all(f,c,...) (([&](decltype((c)) cccc) { return (f)(begin(cccc), end(cccc), ## __VA_ARGS__); })(c))
template<class T> struct SegTree {
private:
int size;
vector<T> v;
using F = function<T(T,T)>;
F f; T default_value;
public:
SegTree(int n, T default_value, F f) : default_value(default_value), f(f) {
size = 1;
while (size < n) size *= 2;
v.resize(2 * size - 1, default_value);
}
void update(int index, T val) {
index = index + size - 1;
v[index] += val;
while (index > 0) {
index = (index - 1) / 2;
v[index] = f(v[2 * index + 1], v[2 * index + 2]);
}
}
T query(int l, int r) { return query(l, r, 0, size, 0); }
T query(int l, int r, int cur_l, int cur_r, int index) {
// [l, r)に対するクエリ
if (cur_r <= l || r <= cur_l) return default_value;
if (l <= cur_l && cur_r <= r) return v[index];
int mid = (cur_l + cur_r) / 2;
return f(query(l, r, cur_l, mid, 2 * index + 1), query(l, r, mid, cur_r, 2 * index + 2));
}
};
int main() {
int in(N);
SegTree<int> T(100000, 0, [](int a, int b){return a + b;});
pair<int, pair<int, int>> query[N];
vector<int> A;
rep(i, N) {
int t;
cin >> t >> query[i].second.first >> query[i].second.second;
query[i].first = t;
if (t == 0)
A.push_back(query[i].second.first);
}
all(sort, A);
A.erase(all(unique, A), A.end());
long long ans = 0;
rep(i, N) {
int t = query[i].first;
if (t == 0) {
auto [x, y] = query[i].second;
T.update(all(lower_bound, A, x) - A.begin(), y);
} else {
auto [l, r] = query[i].second;
l = all(lower_bound, A, l) - A.begin();
r = all(lower_bound, A, r+1) - A.begin();
ans += T.query(l, r);
}
}
cout << ans << endl;
return 0;
}
le_panda_noir