結果

問題 No.789 範囲の合計
ユーザー 👑 emthrmemthrm
提出日時 2019-03-11 01:56:11
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 70 ms / 1,000 ms
コード長 2,175 bytes
コンパイル時間 1,273 ms
コンパイル使用メモリ 129,480 KB
実行使用メモリ 6,228 KB
最終ジャッジ日時 2023-09-05 19:59:24
合計ジャッジ時間 2,763 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 64 ms
6,148 KB
testcase_03 AC 38 ms
5,224 KB
testcase_04 AC 60 ms
6,088 KB
testcase_05 AC 52 ms
6,048 KB
testcase_06 AC 55 ms
6,132 KB
testcase_07 AC 32 ms
5,224 KB
testcase_08 AC 44 ms
5,512 KB
testcase_09 AC 41 ms
5,180 KB
testcase_10 AC 70 ms
6,228 KB
testcase_11 AC 56 ms
5,992 KB
testcase_12 AC 56 ms
6,108 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cassert>
#include <cctype>
#include <chrono>
#define _USE_MATH_DEFINES
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()

const int INF = 0x3f3f3f3f, MOD = 1000000007;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
/*-----------------------------------------*/
template <typename Abelian, const Abelian UNITY>
struct BIT {
  BIT(int sz_) : sz(sz_), dat(sz_, 0) {}

  void add(int idx, Abelian value) {
    while (idx < sz) {
      dat[idx] += value;
      idx |= idx + 1;
    }
  }

  Abelian sum(int idx) {
    Abelian res = 0;
    while (idx >= 0) {
      res += dat[idx];
      idx = (idx & (idx + 1)) - 1;
    }
    return res;
  }

  int lower_bound(Abelian value) {
    if (value <= 0) return 0;
    int res = 0, exponent = 1;
    while (exponent <= sz) exponent <<= 1;
    for (int mask = exponent >> 1; mask > 0; mask >>= 1) {
      if (res + mask - 1 < sz && dat[res + mask - 1] < value) {
        value -= dat[res + mask - 1];
        res += mask;
      }
    }
    return res;
  }

private:
  int sz;
  vector<Abelian> dat;
};

int main() {
  cin.tie(0); ios::sync_with_stdio(false);
  // freopen("input.txt", "r", stdin);

  int n; cin >> n;
  vector<int> q(n), l(n), r(n), x;
  REP(i, n) {
    cin >> q[i] >> l[i] >> r[i];
    x.emplace_back(l[i]);
    if (q[i] == 1) x.emplace_back(r[i]);
  }
  sort(ALL(x));
  x.erase(unique(ALL(x)), x.end());
  BIT<long long, 0> bit(x.size());
  long long ans = 0;
  REP(i, n) {
    l[i] = lower_bound(ALL(x), l[i]) - x.begin();
    if (q[i] == 0) {
      bit.add(l[i], r[i]);
    } else {
      r[i] = lower_bound(ALL(x), r[i]) - x.begin();
      ans += bit.sum(r[i]) - (l[i] > 0 ? bit.sum(l[i] - 1) : 0);
    }
  }
  cout << ans << '\n';
  return 0;
}
0