結果

問題 No.789 範囲の合計
ユーザー kk
提出日時 2021-03-12 01:39:22
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,425 bytes
コンパイル時間 1,892 ms
コンパイル使用メモリ 202,276 KB
最終ジャッジ日時 2025-01-19 13:43:09
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 5 WA * 10
権限があれば一括ダウンロードができます

ソースコード

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