結果

問題 No.789 範囲の合計
ユーザー tonegawatonegawa
提出日時 2021-04-01 20:34:18
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 894 ms / 1,000 ms
コード長 1,909 bytes
コンパイル時間 1,009 ms
コンパイル使用メモリ 98,068 KB
実行使用メモリ 59,536 KB
最終ジャッジ日時 2024-05-10 06:45:16
合計ジャッジ時間 9,223 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 840 ms
59,536 KB
testcase_03 AC 80 ms
5,376 KB
testcase_04 AC 844 ms
59,412 KB
testcase_05 AC 624 ms
53,264 KB
testcase_06 AC 705 ms
54,288 KB
testcase_07 AC 122 ms
5,376 KB
testcase_08 AC 606 ms
45,844 KB
testcase_09 AC 539 ms
42,892 KB
testcase_10 AC 894 ms
59,152 KB
testcase_11 AC 794 ms
58,132 KB
testcase_12 AC 767 ms
58,128 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <vector>
#include <cassert>
#include <algorithm>

template<typename Idx = int, int bitlen = sizeof(Idx) * 8>
struct pseudo_segment_tree{
  Idx M, N;
  pseudo_segment_tree(Idx n):N(n){
    M = 1;
    while(M < N) M <<= 1;
  }
  int depth(Idx k){
    if(bitlen <= 32) return 31 - __builtin_clz(k + 1);
    return 63 - __builtin_clzll(k + 1);
  }
  Idx width(Idx k){
    return M >> depth(k);
  }
  bool is_leaf(Idx k){
    return width(k) == (Idx)1;
  }
  std::pair<Idx, Idx> index_to_range(Idx k){
    assert(0 <= k && k < 2 * M - 1);
    int dep = depth(k);
    Idx offset = (k + 1) - ((Idx)1 << dep);
    Idx wid = M >> dep;
    return std::make_pair(offset * wid, (offset + 1) * wid);
  }
  std::vector<Idx> range_to_index(Idx l, Idx r){
    l = std::max(l, 0), r = std::min(r, N);
    assert(l <= r);
    l += M, r += M;
    std::vector<Idx> left, right;
    while(l < r){
      if(l & 1) left.push_back((l++) - 1);
      if(r & 1) right.push_back((--r) - 1);
      l >>= 1;
      r >>= 1;
    }
    reverse(right.begin(), right.end());
    left.insert(left.end(), right.begin(), right.end());
    return left;
  }
  std::vector<Idx> point_to_index(Idx k){
    assert(0 <= k && k < N);
    k += M - 1;
    std::vector<Idx> ret{k};
    while(k){
      k = (k - 1) >> 1;
      ret.push_back(k);
    }
    return ret;
  }
};

#include <iostream>
#include <unordered_map>
int main(){
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);
  int q;std::cin >> q;
  pseudo_segment_tree segment(1000000001);
  std::unordered_map<int, long long> sum;
  long long ans = 0;
  for(int i=0;i<q;i++){
    int t, a, b;std::cin >> t >> a >> b;
    if(t == 0){
      auto indices = segment.point_to_index(a);
      for(auto idx : indices) sum[idx] += b;
    }else{
      auto indices = segment.range_to_index(a, b + 1);
      for(auto idx : indices) ans += sum[idx];
    }
  }
  std::cout << ans << '\n';
}
0