結果

問題 No.743 Segments on a Polygon
ユーザー tnakao0123tnakao0123
提出日時 2018-10-11 13:13:10
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,555 bytes
コンパイル時間 614 ms
コンパイル使用メモリ 86,904 KB
実行使用メモリ 5,348 KB
最終ジャッジ日時 2023-08-02 19:33:26
合計ジャッジ時間 2,083 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 24 ms
5,132 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++)
    scanf("%d%d", &rs[i].first, &rs[i].second);
  sort(rs, rs + n);
  
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    int a = rs[i].first, b = rs[i].second;
    if (a > b) swap(a, b);
    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