結果

問題 No.1332 Range Nearest Query
ユーザー KudeKude
提出日時 2021-01-08 23:05:05
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 1,700 bytes
コンパイル時間 3,094 ms
コンパイル使用メモリ 204,840 KB
最終ジャッジ日時 2025-01-17 14:05:32
ジャッジサーバーID
(参考情報)
judge4 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 27 TLE * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = (n)-1; i >= 0; --i)
#define chmax(a, b) a = max(a, b)
template<class T>
void chmin(T& a, const T& b) {
    a = min<T>(a, b);
}
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;

struct Q {
    int i;
    int l, r, x;
    bool operator<(Q& y) {
        int bx = l / 550, by = y.l / 550;
        return (bx < by) || (bx == by && r < y.r);
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    VI x(n);
    rep(i, n) cin >> x[i];
    int q;
    cin >> q;
    vector<Q> qs(q);
    rep(i, q) {
        qs[i].i = i;
        cin >> qs[i].l >> qs[i].r >> qs[i].x;
        qs[i].l--;
    }
    sort(all(qs));
    int cur_b = -1, blast = -1;
    VI ans(q);
    set<int> s;
    int p = -1;
    rep(i, q) {
        int l = qs[i].l, r = qs[i].r, z = qs[i].x;
        int b = l / 550;
        if (b != cur_b) {
            cur_b = b;
            s.clear();
            blast = 550 * (b + 1);
            p = blast;
        }
        while(p < r) s.insert(x[p++]);
        auto it = s.lower_bound(z);
        int tans = 1001001001;
        if (it != s.end()) {
            chmin(tans, *it - z);
        }
        if (it != s.begin()) {
            it--;
            chmin(tans, z - *it);
        }
        for(int j = l; j < min({blast, n, r}); ++j) {
            chmin(tans, abs(x[j] - z));
        }
        ans[qs[i].i] = tans;
    }
    rep(i, q) cout << ans[i] << '\n';
}
0