結果

問題 No.743 Segments on a Polygon
ユーザー poyompypoyompy
提出日時 2018-10-05 21:57:53
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 472 ms / 2,000 ms
コード長 1,369 bytes
コンパイル時間 528 ms
コンパイル使用メモリ 65,380 KB
実行使用メモリ 41,600 KB
最終ジャッジ日時 2024-04-26 18:05:19
合計ジャッジ時間 5,950 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 440 ms
41,344 KB
testcase_01 AC 455 ms
41,472 KB
testcase_02 AC 444 ms
41,472 KB
testcase_03 AC 460 ms
41,472 KB
testcase_04 AC 472 ms
41,344 KB
testcase_05 AC 455 ms
41,472 KB
testcase_06 AC 453 ms
41,344 KB
testcase_07 AC 465 ms
41,472 KB
testcase_08 AC 452 ms
41,600 KB
testcase_09 AC 133 ms
41,472 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:47:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   47 |   scanf("%d %d", &n, &m);
      |   ~~~~~^~~~~~~~~~~~~~~~~
main.cpp:51:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   51 |     scanf("%d %d", &a[i], &b[i]);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

const int N = 2 << 17;

vector<int> seg[N * 2];

int lt(vector<int> &a, int x) {
  return lower_bound(a.begin(), a.end(), x) - a.begin();
}

int gt(vector<int> &a, int x) {
  return a.end() - upper_bound(a.begin(), a.end(), x);
}

int lt(int l, int r, int x) {
  l += N;
  r += N;
  int res = 0;
  while (l < r) {
    if (l & 1) res += lt(seg[l++], x);
    if (r & 1) res += lt(seg[--r], x);
    l >>= 1;
    r >>= 1;
  }
  return res;
}

int gt(int l, int r, int x) {
  l += N;
  r += N;
  int res = 0;
  while (l < r) {
    if (l & 1) res += gt(seg[l++], x);
    if (r & 1) res += gt(seg[--r], x);
    l >>= 1;
    r >>= 1;
  }
  return res;
}

int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  long long ans = 0;
  vector<int> a(m), b(m);
  for (int i = 0; i < n; i++) {
    scanf("%d %d", &a[i], &b[i]);
    if (a[i] > b[i]) {
      swap(a[i], b[i]);
    }
    seg[a[i] + N].push_back(b[i]);
    seg[b[i] + N].push_back(a[i]);
  }
  for (int i = N - 1; i >= 1; i--) {
    auto &x = seg[i * 2];
    auto &y = seg[i * 2 + 1];
    seg[i].resize(x.size() + y.size());
    merge(x.begin(), x.end(), y.begin(), y.end(), seg[i].begin());
  }
  for (int i = 0; i < m; i++) {
    ans += lt(a[i], b[i], a[i]);
    ans += gt(a[i], b[i], b[i]);
  }
  printf("%lld\n", ans / 2);
  return 0;
}
0