結果

問題 No.1332 Range Nearest Query
ユーザー fastmathfastmath
提出日時 2021-01-08 21:59:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 888 ms / 2,500 ms
コード長 2,584 bytes
コンパイル時間 2,461 ms
コンパイル使用メモリ 207,740 KB
実行使用メモリ 109,956 KB
最終ジャッジ日時 2024-04-28 02:55:36
合計ジャッジ時間 28,625 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 23 ms
31,360 KB
testcase_01 AC 23 ms
31,616 KB
testcase_02 AC 26 ms
31,488 KB
testcase_03 AC 774 ms
83,676 KB
testcase_04 AC 766 ms
83,676 KB
testcase_05 AC 745 ms
83,676 KB
testcase_06 AC 423 ms
109,824 KB
testcase_07 AC 418 ms
109,824 KB
testcase_08 AC 421 ms
109,692 KB
testcase_09 AC 425 ms
109,952 KB
testcase_10 AC 438 ms
109,824 KB
testcase_11 AC 426 ms
109,948 KB
testcase_12 AC 421 ms
109,820 KB
testcase_13 AC 424 ms
109,952 KB
testcase_14 AC 422 ms
109,952 KB
testcase_15 AC 420 ms
109,820 KB
testcase_16 AC 876 ms
109,956 KB
testcase_17 AC 871 ms
109,824 KB
testcase_18 AC 862 ms
109,824 KB
testcase_19 AC 888 ms
109,952 KB
testcase_20 AC 855 ms
109,820 KB
testcase_21 AC 858 ms
109,944 KB
testcase_22 AC 871 ms
109,824 KB
testcase_23 AC 883 ms
109,820 KB
testcase_24 AC 861 ms
109,820 KB
testcase_25 AC 864 ms
109,824 KB
testcase_26 AC 304 ms
109,952 KB
testcase_27 AC 308 ms
109,824 KB
testcase_28 AC 46 ms
31,488 KB
testcase_29 AC 51 ms
31,616 KB
testcase_30 AC 53 ms
31,360 KB
testcase_31 AC 48 ms
31,488 KB
testcase_32 AC 53 ms
31,488 KB
testcase_33 AC 51 ms
31,468 KB
testcase_34 AC 49 ms
31,616 KB
testcase_35 AC 50 ms
31,488 KB
testcase_36 AC 50 ms
31,488 KB
testcase_37 AC 51 ms
31,488 KB
testcase_38 AC 554 ms
69,900 KB
testcase_39 AC 165 ms
33,732 KB
testcase_40 AC 846 ms
108,168 KB
testcase_41 AC 341 ms
42,496 KB
testcase_42 AC 585 ms
68,360 KB
testcase_43 AC 435 ms
52,228 KB
testcase_44 AC 700 ms
79,660 KB
testcase_45 AC 649 ms
76,652 KB
testcase_46 AC 491 ms
56,280 KB
testcase_47 AC 599 ms
73,064 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define x first
#define y second
#define Time (double)clock()/CLOCKS_PER_SEC
#define debug(x) std::cout << #x << ": " << x << '\n';
#define FOR(i, n) for (int i = 0; i < n; ++i)
#define pb push_back
#define trav(a, x) for (auto& a : x)
using vi = vector<int>;
template <typename T>
std::ostream& operator <<(std::ostream& output, const pair <T, T> & data)
{
    output << "(" << data.x << "," << data.y << ")";
    return output;
}
template <typename T>
std::ostream& operator <<(std::ostream& output, const std::vector<T>& data)
{
    for (const T& x : data)
        output << x << " ";
    return output;
}
//ll div_up(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up
//ll div_down(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down 
#define tcT template<class T
#define tcTU tcT, class U
tcT> using V = vector<T>; 
tcT> void re(V<T>& x) { 
    trav(a, x)
        cin >> a;
}
tcT> bool ckmin(T& a, const T& b) {
    return b < a ? a = b, 1 : 0; 
} // set a = min(a,b)
tcT> bool ckmax(T& a, const T& b) {
    return a < b ? a = b, 1 : 0; 
}

const int N = 3e5+7, INF = 1e9+7;
int n, q;
int a[N];
vi t[N << 2];

void build(int v, int tl, int tr) {
    if (tl == tr) {
        t[v] = {a[tl]};
        return;
    }   
    int tm = (tl + tr) >> 1;
    build(v * 2 + 1, tl, tm);
    build(v * 2 + 2, tm + 1, tr);
    trav (x, t[v * 2 + 1])
        t[v].app(x);
    trav (x, t[v * 2 + 2])
        t[v].app(x);
    sort(all(t[v]));
}   

int get(int v, int tl, int tr, int l, int r, int x) {
    if (tr < l || r < tl)
        return INF;
    if (l <= tl && tr <= r) {
        auto u = lower_bound(all(t[v]), x);
        int ans = INF;
        if (u != t[v].begin())
            ckmin(ans, abs(*prev(u) - x));
        if (u != t[v].end())
            ckmin(ans, abs(*u - x));
        return ans;
    }   
    int tm = (tl + tr) >> 1;
    return min(get(v * 2 + 1, tl, tm, l, r, x) , get(v * 2 + 2, tm + 1, tr, l, r, x));
}   

signed main() {
    #ifdef HOME
    freopen("input.txt", "r", stdin);
    #else
    #define endl '\n'
    ios_base::sync_with_stdio(0); cin.tie(0);
    #endif
    cin >> n;
    FOR (i, n)
        cin >> a[i];
    build(0, 0, n - 1);
    cin >> q;            
    while (q--) {
        int l, r, x;
        cin >> l >> r >> x;
        cout << get(0, 0, n - 1, l - 1, r - 1, x) << endl;
    }   
}
0