結果

問題 No.789 範囲の合計
ユーザー Suu0313Suu0313
提出日時 2021-04-06 15:51:34
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 870 ms / 1,000 ms
コード長 1,319 bytes
コンパイル時間 2,697 ms
コンパイル使用メモリ 209,156 KB
実行使用メモリ 45,720 KB
最終ジャッジ日時 2023-09-02 00:14:11
合計ジャッジ時間 11,831 ms
ジャッジサーバーID
(参考情報)
judge15 / judge16
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 854 ms
42,100 KB
testcase_03 AC 175 ms
4,376 KB
testcase_04 AC 870 ms
42,760 KB
testcase_05 AC 732 ms
42,200 KB
testcase_06 AC 747 ms
42,088 KB
testcase_07 AC 332 ms
4,380 KB
testcase_08 AC 769 ms
45,720 KB
testcase_09 AC 719 ms
42,820 KB
testcase_10 AC 572 ms
25,184 KB
testcase_11 AC 638 ms
42,132 KB
testcase_12 AC 648 ms
42,124 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0