結果

問題 No.1332 Range Nearest Query
ユーザー tokusakuraitokusakurai
提出日時 2021-01-08 22:20:55
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,605 ms / 2,500 ms
コード長 1,804 bytes
コンパイル時間 2,165 ms
コンパイル使用メモリ 209,360 KB
実行使用メモリ 8,148 KB
最終ジャッジ日時 2023-08-10 10:15:09
合計ジャッジ時間 51,425 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1,477 ms
7,784 KB
testcase_04 AC 1,470 ms
7,712 KB
testcase_05 AC 1,480 ms
7,820 KB
testcase_06 AC 1,125 ms
7,980 KB
testcase_07 AC 1,124 ms
7,984 KB
testcase_08 AC 1,127 ms
8,148 KB
testcase_09 AC 1,123 ms
8,148 KB
testcase_10 AC 1,125 ms
8,044 KB
testcase_11 AC 1,120 ms
7,976 KB
testcase_12 AC 1,126 ms
7,956 KB
testcase_13 AC 1,121 ms
7,960 KB
testcase_14 AC 1,125 ms
8,032 KB
testcase_15 AC 1,124 ms
8,032 KB
testcase_16 AC 1,593 ms
8,024 KB
testcase_17 AC 1,600 ms
7,980 KB
testcase_18 AC 1,605 ms
8,128 KB
testcase_19 AC 1,595 ms
8,028 KB
testcase_20 AC 1,594 ms
8,132 KB
testcase_21 AC 1,598 ms
8,016 KB
testcase_22 AC 1,590 ms
8,020 KB
testcase_23 AC 1,604 ms
7,984 KB
testcase_24 AC 1,605 ms
8,048 KB
testcase_25 AC 1,602 ms
8,048 KB
testcase_26 AC 938 ms
8,088 KB
testcase_27 AC 893 ms
8,144 KB
testcase_28 AC 27 ms
4,380 KB
testcase_29 AC 27 ms
4,380 KB
testcase_30 AC 28 ms
4,380 KB
testcase_31 AC 25 ms
4,384 KB
testcase_32 AC 29 ms
4,384 KB
testcase_33 AC 28 ms
4,384 KB
testcase_34 AC 27 ms
4,384 KB
testcase_35 AC 28 ms
4,380 KB
testcase_36 AC 28 ms
4,380 KB
testcase_37 AC 27 ms
4,380 KB
testcase_38 AC 1,099 ms
5,716 KB
testcase_39 AC 276 ms
4,384 KB
testcase_40 AC 1,571 ms
8,032 KB
testcase_41 AC 641 ms
4,380 KB
testcase_42 AC 1,060 ms
5,580 KB
testcase_43 AC 828 ms
4,816 KB
testcase_44 AC 1,366 ms
6,908 KB
testcase_45 AC 1,285 ms
6,132 KB
testcase_46 AC 995 ms
5,496 KB
testcase_47 AC 1,196 ms
5,900 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define each(e, v) for(auto &e: v)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)x.size()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const int MOD = 1000000007;
//const int MOD = 998244353;
const int inf = (1<<30)-1;
const ll INF = (1LL<<60)-1;
template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;};
template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;};

struct io_setup{
    io_setup(){
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout << fixed << setprecision(15);
    }
} io_setup;

int main(){
    int N;
    cin >> N;
    vector<ll> X(N);
    rep(i, N) cin >> X[i];

    int K = 0;
    while(K*K < 20*N) K++;
    vector<vector<ll>> Xs(N/K+1);
    rep(i, N){
        Xs[i/K].eb(X[i]);
    }
    each(e, Xs){
        e.eb(-INF), e.eb(INF);
        sort(all(e));
    }

    int Q;
    cin >> Q;
    while(Q--){
        int L, R; ll x;
        cin >> L >> R >> x; L--;
        ll ans = INF;
        while(L%K != 0 && L < R){
            chmin(ans, abs(X[L]-x));
            L++;
        }
        while(R%K != 0 && L < R){
            chmin(ans, abs(X[R-1]-x));
            R--;
        }
        while(L < R){
            int i = L/K;
            auto it = lower_bound(all(Xs[i]), x);
            chmin(ans, *it-x);
            it--;
            chmin(ans, x-*it);
            L += K;
        }
        cout << ans << '\n';
    }
}
0