結果

問題 No.1332 Range Nearest Query
ユーザー Ricky_ponRicky_pon
提出日時 2021-01-08 23:06:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,264 ms / 2,500 ms
コード長 2,355 bytes
コンパイル時間 2,264 ms
コンパイル使用メモリ 202,036 KB
実行使用メモリ 6,112 KB
最終ジャッジ日時 2023-08-10 11:37:24
合計ジャッジ時間 35,616 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1,112 ms
5,588 KB
testcase_04 AC 1,105 ms
5,572 KB
testcase_05 AC 1,119 ms
5,720 KB
testcase_06 AC 349 ms
5,816 KB
testcase_07 AC 348 ms
5,944 KB
testcase_08 AC 350 ms
5,988 KB
testcase_09 AC 353 ms
5,944 KB
testcase_10 AC 348 ms
5,804 KB
testcase_11 AC 349 ms
5,888 KB
testcase_12 AC 349 ms
5,880 KB
testcase_13 AC 349 ms
5,944 KB
testcase_14 AC 347 ms
5,988 KB
testcase_15 AC 350 ms
5,880 KB
testcase_16 AC 1,246 ms
5,876 KB
testcase_17 AC 1,251 ms
5,988 KB
testcase_18 AC 1,264 ms
5,820 KB
testcase_19 AC 1,250 ms
5,988 KB
testcase_20 AC 1,258 ms
5,804 KB
testcase_21 AC 1,241 ms
5,872 KB
testcase_22 AC 1,249 ms
6,016 KB
testcase_23 AC 1,257 ms
6,112 KB
testcase_24 AC 1,249 ms
5,912 KB
testcase_25 AC 1,251 ms
5,912 KB
testcase_26 AC 873 ms
5,924 KB
testcase_27 AC 763 ms
5,820 KB
testcase_28 AC 78 ms
4,376 KB
testcase_29 AC 78 ms
4,376 KB
testcase_30 AC 79 ms
4,380 KB
testcase_31 AC 76 ms
4,380 KB
testcase_32 AC 79 ms
4,380 KB
testcase_33 AC 79 ms
4,380 KB
testcase_34 AC 77 ms
4,376 KB
testcase_35 AC 77 ms
4,380 KB
testcase_36 AC 78 ms
4,380 KB
testcase_37 AC 78 ms
4,380 KB
testcase_38 AC 722 ms
4,848 KB
testcase_39 AC 197 ms
4,376 KB
testcase_40 AC 1,226 ms
5,852 KB
testcase_41 AC 372 ms
4,380 KB
testcase_42 AC 687 ms
4,600 KB
testcase_43 AC 500 ms
4,384 KB
testcase_44 AC 966 ms
5,296 KB
testcase_45 AC 875 ms
5,096 KB
testcase_46 AC 630 ms
4,544 KB
testcase_47 AC 804 ms
4,928 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

//#include <atcoder/all>
#define For(i, a, b) for (int(i) = (int)(a); (i) < (int)(b); ++(i))
#define rFor(i, a, b) for (int(i) = (int)(a)-1; (i) >= (int)(b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
typedef pair<int, int> pii;
typedef pair<lint, lint> pll;
template <class T>
bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
template <class T>
bool chmin(T &a, const T &b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template <class T>
T div_floor(T a, T b) {
    if (b < 0) a *= -1, b *= -1;
    return a >= 0 ? a / b : (a + 1) / b - 1;
}
template <class T>
T div_ceil(T a, T b) {
    if (b < 0) a *= -1, b *= -1;
    return a > 0 ? (a - 1) / b + 1 : a / b;
}

constexpr lint mod = 1000000007;
constexpr lint INF = mod * mod;
constexpr int MAX = 300010;

constexpr int width = 600;
int sorted_x[width * 510], x[width * 510];

int get_min(int k, int y, int l, int r) {
    int ret = mod;
    For(i, k * width, (k + 1) * width) if (l <= i && i < r) {
        chmin(ret, abs(x[i] - y));
    }
    return ret;
}

int get_backet_min(int k, int y) {
    int l = k * width, r = (k + 1) * width;
    int i = upper_bound(sorted_x + l, sorted_x + r, y) - sorted_x;
    int ret = mod;
    if (i > l) chmin(ret, abs(sorted_x[i - 1] - y));
    if (i < r) chmin(ret, abs(sorted_x[i] - y));
    return ret;
}

int main() {
    int n;
    scanf("%d", &n);
    int num = div_ceil(n, width);
    rep(i, width * num) sorted_x[i] = x[i] = 2 * mod;
    rep(i, n) {
        scanf("%d", &x[i]);
        sorted_x[i] = x[i];
    }
    rep(k, num) sort(sorted_x + k * width, sorted_x + (k + 1) * width);

    int q;
    scanf("%d", &q);
    rep(_, q) {
        int l, r, y;
        lint x;
        scanf("%d%d%d", &l, &r, &y);
        --l;
        int ans = mod;
        rep(k, num) {
            int ll = k * width, rr = (k + 1) * width;
            if (r <= ll || rr <= l)
                continue;
            else if (l <= ll && rr <= r)
                chmin(ans, get_backet_min(k, y));
            else
                chmin(ans, get_min(k, y, l, r));
        }
        printf("%d\n", ans);
    }
}
0