結果

問題 No.743 Segments on a Polygon
ユーザー 37zigen37zigen
提出日時 2017-12-27 03:47:06
言語 Java21
(openjdk 21)
結果
AC  
実行時間 899 ms / 2,000 ms
コード長 1,396 bytes
コンパイル時間 3,138 ms
コンパイル使用メモリ 75,620 KB
実行使用メモリ 67,596 KB
最終ジャッジ日時 2023-08-09 02:32:45
合計ジャッジ時間 13,350 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 865 ms
66,956 KB
testcase_01 AC 873 ms
67,420 KB
testcase_02 AC 892 ms
67,220 KB
testcase_03 AC 862 ms
67,064 KB
testcase_04 AC 899 ms
67,048 KB
testcase_05 AC 877 ms
66,940 KB
testcase_06 AC 880 ms
67,596 KB
testcase_07 AC 878 ms
67,080 KB
testcase_08 AC 868 ms
67,204 KB
testcase_09 AC 694 ms
66,628 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

class Main {

	class BIT {
		int n;
		long[] v;

		public BIT(int n_) {
			n = n_;
			v = new long[n + 1];
		}

		void add(int k, int val) {
			while (k <= n) {
				v[k] += val;
				k += k & -k;
			}
		}

		long sum(int k) {
			long ret = 0;
			while (k >= 1) {
				ret += v[k];
				k -= k & -k;
			}
			return ret;
		}

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

	void run() {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int M = sc.nextInt();
		int[][] a = new int[N][2];
		for (int i = 0; i < N; ++i) {
			a[i][0] = sc.nextInt();
			a[i][1] = sc.nextInt();
			if (a[i][0] > a[i][1]) {
				a[i][0] ^= a[i][1];
				a[i][1] ^= a[i][0];
				a[i][0] ^= a[i][1];
			}
		}
		Arrays.sort(a, new Comparator<int[]>() {
			@Override
			public int compare(int[] arg0, int[] arg1) {
				return Integer.compare(arg0[0], arg1[0]);
			}
		});

		BIT bit = new BIT(M);
		long ans = 0;
		for (int i = 0; i < N; ++i) {
			ans += bit.sum(a[i][0], a[i][1]);
			bit.add(a[i][1], 1);
		}
		System.out.println(ans);
	}

	void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}

	public static void main(String[] args) throws FileNotFoundException {
		new Main().run();
	}
}
0