結果
問題 | No.1332 Range Nearest Query |
ユーザー | hir355 |
提出日時 | 2021-01-08 23:16:08 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 503 ms / 2,500 ms |
コード長 | 5,554 bytes |
コンパイル時間 | 2,243 ms |
コンパイル使用メモリ | 190,164 KB |
実行使用メモリ | 29,792 KB |
最終ジャッジ日時 | 2024-11-16 17:34:18 |
合計ジャッジ時間 | 22,784 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 2 ms
5,248 KB |
testcase_03 | AC | 454 ms
20,608 KB |
testcase_04 | AC | 459 ms
20,688 KB |
testcase_05 | AC | 462 ms
20,680 KB |
testcase_06 | AC | 399 ms
29,700 KB |
testcase_07 | AC | 401 ms
29,596 KB |
testcase_08 | AC | 405 ms
29,596 KB |
testcase_09 | AC | 401 ms
29,724 KB |
testcase_10 | AC | 406 ms
29,724 KB |
testcase_11 | AC | 406 ms
29,724 KB |
testcase_12 | AC | 406 ms
29,688 KB |
testcase_13 | AC | 407 ms
29,792 KB |
testcase_14 | AC | 402 ms
29,728 KB |
testcase_15 | AC | 397 ms
29,724 KB |
testcase_16 | AC | 493 ms
29,600 KB |
testcase_17 | AC | 501 ms
29,728 KB |
testcase_18 | AC | 491 ms
29,724 KB |
testcase_19 | AC | 487 ms
29,724 KB |
testcase_20 | AC | 494 ms
29,728 KB |
testcase_21 | AC | 486 ms
29,684 KB |
testcase_22 | AC | 492 ms
29,708 KB |
testcase_23 | AC | 483 ms
29,704 KB |
testcase_24 | AC | 503 ms
29,724 KB |
testcase_25 | AC | 497 ms
29,724 KB |
testcase_26 | AC | 395 ms
29,724 KB |
testcase_27 | AC | 351 ms
29,728 KB |
testcase_28 | AC | 189 ms
6,528 KB |
testcase_29 | AC | 189 ms
6,400 KB |
testcase_30 | AC | 197 ms
6,400 KB |
testcase_31 | AC | 188 ms
6,400 KB |
testcase_32 | AC | 192 ms
6,400 KB |
testcase_33 | AC | 188 ms
6,400 KB |
testcase_34 | AC | 188 ms
6,400 KB |
testcase_35 | AC | 187 ms
6,400 KB |
testcase_36 | AC | 183 ms
6,400 KB |
testcase_37 | AC | 188 ms
6,400 KB |
testcase_38 | AC | 363 ms
18,072 KB |
testcase_39 | AC | 247 ms
7,040 KB |
testcase_40 | AC | 492 ms
29,536 KB |
testcase_41 | AC | 286 ms
9,784 KB |
testcase_42 | AC | 355 ms
17,868 KB |
testcase_43 | AC | 324 ms
12,444 KB |
testcase_44 | AC | 436 ms
19,812 KB |
testcase_45 | AC | 412 ms
19,208 KB |
testcase_46 | AC | 350 ms
13,368 KB |
testcase_47 | AC | 395 ms
18,676 KB |
ソースコード
#include <algorithm> #include <cassert> #include <vector> #ifdef _MSC_VER #include <intrin.h> #endif namespace atcoder { namespace internal { int ceil_pow2(int n) { int x = 0; while ((1U << x) < (unsigned int)(n)) x++; return x; } int bsf(unsigned int n) { #ifdef _MSC_VER unsigned long index; _BitScanForward(&index, n); return index; #else return __builtin_ctz(n); #endif } } // namespace internal } // namespace atcoder namespace atcoder { template <class S, S (*op)(S, S), S (*e)()> struct segtree { public: segtree() : segtree(0) {} segtree(int n) : segtree(std::vector<S>(n, e())) {} segtree(const std::vector<S>& v) : _n(int(v.size())) { log = internal::ceil_pow2(_n); size = 1 << log; d = std::vector<S>(2 * size, e()); for (int i = 0; i < _n; i++) d[size + i] = v[i]; for (int i = size - 1; i >= 1; i--) { update(i); } } void set(int p, S x) { assert(0 <= p && p < _n); p += size; d[p] = x; for (int i = 1; i <= log; i++) update(p >> i); } S get(int p) { assert(0 <= p && p < _n); return d[p + size]; } S prod(int l, int r) { assert(0 <= l && l <= r && r <= _n); S sml = e(), smr = e(); l += size; r += size; while (l < r) { if (l & 1) sml = op(sml, d[l++]); if (r & 1) smr = op(d[--r], smr); l >>= 1; r >>= 1; } return op(sml, smr); } S all_prod() { return d[1]; } template <bool (*f)(S)> int max_right(int l) { return max_right(l, [](S x) { return f(x); }); } template <class F> int max_right(int l, F f) { assert(0 <= l && l <= _n); assert(f(e())); if (l == _n) return _n; l += size; S sm = e(); do { while (l % 2 == 0) l >>= 1; if (!f(op(sm, d[l]))) { while (l < size) { l = (2 * l); if (f(op(sm, d[l]))) { sm = op(sm, d[l]); l++; } } return l - size; } sm = op(sm, d[l]); l++; } while ((l & -l) != l); return _n; } template <bool (*f)(S)> int min_left(int r) { return min_left(r, [](S x) { return f(x); }); } template <class F> int min_left(int r, F f) { assert(0 <= r && r <= _n); assert(f(e())); if (r == 0) return 0; r += size; S sm = e(); do { r--; while (r > 1 && (r % 2)) r >>= 1; if (!f(op(d[r], sm))) { while (r < size) { r = (2 * r + 1); if (f(op(d[r], sm))) { sm = op(d[r], sm); r--; } } return r + 1 - size; } sm = op(d[r], sm); } while ((r & -r) != r); return 0; } private: int _n, size, log; std::vector<S> d; void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); } }; } // namespace atcoder #include <bits/stdc++.h> using namespace std; using namespace atcoder; #define INF_LL 100000000000000000LL #define INF 2000000000 #define MOD 1000000007 #define ll long long #define all(x) x.begin(), x.end() #define REP(i, a, b) for(int i = a; i < b; i++) #define rep(i, n) REP(i, 0, n) #define int long long // typedef float double; // typedef priority_queue prique; typedef pair<ll, ll> P; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<P> vp; typedef vector<ll> vl; typedef vector<vi> matrix; int dx[4] = {0, 1, 0, -1}; int dy[4] = {1, 0, -1, 0}; int sign[2] = {1, -1}; template <class T> bool chmax(T &a, T b) { if(a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, T b) { if(a > b) { a = b; return 1; } return 0; } ll modpow(ll a, ll b, ll m) { if(b == 0) return 1; ll t = modpow(a, b / 2, m); if(b & 1) { return (t * t % m) * a % m; } else { return t * t % m; } } struct edge { int to; ll cost; edge(int t, ll c) { to = t, cost = c; } }; typedef vector<vector<edge>> graph; // using mint = modint998244353; int minop(int x, int y) {return min(x, y);} int maxop(int x, int y) {return max(x, y);} int mine() {return INF;} int maxe() {return -INF;} signed main() { int n, q; cin >> n; vector<pair<int, int>> a(n); rep(i, n) {cin >> a[i].first;a[i].second = i;} segtree<int, maxop, maxe> seg1(n); segtree<int, minop, mine> seg2(n); rep(i, n) seg2.set(i, a[i].first); sort(all(a)); cin >> q; vector<tuple<int, int, int, int>> query(q); rep(i, q){ int l, r, x; cin >> l >> r >> x; query[i] = {x, l, r, i}; } sort(all(query)); int idx = 0; rep(i, q){ int x, l, r, g; tie(x, l, r, g) = query[i]; while(idx < n && x > a[idx].first){ seg1.set(a[idx].second, seg2.get(a[idx].second)); seg2.set(a[idx].second, mine()); idx++; } get<0>(query[i]) = min(seg2.prod(l - 1, r) - x, x - seg1.prod(l - 1, r)); } sort(begin(query), end(query), [](const auto &x, const auto &y){return get<3>(x) < get<3>(y);}); rep(i, q){ cout << get<0>(query[i]) << endl; } return 0; }