結果

問題 No.1332 Range Nearest Query
ユーザー ks2mks2m
提出日時 2021-01-08 23:12:18
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 1,699 bytes
コンパイル時間 4,870 ms
コンパイル使用メモリ 78,452 KB
実行使用メモリ 99,268 KB
最終ジャッジ日時 2024-04-28 05:06:23
合計ジャッジ時間 8,452 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
42,404 KB
testcase_01 AC 53 ms
37,212 KB
testcase_02 AC 52 ms
37,032 KB
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
権限があれば一括ダウンロードができます

ソースコード

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[j]);
			}
			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