結果
問題 | No.789 範囲の合計 |
ユーザー |
|
提出日時 | 2021-04-06 15:51:34 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
TLE
(最新)
AC
(最初)
|
実行時間 | - |
コード長 | 1,319 bytes |
コンパイル時間 | 2,889 ms |
コンパイル使用メモリ | 201,708 KB |
最終ジャッジ日時 | 2025-01-20 12:26:50 |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 8 TLE * 7 |
ソースコード
#include<bits/stdc++.h> using namespace std; template<typename T, typename U = int> struct DynamicSegmentTree{ using OP = function<T(T,T)>; U n, sz; unordered_map<U, T> node; T t; OP op; DynamicSegmentTree(U n, T t, OP op): n(n), t(t), op(op){ sz = 1; while(sz < n) sz <<= 1; } void update(U k, T x){ k += sz; node[k] = x; while(k >>= 1){ T l = node.find(2*k)==node.end() ? t : node[2*k]; T r = node.find(2*k+1)==node.end() ? t : node[2*k+1]; node[k] = op(l, r); } } T query(int a, int b){ T L = t, R = t; for(a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if(a & 1){ T x = node.find(a)==node.end() ? t : node[a]; L = op(L, x); a++; } if(b & 1){ --b; T x = node.find(b)==node.end() ? t : node[b]; R = op(x, R); } } return op(L, R); } }; int main(){ int n = 1000000000, q; cin >> q; DynamicSegmentTree<int64_t, int> seg(n+1, 0, [](int64_t a, int64_t b){ return a+b; }); int64_t ans = 0; while(q--){ int t; cin >> t; if(!t){ int p, x; cin >> p >> x; auto a = seg.query(p, p+1); seg.update(p, a + x); }else{ int l, r; cin >> l >> r; auto res = seg.query(l, r+1); ans += res; } } cout << ans << endl; }