結果

問題 No.1332 Range Nearest Query
ユーザー Example0911Example0911
提出日時 2021-02-03 20:54:37
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 442 ms / 2,500 ms
コード長 1,828 bytes
コンパイル時間 5,096 ms
コンパイル使用メモリ 209,408 KB
実行使用メモリ 30,588 KB
最終ジャッジ日時 2023-09-12 16:39:18
合計ジャッジ時間 26,469 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 376 ms
27,528 KB
testcase_04 AC 394 ms
27,324 KB
testcase_05 AC 389 ms
27,256 KB
testcase_06 AC 324 ms
30,308 KB
testcase_07 AC 331 ms
30,440 KB
testcase_08 AC 334 ms
30,404 KB
testcase_09 AC 336 ms
30,272 KB
testcase_10 AC 338 ms
30,412 KB
testcase_11 AC 327 ms
30,388 KB
testcase_12 AC 331 ms
30,452 KB
testcase_13 AC 331 ms
30,588 KB
testcase_14 AC 354 ms
30,536 KB
testcase_15 AC 336 ms
30,356 KB
testcase_16 AC 414 ms
30,408 KB
testcase_17 AC 419 ms
30,452 KB
testcase_18 AC 417 ms
30,264 KB
testcase_19 AC 419 ms
30,408 KB
testcase_20 AC 414 ms
30,356 KB
testcase_21 AC 421 ms
30,260 KB
testcase_22 AC 415 ms
30,404 KB
testcase_23 AC 403 ms
30,504 KB
testcase_24 AC 416 ms
30,504 KB
testcase_25 AC 442 ms
30,408 KB
testcase_26 AC 301 ms
30,296 KB
testcase_27 AC 269 ms
30,256 KB
testcase_28 AC 165 ms
7,032 KB
testcase_29 AC 166 ms
6,884 KB
testcase_30 AC 170 ms
6,892 KB
testcase_31 AC 159 ms
6,860 KB
testcase_32 AC 172 ms
6,896 KB
testcase_33 AC 174 ms
6,888 KB
testcase_34 AC 160 ms
6,956 KB
testcase_35 AC 162 ms
6,888 KB
testcase_36 AC 165 ms
7,088 KB
testcase_37 AC 166 ms
6,880 KB
testcase_38 AC 318 ms
18,900 KB
testcase_39 AC 231 ms
7,876 KB
testcase_40 AC 400 ms
29,500 KB
testcase_41 AC 263 ms
11,428 KB
testcase_42 AC 310 ms
18,224 KB
testcase_43 AC 288 ms
14,244 KB
testcase_44 AC 360 ms
24,712 KB
testcase_45 AC 349 ms
22,652 KB
testcase_46 AC 302 ms
17,120 KB
testcase_47 AC 327 ms
20,644 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;

using ll = long long;
using P = pair<ll, ll>;
const ll INF = (1LL << 61);
ll mod = 998244353;

template <typename T>
struct RMQ {
	int n;         // 葉の数
	vector<T> dat; // 完全二分木の配列
	RMQ(int n_) : n(), dat(n_ * 4, INF) { // 葉の数は 2^x の形
		int x = 1;
		while (n_ > x) {
			x *= 2;
		}
		n = x;
	}
	void update(int i, T x) {
		i += n - 1;
		dat[i] = x;
		while (i > 0) {
			i = (i - 1) / 2;  // parent
			dat[i] = min(dat[i * 2 + 1], dat[i * 2 + 2]);
		}
	}
	// the minimum element of [a,b)
	T query(int a, int b) { return query_sub(a, b, 0, 0, n); }
	T query_sub(int a, int b, int k, int l, int r) {
		if (r <= a || b <= l) {
			return INF;
		}
		else if (a <= l && r <= b) {
			return dat[k];
		}
		else {
			T vl = query_sub(a, b, k * 2 + 1, l, (l + r) / 2);
			T vr = query_sub(a, b, k * 2 + 2, (l + r) / 2, r);
			return min(vl, vr);
		}
	}
}; 
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	ll N; cin >> N;
	vector<P>p;
	for (int i = 0; i < N; i++) {
		ll X; cin >> X;
		p.push_back({ X, -i });
	}
	ll Q; cin >> Q;
	vector<ll>l(Q), r(Q);
	for (int i = 0; i < Q; i++) {
		cin >> l[i] >> r[i]; l[i]--;
		ll x; cin >> x;
		p.push_back({ x, i + 1 });
	}
	sort(p.begin(), p.end());
	vector<ll>ans(Q, INF);
	// まず, 集合Lについて試す
	RMQ<ll>rmq(N), rmq2(N);
	for (auto x : p) {
		if (x.second <= 0)rmq.update(-x.second, -x.first);
		else {
			ll idx = x.second - 1;
			ans[idx] = min(ans[idx], x.first + rmq.query(l[idx], r[idx]));
		}
	}
	
	reverse(p.begin(), p.end());
	for (auto x : p) {
		if (x.second <= 0)rmq2.update(-x.second, x.first);
		else {
			ll idx = x.second - 1;
			ans[idx] = min(ans[idx], rmq2.query(l[idx], r[idx]) - x.first);
		}
	}

	for (int i = 0; i < Q; i++) {
		cout << ans[i] << endl;
	}
	return 0;
}
0