結果

問題 No.789 範囲の合計
ユーザー playrollerplayroller
提出日時 2019-05-07 10:13:18
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 90 ms / 1,000 ms
コード長 2,079 bytes
コンパイル時間 1,789 ms
コンパイル使用メモリ 177,176 KB
実行使用メモリ 9,156 KB
最終ジャッジ日時 2023-09-14 16:23:42
合計ジャッジ時間 3,464 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 82 ms
9,156 KB
testcase_03 AC 44 ms
5,224 KB
testcase_04 AC 75 ms
8,880 KB
testcase_05 AC 63 ms
9,104 KB
testcase_06 AC 67 ms
9,080 KB
testcase_07 AC 35 ms
5,216 KB
testcase_08 AC 52 ms
6,804 KB
testcase_09 AC 47 ms
6,240 KB
testcase_10 AC 90 ms
9,112 KB
testcase_11 AC 69 ms
8,816 KB
testcase_12 AC 69 ms
8,820 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define rep(i,j,n) for(int i=j;i<n;++i)
#define all(i) i.begin(),i.end()
#define rall(i) i.rbegin(), i.rend()
#define INF 1e9
#define LINF 1e18
const int mod = 1e9 + 7;

typedef long long i64;
typedef pair<int, int> pi;

template <class T> using vt = vector<T>;
template <class T> using vvt = vector<vector<T>>;

i64 gcd(i64 n, i64 m) {return (m == 0? n : gcd(m, n % m));}
i64 lcd(i64 n, i64 m) {return (n / gcd(n, m) * m);}
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

template <class Monoid> struct SegmentTree {
  using F = function<Monoid(Monoid, Monoid)>;

  int sz;
  vector<Monoid> seg;

  const F f;
  const Monoid M1;

  SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) {
    sz = 1;
    while(sz < n) sz <<= 1;
    seg.assign(2 * sz, M1);
  }

  void set(int k, const Monoid &x) {
    seg[k + sz] = x;
  }

  void build() {
    for(int k = sz - 1; k > 0; --k) {
      seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
    }
  }

  void update(int k, const Monoid &x) {
    k += sz;
    seg[k] += x;
    while(k >>= 1){
      seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
    }
  }

  Monoid query(int a, int b) {
    Monoid L = M1, R = M1;
    for(a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
      if(a & 1) L = f(L, seg[a++]);
      if(b & 1) R = f(seg[--b], R);
    }
    return f(L, R);
  }
};

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

  int n;
  cin >> n;
  vt<int> use, a(n), b(n), c(n);
  rep(i, 0, n) {
    cin >> a[i] >> b[i] >> c[i];
    use.push_back(b[i]);
    if(a[i] == 1) use.push_back(c[i]);
  }
  sort(all(use));
  use.erase(unique(all(use)), use.end());

  i64 ans = 0;
  SegmentTree<i64> seg(use.size(), [](i64 a, i64 b){return a + b;}, 0);
  rep(i, 0, n) {
    if(a[i] == 0) {
      int x = lower_bound(all(use), b[i]) - use.begin();
      seg.update(x, c[i]);
    }
    else {
      int left = lower_bound(all(use), b[i]) - use.begin();
      int right = upper_bound(all(use), c[i]) - use.begin();
      ans += seg.query(left, right);
    }
  }
  cout << ans << endl;
}
0