結果

問題 No.743 Segments on a Polygon
コンテスト
ユーザー bal4u
提出日時 2019-05-15 07:45:16
言語 C(gnu17)
(gcc 15.2.0)
コンパイル:
gcc-15 -O2 -std=gnu17 -Wno-error=implicit-function-declaration -Wno-error=implicit-int -DONLINE_JUDGE -o a.out _filename_ -lm
実行:
./a.out
結果
AC  
実行時間 13 ms / 2,000 ms
コード長 832 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 182 ms
コンパイル使用メモリ 38,884 KB
最終ジャッジ日時 2026-02-22 03:24:06
ジャッジサーバーID
(参考情報)
judge3 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 10
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

// yukicoder: No.743 Segments on a Polygon
// 2019.5.15 bal4u

#include <stdio.h>
#include <stdlib.h>

#if 1
#define gc() getchar_unlocked()
#else
#define gc() getchar()
#endif
int in()
{
	int n = 0, c = gc();
	do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

// Bit木 library
#define MAX 200005
int bit[MAX]; int n;
long long sum(int i) {
	long long s = 0;
	while (i > 0) s += bit[i], i -= i & -i;
	return s;
}

void add(int i) {
	while (i <= n) bit[i]++, i += i & -i;
}

int f[MAX];

int main()
{
	int i, N, M, a, b;
	long long ans;
	
	N = in(), M = in();
	for (i = 0; i < N; i++) {
		a = in(), b = in();
		if (a < b) f[a] = b;
		else       f[b] = a;
	}
	
	// bit木で集計
	n = M, ans = 0;
	for (i = 0; i < M; i++) if (f[i]) {
		ans += sum(f[i]) - sum(i);
		add(f[i]);
	}
	printf("%lld\n", ans);
	return 0;
}
0