結果

問題 No.1332 Range Nearest Query
ユーザー ks2mks2m
提出日時 2021-01-08 23:06:52
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,699 bytes
コンパイル時間 2,447 ms
コンパイル使用メモリ 78,924 KB
実行使用メモリ 104,672 KB
最終ジャッジ日時 2024-04-28 04:58:24
合計ジャッジ時間 76,704 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 53 ms
36,764 KB
testcase_01 AC 54 ms
37,000 KB
testcase_02 AC 55 ms
37,040 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 1,683 ms
82,360 KB
testcase_27 TLE -
testcase_28 AC 408 ms
45,860 KB
testcase_29 AC 391 ms
45,788 KB
testcase_30 WA -
testcase_31 AC 378 ms
45,548 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 AC 420 ms
45,432 KB
testcase_35 AC 420 ms
45,312 KB
testcase_36 AC 395 ms
45,692 KB
testcase_37 AC 384 ms
45,660 KB
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		String[] sa = br.readLine().split(" ");
		int[] x = new int[n];
		for (int i = 0; i < n; i++) {
			x[i] = Integer.parseInt(sa[i]);
		}

		int rn = (int) Math.sqrt(n) + 1;
		List<TreeSet<Integer>> list = new ArrayList<>(rn);
		for (int i = 0; i < rn; i++) {
			int start = rn * i;
			int end = Math.min(n, start + rn);
			TreeSet<Integer> set = new TreeSet<>();
			set.add(-1000000000);
			set.add(2000000000);
			for (int j = start; j < end; j++) {
				set.add(x[i]);
			}
			list.add(set);
		}

		int q = Integer.parseInt(br.readLine());
		PrintWriter pw = new PrintWriter(System.out);
		for (int i = 0; i < q; i++) {
			sa = br.readLine().split(" ");
			int l = Integer.parseInt(sa[0]) - 1;
			int r = Integer.parseInt(sa[1]);
			int y = Integer.parseInt(sa[2]);
			int ll = (l - 1) / rn + 1;
			int rr = r / rn;
			int ans = Integer.MAX_VALUE;
			int end = Math.min(ll * rn, r);
			for (int j = l; j < end; j++) {
				ans = Math.min(ans, Math.abs(x[j] - y));
			}
			for (int j = ll; j < rr; j++) {
				TreeSet<Integer> set = list.get(j);
				int ol = set.floor(y);
				ans = Math.min(ans, y - ol);
				int or = set.ceiling(y);
				ans = Math.min(ans, or - y);
			}
			for (int j = Math.max(rr * rn, l); j < r; j++) {
				ans = Math.min(ans, Math.abs(x[j] - y));
			}
			pw.println(ans);
		}
		pw.flush();
		br.close();
	}
}
0