結果

問題 No.1332 Range Nearest Query
ユーザー 👑 jupirojupiro
提出日時 2021-01-23 15:53:03
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 277 ms / 2,500 ms
コード長 4,034 bytes
コンパイル時間 1,632 ms
コンパイル使用メモリ 152,404 KB
実行使用メモリ 34,304 KB
最終ジャッジ日時 2023-08-29 07:18:35
合計ジャッジ時間 18,772 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 244 ms
20,624 KB
testcase_04 AC 234 ms
20,848 KB
testcase_05 AC 229 ms
21,120 KB
testcase_06 AC 139 ms
33,424 KB
testcase_07 AC 141 ms
32,768 KB
testcase_08 AC 142 ms
34,192 KB
testcase_09 AC 144 ms
34,304 KB
testcase_10 AC 141 ms
33,684 KB
testcase_11 AC 142 ms
33,712 KB
testcase_12 AC 140 ms
32,780 KB
testcase_13 AC 140 ms
32,360 KB
testcase_14 AC 141 ms
32,872 KB
testcase_15 AC 138 ms
33,212 KB
testcase_16 AC 259 ms
33,316 KB
testcase_17 AC 265 ms
32,512 KB
testcase_18 AC 274 ms
32,876 KB
testcase_19 AC 271 ms
34,044 KB
testcase_20 AC 277 ms
32,740 KB
testcase_21 AC 275 ms
32,560 KB
testcase_22 AC 269 ms
32,856 KB
testcase_23 AC 272 ms
33,328 KB
testcase_24 AC 269 ms
32,904 KB
testcase_25 AC 267 ms
33,956 KB
testcase_26 AC 160 ms
32,524 KB
testcase_27 AC 108 ms
32,620 KB
testcase_28 AC 42 ms
6,152 KB
testcase_29 AC 44 ms
6,208 KB
testcase_30 AC 44 ms
6,168 KB
testcase_31 AC 41 ms
6,224 KB
testcase_32 AC 45 ms
6,304 KB
testcase_33 AC 45 ms
6,236 KB
testcase_34 AC 42 ms
6,340 KB
testcase_35 AC 43 ms
6,152 KB
testcase_36 AC 43 ms
6,152 KB
testcase_37 AC 44 ms
6,128 KB
testcase_38 AC 153 ms
18,812 KB
testcase_39 AC 73 ms
7,100 KB
testcase_40 AC 263 ms
32,432 KB
testcase_41 AC 99 ms
9,652 KB
testcase_42 AC 153 ms
20,572 KB
testcase_43 AC 120 ms
12,472 KB
testcase_44 AC 219 ms
20,000 KB
testcase_45 AC 200 ms
19,948 KB
testcase_46 AC 146 ms
13,416 KB
testcase_47 AC 180 ms
19,688 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>
#include <numeric>
#include <cctype>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <utility>
#include <fstream>

using namespace std;
typedef long long ll;
using ull = unsigned long long;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
//template<typename T> T gcd(T a, T b) { a = abs(a), b = abs(b); while (b > 0) { tie(a, b) = make_pair(b, a % b); }return a; }
//mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

constexpr long long INF = 1LL << 60;
constexpr int inf = 1000000007;
constexpr long long mod = 1000000007LL;
//constexpr long long  mod = 998244353;


template<typename Monoid, typename F>
struct SegmentTree {
	int sz;
	vector<Monoid> seg;
	const F f;
	const Monoid IE;

	SegmentTree(int n, const F f, const Monoid& IE) :f(f), IE(IE) {
		sz = 1;
		while (sz < n) sz *= 2;
		seg.assign(2 * sz, IE);
	}

	void set(int k, const Monoid& x) {
		seg[k + sz] = x;
	}

	void build() {
		for (int k = sz - 1; k > 0; k--) {
			seg[k] = f(seg[k * 2], seg[k * 2 + 1]);
		}
	}

	void update(int k, const Monoid& x) {
		k += sz;
		seg[k] = x;
		while (k /= 2) {
			seg[k] = f(seg[k * 2], seg[k * 2 + 1]);
		}
	}

	Monoid query(int a, int b) {
		if (a >= b) return IE;
		Monoid L = IE, R = IE;
		for (a += sz, b += sz; a < b; a /= 2, b /= 2) {
			if (a & 1) L = f(L, seg[a++]);
			if (b & 1) R = f(seg[--b], R);
		}
		return f(L, R);
	}

	template<typename C>
	int find_subtree(int idx, const C& check, Monoid& M, bool type) {
		while (idx < sz) {
			Monoid nxt = type ? f(seg[2 * idx + 1], M) : f(M, seg[2 * idx]);
			if (check(nxt)) idx = idx * 2 + type;
			else M = nxt, idx = idx * 2 + 1 - type;
		}
		return idx - sz;
	}

	template<typename C>
	int min_left(int a, const C& check) {
		Monoid L = IE;
		if (a <= 0) {
			if (check(f(L, seg[1]))) return find_subtree(1, check, L, false);
			return -1;
		}
		int b = sz;
		for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
			if (a & 1) {
				Monoid nxt = f(L, seg[a]);
				if (check(nxt)) return find_subtree(a, check, L, false);
				L = nxt;
				a += 1;
			}
		}
		return -1;
	}

	template<typename C>
	int max_right(int b, const C& check) {
		Monoid R = IE;
		if (b >= sz) {
			if (check(f(seg[1], R))) return find_subtree(1, check, R, true);
			return -1;
		}
		int a = 0;
		for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
			if (b & 1) {
				b -= 1;
				Monoid nxt = f(seg[b], R);
				if (check(nxt)) return find_subtree(b, check, R, true);
				R = nxt;
			}
		}
		return -1;
	}
};

int main()
{

	std::cin.tie(nullptr);
	std::ios::sync_with_stdio(false);

	int n; cin >> n;
	vector<ll> X(n); for (auto& e : X) cin >> e;
	int Q; cin >> Q;
	vector<tuple<ll, int, int, int>> vt(Q);
	for (int i = 0; i < Q; i++) {
		int l, r, x; cin >> l >> r >> x;
		l--;
		vt[i] = make_tuple(x, l, r, i);
	}
	sort(vt.begin(), vt.end());

	auto seg_mn = SegmentTree(n, [](ll a, ll b) {return min(a, b); }, INF);
	auto seg_mx = SegmentTree(n, [](ll a, ll b) {return max(a, b); }, -INF);
	using P = pair<ll, int>;
	priority_queue<P, vector<P>, greater<>> pq;
	for (int i = 0; i < n; i++) pq.emplace(X[i], i);
	for (int i = 0; i < n; i++) seg_mn.set(i, X[i]);
	seg_mn.build();
	seg_mx.build();
	vector<ll> ans(Q, INF);
	for (auto [x, l, r, idx] : vt) {
		while (!pq.empty() and pq.top().first <= x) {
			auto [t, i] = pq.top();
			pq.pop();
			seg_mn.update(i, INF);
			seg_mx.update(i, t);
		}
		ll res = INF;
		chmin(res, x - seg_mx.query(l, r));
		chmin(res, seg_mn.query(l, r) - x);
		ans[idx] = res;
	}
	for (auto res : ans) cout << res << "\n";
}
0