結果
問題 | No.1332 Range Nearest Query |
ユーザー | hir355 |
提出日時 | 2021-01-08 23:08:20 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 5,529 bytes |
コンパイル時間 | 2,452 ms |
コンパイル使用メモリ | 189,856 KB |
実行使用メモリ | 16,628 KB |
最終ジャッジ日時 | 2024-11-16 15:37:48 |
合計ジャッジ時間 | 21,411 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
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 | 423 ms
11,984 KB |
testcase_04 | WA | - |
testcase_05 | AC | 414 ms
11,984 KB |
testcase_06 | AC | 378 ms
16,504 KB |
testcase_07 | AC | 377 ms
16,504 KB |
testcase_08 | AC | 369 ms
16,500 KB |
testcase_09 | AC | 367 ms
16,500 KB |
testcase_10 | AC | 361 ms
16,500 KB |
testcase_11 | AC | 377 ms
16,628 KB |
testcase_12 | AC | 385 ms
16,504 KB |
testcase_13 | AC | 381 ms
16,624 KB |
testcase_14 | AC | 375 ms
16,504 KB |
testcase_15 | AC | 375 ms
16,496 KB |
testcase_16 | AC | 426 ms
16,496 KB |
testcase_17 | AC | 439 ms
16,500 KB |
testcase_18 | AC | 442 ms
16,500 KB |
testcase_19 | AC | 434 ms
16,504 KB |
testcase_20 | AC | 453 ms
16,500 KB |
testcase_21 | WA | - |
testcase_22 | AC | 438 ms
16,500 KB |
testcase_23 | AC | 430 ms
16,568 KB |
testcase_24 | AC | 438 ms
16,500 KB |
testcase_25 | WA | - |
testcase_26 | AC | 369 ms
16,496 KB |
testcase_27 | AC | 329 ms
16,500 KB |
testcase_28 | AC | 180 ms
5,248 KB |
testcase_29 | AC | 181 ms
5,248 KB |
testcase_30 | AC | 194 ms
5,248 KB |
testcase_31 | AC | 179 ms
5,248 KB |
testcase_32 | AC | 182 ms
5,248 KB |
testcase_33 | AC | 182 ms
5,248 KB |
testcase_34 | AC | 186 ms
5,248 KB |
testcase_35 | AC | 188 ms
5,248 KB |
testcase_36 | AC | 180 ms
5,248 KB |
testcase_37 | AC | 178 ms
5,248 KB |
testcase_38 | WA | - |
testcase_39 | WA | - |
testcase_40 | AC | 434 ms
16,432 KB |
testcase_41 | WA | - |
testcase_42 | WA | - |
testcase_43 | WA | - |
testcase_44 | WA | - |
testcase_45 | WA | - |
testcase_46 | WA | - |
testcase_47 | AC | 356 ms
10,932 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) // 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;} int 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; }