結果

問題 No.743 Segments on a Polygon
ユーザー tnakao0123tnakao0123
提出日時 2018-10-11 13:15:38
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 35 ms / 2,000 ms
コード長 1,577 bytes
コンパイル時間 1,040 ms
コンパイル使用メモリ 86,572 KB
実行使用メモリ 5,152 KB
最終ジャッジ日時 2023-08-09 06:30:11
合計ジャッジ時間 2,315 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
4,960 KB
testcase_01 AC 35 ms
5,044 KB
testcase_02 AC 33 ms
4,964 KB
testcase_03 AC 34 ms
5,100 KB
testcase_04 AC 34 ms
5,152 KB
testcase_05 AC 34 ms
5,100 KB
testcase_06 AC 34 ms
5,104 KB
testcase_07 AC 34 ms
5,148 KB
testcase_08 AC 34 ms
5,100 KB
testcase_09 AC 24 ms
5,112 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 743.cc:  No.743 Segments on a Polygon - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 100000;
const int MAX_M = 200000;

/* typedef */

typedef long long ll;
typedef pair<int,int> pii;

template <typename T, const int MAX_N>
struct BIT {
  int n;
  T bits[MAX_N + 1];
  BIT() {}

  void init(int _n) {
    n = _n;
    memset(bits, 0, sizeof(bits));
  }

  T sum(int x) {
    T s = 0;
    while (x > 0) {
      s += bits[x];
      x -= (x & -x);
    }
    return s;
  }

  void add(int x, T v) {
    while (x <= n) {
      bits[x] += v;
      x += (x & -x);
    }
  }
};

/* global variables */

BIT<int,MAX_M+1> bit;
pii rs[MAX_N];

/* subroutines */

/* main */

int main() {
  int n, m;
  scanf("%d%d", &n, &m);

  bit.init(m + 1);

  for (int i = 0; i < n; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    if (a > b) swap(a, b);
    rs[i] = pii(a, b);
  }
  sort(rs, rs + n);
  
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    int a = rs[i].first, b = rs[i].second;
    a++, b++;

    int d = bit.sum(b) - bit.sum(a);
    sum += d;

    //for (int i = 1; i <= m; i++) printf("%d ", bit.sum(i));
    //printf("%d-%d: d=%d\n", a, b, d);
    
    bit.add(b, 1);
  }

  printf("%lld\n", sum);
  return 0;
}
0