結果

問題 No.1332 Range Nearest Query
ユーザー Ricky_pon
提出日時 2021-01-08 23:06:58
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,469 ms / 2,500 ms
コード長 2,355 bytes
コンパイル時間 2,162 ms
コンパイル使用メモリ 197,276 KB
最終ジャッジ日時 2025-01-17 14:11:45
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 48
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:68:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   68 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
main.cpp:72:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   72 |         scanf("%d", &x[i]);
      |         ~~~~~^~~~~~~~~~~~~
main.cpp:78:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   78 |     scanf("%d", &q);
      |     ~~~~~^~~~~~~~~~
main.cpp:82:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   82 |         scanf("%d%d%d", &l, &r, &y);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~

ソースコード

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