結果

問題 No.789 範囲の合計
ユーザー 👑 emthrmemthrm
提出日時 2019-03-11 01:43:02
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,412 bytes
コンパイル時間 1,345 ms
コンパイル使用メモリ 129,948 KB
実行使用メモリ 10,696 KB
最終ジャッジ日時 2023-09-05 19:59:05
合計ジャッジ時間 4,062 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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 T>
vector<T> compress(vector<T> &x) {
  int sz = x.size();
  vector<T> xs;
  REP(i, sz) xs.emplace_back(x[i]);
  sort(ALL(xs));
  xs.erase(unique(ALL(xs)), xs.end());
  return xs;
}

template <typename Abelian, const Abelian UNITY>
struct BIT {
  BIT(int sz_) {
    sz = sz_ + 1;
    dat.assign(sz, 0);
  }

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

  Abelian sum(int idx) {
    Abelian res = 0;
    while (idx > 0) {
      res += dat[idx];
      idx -= idx & -idx;
    }
    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 < sz && dat[res + mask] < value) {
        value -= dat[res + mask];
        res += mask;
      }
    }
    return res + 1;
  }

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), coordinates;
  REP(i, n) {
    cin >> q[i] >> l[i] >> r[i];
    coordinates.emplace_back(l[i]);
    if (q[i] == 1) coordinates.emplace_back(r[i]);
  }
  vector<int> x = compress(coordinates);
  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