結果

問題 No.743 Segments on a Polygon
ユーザー 37zigen37zigen
提出日時 2018-10-06 00:17:11
言語 Java21
(openjdk 21)
結果
AC  
実行時間 967 ms / 2,000 ms
コード長 1,559 bytes
コンパイル時間 3,195 ms
コンパイル使用メモリ 76,256 KB
実行使用メモリ 67,164 KB
最終ジャッジ日時 2023-08-09 02:44:26
合計ジャッジ時間 14,111 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 939 ms
52,768 KB
testcase_01 AC 957 ms
53,052 KB
testcase_02 AC 928 ms
53,208 KB
testcase_03 AC 949 ms
52,588 KB
testcase_04 AC 914 ms
53,136 KB
testcase_05 AC 903 ms
53,016 KB
testcase_06 AC 967 ms
53,832 KB
testcase_07 AC 902 ms
66,916 KB
testcase_08 AC 896 ms
67,164 KB
testcase_09 AC 722 ms
66,912 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();
		if(M<=2)throw new AssertionError();
		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