結果

問題 No.789 範囲の合計
ユーザー iqueue02iqueue02
提出日時 2020-01-29 15:52:24
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 125 ms / 1,000 ms
コード長 1,664 bytes
コンパイル時間 1,711 ms
コンパイル使用メモリ 175,224 KB
実行使用メモリ 5,448 KB
最終ジャッジ日時 2023-10-14 05:46:31
合計ジャッジ時間 4,695 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 120 ms
5,188 KB
testcase_03 AC 71 ms
4,732 KB
testcase_04 AC 116 ms
5,244 KB
testcase_05 AC 104 ms
5,112 KB
testcase_06 AC 105 ms
5,260 KB
testcase_07 AC 62 ms
4,832 KB
testcase_08 AC 94 ms
5,448 KB
testcase_09 AC 90 ms
5,204 KB
testcase_10 AC 125 ms
4,844 KB
testcase_11 AC 95 ms
5,160 KB
testcase_12 AC 92 ms
5,100 KB
testcase_13 AC 2 ms
4,352 KB
testcase_14 AC 1 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n);i++)
#define sz(x) int(x.size())
typedef long long ll;
typedef pair<int,int> P;

template <typename T> 
struct SegmentTree {
  
  int n;
  vector<T> node;

  T f(T a, T b) {return (a + b);}

  SegmentTree (int sz, T init = 0) {
    n = 1; while (n < sz) n <<= 1;
    node.assign(2*n, init);
  }

  void update(int k, T x) {
    k += (n - 1);
    node[k] += x;
    while (k > 0) {
      k = (k - 1) >> 1;
      node[k] = f(node[2*k+1], node[2*k+2]);
    } 
  }

  T query(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0) r = n;
    if (r <= a || b <= l) return 0;
    if (a <= l && r <= b) return node[k];
    T vl = query(a, b, 2*k+1, l, (l+r)/2);
    T vr = query(a, b, 2*k+2, (l+r)/2, r);
    return f(vl, vr);
  }

  T getvalue(int k) {
    return query(k,k+1);
  }

};

int main(){
  int n;
  cin >> n;
  vector<int> t;
  vector<pair<int,P>> q(n);
  for (int i = 0; i < n; i++) {
    cin >> q[i].first >> q[i].second.first >> q[i].second.second;
    if (!q[i].first) t.emplace_back(q[i].second.first);
  }
  sort(t.begin(), t.end());
  t.erase(unique(t.begin(), t.end()), t.end());
  int m = sz(t);
  SegmentTree<int> seg(m);
  ll res = 0;
  for (int i = 0; i < n; i++) {
    int f = q[i].first, a = q[i].second.first, b = q[i].second.second;
    if (f) {
      auto l = lower_bound(t.begin(), t.end(), a) - t.begin();
      auto r = upper_bound(t.begin(), t.end(), b) - t.begin();
      res += seg.query(l, r);
    } else {
      auto l = lower_bound(t.begin(), t.end(), a) - t.begin();
      seg.update(l, b);
    }
  }
  cout << res << endl;
  return 0;
}
0