結果

問題 No.743 Segments on a Polygon
ユーザー 37zigen37zigen
提出日時 2018-10-05 21:38:28
言語 Java21
(openjdk 21)
結果
AC  
実行時間 916 ms / 2,000 ms
コード長 1,521 bytes
コンパイル時間 3,325 ms
コンパイル使用メモリ 76,324 KB
実行使用メモリ 67,432 KB
最終ジャッジ日時 2023-08-09 02:34:30
合計ジャッジ時間 13,859 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 864 ms
67,432 KB
testcase_01 AC 877 ms
67,148 KB
testcase_02 AC 887 ms
67,024 KB
testcase_03 AC 886 ms
66,928 KB
testcase_04 AC 899 ms
67,424 KB
testcase_05 AC 859 ms
66,716 KB
testcase_06 AC 916 ms
67,008 KB
testcase_07 AC 843 ms
67,384 KB
testcase_08 AC 875 ms
67,080 KB
testcase_09 AC 696 ms
66,992 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//a[i]=b[i]なる入力が含まれていないことを確認



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])
				throw new AssertionError();
			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