結果

問題 No.1332 Range Nearest Query
ユーザー hir355hir355
提出日時 2021-01-08 23:16:08
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 524 ms / 2,500 ms
コード長 5,554 bytes
コンパイル時間 3,144 ms
コンパイル使用メモリ 188,768 KB
実行使用メモリ 29,760 KB
最終ジャッジ日時 2023-08-10 12:08:05
合計ジャッジ時間 24,579 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 470 ms
20,512 KB
testcase_04 AC 468 ms
20,508 KB
testcase_05 AC 464 ms
20,504 KB
testcase_06 AC 397 ms
29,444 KB
testcase_07 AC 396 ms
29,488 KB
testcase_08 AC 396 ms
29,544 KB
testcase_09 AC 396 ms
29,516 KB
testcase_10 AC 394 ms
29,760 KB
testcase_11 AC 400 ms
29,488 KB
testcase_12 AC 393 ms
29,572 KB
testcase_13 AC 394 ms
29,520 KB
testcase_14 AC 395 ms
29,704 KB
testcase_15 AC 400 ms
29,492 KB
testcase_16 AC 502 ms
29,740 KB
testcase_17 AC 513 ms
29,508 KB
testcase_18 AC 524 ms
29,644 KB
testcase_19 AC 511 ms
29,552 KB
testcase_20 AC 514 ms
29,444 KB
testcase_21 AC 511 ms
29,644 KB
testcase_22 AC 505 ms
29,448 KB
testcase_23 AC 513 ms
29,588 KB
testcase_24 AC 499 ms
29,512 KB
testcase_25 AC 500 ms
29,448 KB
testcase_26 AC 395 ms
29,708 KB
testcase_27 AC 350 ms
29,508 KB
testcase_28 AC 193 ms
6,100 KB
testcase_29 AC 199 ms
6,416 KB
testcase_30 AC 198 ms
6,148 KB
testcase_31 AC 195 ms
6,188 KB
testcase_32 AC 202 ms
6,288 KB
testcase_33 AC 200 ms
6,224 KB
testcase_34 AC 194 ms
6,184 KB
testcase_35 AC 193 ms
6,200 KB
testcase_36 AC 195 ms
6,184 KB
testcase_37 AC 196 ms
6,436 KB
testcase_38 AC 375 ms
17,980 KB
testcase_39 AC 257 ms
6,768 KB
testcase_40 AC 495 ms
29,588 KB
testcase_41 AC 300 ms
9,696 KB
testcase_42 AC 376 ms
17,692 KB
testcase_43 AC 328 ms
12,656 KB
testcase_44 AC 443 ms
19,648 KB
testcase_45 AC 414 ms
19,112 KB
testcase_46 AC 354 ms
13,316 KB
testcase_47 AC 390 ms
18,392 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0