結果

問題 No.743 Segments on a Polygon
ユーザー risujirohrisujiroh
提出日時 2018-10-05 22:01:37
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 30 ms / 2,000 ms
コード長 1,373 bytes
コンパイル時間 1,829 ms
コンパイル使用メモリ 166,944 KB
実行使用メモリ 5,632 KB
最終ジャッジ日時 2023-08-09 02:35:29
合計ジャッジ時間 2,948 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
5,376 KB
testcase_01 AC 28 ms
5,400 KB
testcase_02 AC 29 ms
5,424 KB
testcase_03 AC 29 ms
5,632 KB
testcase_04 AC 30 ms
5,428 KB
testcase_05 AC 29 ms
5,376 KB
testcase_06 AC 29 ms
5,632 KB
testcase_07 AC 29 ms
5,368 KB
testcase_08 AC 29 ms
5,380 KB
testcase_09 AC 24 ms
5,476 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
template<class T = int> using V = vector<T>;
template<class T = int> using VV = V< V<T> >;
template<class T> void assign(V<T>& v, int n, const T& a = T()) { v.assign(n, a); }
template<class T, class... U> void assign(V<T>& v, int n, const U&... u) { v.resize(n); for (auto&& i : v) assign(i, u...); }

template<class T> struct BIT {
  int n, h;
  V<T> t;

  BIT(int n) : n(n), h(__lg(n) + 1) { t.assign(n + 1, (T) 0); }

  void add(int x, T a) {
    for (int i = x + 1; i < n + 1; i += i & -i) t[i] += a;
  }

  int lower_bound(T s) {
    if (s <= 0) return 0;
    int res = 0;
    for (int k = 1 << h - 1; k; k >>= 1) if (res + k < n + 1 and t[res + k] < s) s -= t[res += k];
    return res + 1;
  }

  T sum(int x) {
    T res = 0;
    for (int i = x; i; i -= i & -i) res += t[i];
    return res;
  }

  T sum(int l, int r) {
    return sum(r) - sum(l);
  }
};

int main() {
  cin.tie(NULL); ios::sync_with_stdio(false);
  int n, m; cin >> n >> m;
  V<> p(m, -1), q(m);
  for (int i = 0; i < n; i++) {
    int a, b; cin >> a >> b;
    if (a > b) swap(a, b);
    p[b] = i;
    q[b] = a;
  }
  lint res = 0;
  BIT<int> bit(m);
  for (int i = 0; i < m; i++) {
    if (p[i] == -1) bit.add(i, 1);
    else {
      res += bit.sum(q[i] + 1, i);
      bit.add(q[i], -1);
    }
  }
  cout << res << '\n';
}
0