結果

問題 No.789 範囲の合計
ユーザー kk
提出日時 2021-03-12 01:39:22
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,425 bytes
コンパイル時間 2,541 ms
コンパイル使用メモリ 203,264 KB
実行使用メモリ 6,524 KB
最終ジャッジ日時 2024-04-21 13:37:40
合計ジャッジ時間 4,135 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 2 ms
5,376 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 48 ms
5,628 KB
testcase_09 AC 45 ms
5,628 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

template <typename T>
class fenwick_tree {
  vector<T> data;
public:
  fenwick_tree() {}
  // manage data in [1, n]
  fenwick_tree(int n) : data(n + 1) {}
  
  void init() {
    fill(data.begin(), data.end(), 0);
  }
  
  // return sum in [1, i]
  T sum(int i){
    T s = 0;
    while(i > 0){
      s += data[i];
      i -= i & -i;
    }
    return s;
  }
  
  // return sum in [l, r]
  T sum(int l, int r) {
    return sum(r) - sum(l-1);
  }
  
  void add(int i, T x){
    while(i < (int)data.size()){
      data[i] += x;
      i += i & -i;
    }
  }
};

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int n;
  cin >> n;

  vector<int> a;
  vector<int> t(n), x(n), y(n);
  for (int i = 0; i < n; i++) {
    cin >> t[i] >> x[i] >> y[i];
    if (t[i] == 0)
      a.push_back(x[i]);
    else {
      a.push_back(x[i]);
      a.push_back(y[i]);
    }
  }

  sort(a.begin(), a.end());
  a.erase(unique(a.begin(), a.end()), a.end());
  fenwick_tree<long long> bit(a.size());
  
  long long ret = 0;
  for (int i = 0; i < n; i++) {
    if (t[i] == 0) {
      int j = lower_bound(a.begin(), a.end(), x[i]) - a.begin();
      bit.add(j+1, y[i]);
    } else {
      int j = lower_bound(a.begin(), a.end(), x[i]) - a.begin();
      int k = lower_bound(a.begin(), a.end(), y[i]) - a.begin();
      ret += bit.sum(j, k+1);
    }
  }

  cout << ret << endl;
  
  return 0;
}
0