結果
問題 |
No.1332 Range Nearest Query
|
ユーザー |
|
提出日時 | 2021-01-23 15:53:03 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 378 ms / 2,500 ms |
コード長 | 4,034 bytes |
コンパイル時間 | 1,881 ms |
コンパイル使用メモリ | 146,644 KB |
最終ジャッジ日時 | 2025-01-18 07:32:13 |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 48 |
ソースコード
#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"; }