結果
問題 | No.789 範囲の合計 |
ユーザー | tonegawa |
提出日時 | 2023-05-01 17:09:25 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 149 ms / 1,000 ms |
コード長 | 4,736 bytes |
コンパイル時間 | 1,174 ms |
コンパイル使用メモリ | 102,108 KB |
実行使用メモリ | 70,144 KB |
最終ジャッジ日時 | 2024-11-20 15:06:35 |
合計ジャッジ時間 | 3,692 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 135 ms
40,704 KB |
testcase_03 | AC | 141 ms
70,144 KB |
testcase_04 | AC | 149 ms
46,336 KB |
testcase_05 | AC | 108 ms
38,656 KB |
testcase_06 | AC | 112 ms
40,704 KB |
testcase_07 | AC | 115 ms
62,464 KB |
testcase_08 | AC | 123 ms
51,712 KB |
testcase_09 | AC | 109 ms
46,464 KB |
testcase_10 | AC | 117 ms
25,984 KB |
testcase_11 | AC | 116 ms
51,584 KB |
testcase_12 | AC | 109 ms
51,584 KB |
testcase_13 | AC | 2 ms
5,248 KB |
testcase_14 | AC | 2 ms
5,248 KB |
ソースコード
#include <vector> #include <numeric> #include <limits> #include <cassert> template<typename monoid, typename Val> struct persistent_segment_tree_iter; #include <random> unsigned long long random_once(){ static unsigned long long ret = 0; if(ret == 0){ std::random_device seed_gen; std::mt19937_64 engine(seed_gen()); return ret = engine(); } return ret; } int X = random_once(); template<typename monoid, typename Val, typename I> struct persistent_segment_tree{ private: using node = persistent_segment_tree<monoid, Val, I>; I idx; node *l, *r; Val val, sum; persistent_segment_tree(){} persistent_segment_tree(I idx, Val val): idx(idx), l(nullptr), r(nullptr), val(val), sum(val){} static node *make_node(I idx, Val x = monoid::template id<Val>()){ return new node(idx, x); } static node *copy_node(node *v){ assert(v); return new node(*v); } static node *eval(node *v){ v->sum = v->val; if(v->l) v->sum = monoid::template merge<Val>(v->l->sum, v->sum); if(v->r) v->sum = monoid::template merge<Val>(v->sum, v->r->sum); return v; } static void set(node* &v, int k, Val x, I l, I r){ if(!v){ v = make_node(k, x); return; } v = copy_node(v); if(v->idx == k){ v->val = x; eval(v); return; } I mid = ((long long)l + r) >> 1; if(k < mid){ if(v->idx < k) std::swap(v->idx, k), std::swap(v->val, x); set(v->l, k, x, l, mid); }else{ if(v->idx > k) std::swap(v->idx, k), std::swap(v->val, x); set(v->r, k, x, mid, r); } eval(v); } static void update(node* &v, int k, Val x, I l, I r){ if(!v){ v = make_node(k, x); return; } v = copy_node(v); if(v->idx == k){ v->val = monoid::template merge<Val>(v->val, x); eval(v); return; } I mid = ((long long)l + r) >> 1; if(k < mid){ if(v->idx < k) std::swap(v->idx, k), std::swap(v->val, x); update(v->l, k, x, l, mid); }else{ if(v->idx > k) std::swap(v->idx, k), std::swap(v->val, x); update(v->r, k, x, mid, r); } eval(v); } static Val get(node *v, int k, I l, I r){ if(!v) return monoid::template id<Val>(); if(v->idx == k) return v->val; I mid = ((long long)l + r) >> 1; if(k < mid) return get(v->l, k, l, mid); else return get(v->r, k, mid, r); } static Val query(node *v, I a, I b, I l, I r, int d, const I mask){ if(!v || b <= l || r <= a) return monoid::template id<Val>(); if(a <= l && r <= b) return v->sum; I mid = ((long long)l + r) >> 1; I vx = (v->idx ^ X) & mask; if((X >> d) & 1){ Val ret = query(v->r, a, b, l, mid, d - 1, mask); if(a <= vx && vx < b) ret = monoid::template merge<Val>(ret, v->val); return monoid::template merge<Val>(ret, query(v->l, a, b, mid, r, d - 1, mask)); }else{ Val ret = query(v->l, a, b, l, mid, d - 1, mask); if(a <= vx && vx < b) ret = monoid::template merge<Val>(ret, v->val); return monoid::template merge<Val>(ret, query(v->r, a, b, mid, r, d - 1, mask)); } } friend persistent_segment_tree_iter<monoid, Val>; }; template<typename monoid, typename Val> struct persistent_segment_tree_iter{ private: using I = int; using node = persistent_segment_tree<monoid, Val, I>; using iter = persistent_segment_tree_iter<monoid, Val>; I maxx; int h; node *root; persistent_segment_tree_iter(I maxx, node *v): maxx(maxx), root(v){} public: persistent_segment_tree_iter(I _maxx):root(nullptr){ I tmp = 1; int c = 0; while(tmp < _maxx) tmp <<= 1, c++; h = c; maxx = tmp; } iter set(I k, Val x){ node *r = root; node::set(r, (X ^ k) & (maxx - 1), x, 0, maxx); return iter(maxx, r); } iter update(int k, Val x){ node *r = root; node::update(r, (X ^ k) & (maxx - 1), x, 0, maxx); return iter(maxx, r); } Val get(I k){ return node::get(root, (X ^ k) & (maxx - 1), 0, maxx); } Val query(I l, I r){ return node::query(root, l, r, 0, maxx, h - 1, maxx - 1); } }; struct point_add_range_sum{ template<typename T> static T id(){ return 0; } template<typename T> static T update(T a, T b){ return a + b; } template<typename T> static T merge(T a, T b){ return a + b; } }; #include <iostream> int main(){ std::cin.tie(nullptr); std::ios::sync_with_stdio(false); int n; std::cin >> n; persistent_segment_tree_iter<point_add_range_sum, int> seg(1000000001); long long ans = 0; for(int i = 0; i < n; i++){ int a, b, c; std::cin >> a >> b >> c; if(!a){ seg = seg.update(b, c); }else{ ans += seg.query(b, c + 1); } } std::cout << ans << '\n'; }