結果

問題 No.1332 Range Nearest Query
ユーザー IKyoproIKyopro
提出日時 2021-01-08 21:35:54
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,132 ms / 2,500 ms
コード長 2,340 bytes
コンパイル時間 4,467 ms
コンパイル使用メモリ 226,280 KB
実行使用メモリ 7,952 KB
最終ジャッジ日時 2023-08-10 08:53:39
合計ジャッジ時間 32,929 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 991 ms
7,204 KB
testcase_04 AC 975 ms
7,232 KB
testcase_05 AC 982 ms
7,272 KB
testcase_06 AC 278 ms
7,788 KB
testcase_07 AC 272 ms
7,724 KB
testcase_08 AC 272 ms
7,728 KB
testcase_09 AC 270 ms
7,716 KB
testcase_10 AC 275 ms
7,760 KB
testcase_11 AC 282 ms
7,716 KB
testcase_12 AC 273 ms
7,788 KB
testcase_13 AC 269 ms
7,768 KB
testcase_14 AC 270 ms
7,788 KB
testcase_15 AC 269 ms
7,952 KB
testcase_16 AC 1,128 ms
7,932 KB
testcase_17 AC 1,123 ms
7,820 KB
testcase_18 AC 1,110 ms
7,824 KB
testcase_19 AC 1,115 ms
7,800 KB
testcase_20 AC 1,132 ms
7,864 KB
testcase_21 AC 1,114 ms
7,720 KB
testcase_22 AC 1,105 ms
7,760 KB
testcase_23 AC 1,111 ms
7,736 KB
testcase_24 AC 1,113 ms
7,736 KB
testcase_25 AC 1,118 ms
7,752 KB
testcase_26 AC 826 ms
7,720 KB
testcase_27 AC 1,036 ms
7,948 KB
testcase_28 AC 27 ms
4,376 KB
testcase_29 AC 27 ms
4,376 KB
testcase_30 AC 26 ms
4,376 KB
testcase_31 AC 24 ms
4,376 KB
testcase_32 AC 26 ms
4,376 KB
testcase_33 AC 27 ms
4,380 KB
testcase_34 AC 25 ms
4,380 KB
testcase_35 AC 25 ms
4,380 KB
testcase_36 AC 27 ms
4,384 KB
testcase_37 AC 25 ms
4,376 KB
testcase_38 AC 597 ms
5,344 KB
testcase_39 AC 80 ms
4,380 KB
testcase_40 AC 1,095 ms
7,500 KB
testcase_41 AC 258 ms
4,380 KB
testcase_42 AC 567 ms
5,380 KB
testcase_43 AC 387 ms
4,692 KB
testcase_44 AC 855 ms
6,752 KB
testcase_45 AC 769 ms
6,128 KB
testcase_46 AC 516 ms
5,120 KB
testcase_47 AC 688 ms
5,880 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;
template<class T> bool chmin(T& a,T b){if(a>b) {a = b; return true;} return false;}
template<class T> bool chmax(T& a,T b){if(a<b) {a = b; return true;} return false;}
#define rep(i,n) for(int i=0;i<(n);i++)
#define drep(i,n) for(int i=(n)-1;i>=0;i--)
#define all(x) (x).begin(),(x).end()
#define debug(x)  cerr << #x << " = " << (x) << endl;

constexpr int si = 600;
constexpr int inf = 1e9;

class SqrtDecomposition{
private:

    class bucket{
        vec<int> val;
        vec<int> s;
        int N,l,r;

        bool invalid(int a,int b){
            return max(l,a)>min(b,r);
        }


        int query_all(int x){
            int res = inf;
            int id = lower_bound(all(s),x)-s.begin();
            if(id!=N) chmin(res,abs(x-s[id]));
            if(id!=0) chmin(res,abs(x-s[id-1]));
            return res;
        }

        int query_partial(int a,int b,int x){
            int res = inf;
            for(int i=a;i<b;i++) chmin(res,abs(x-val[i]));
            return res;
        }

    public:
        bucket(int l,int r,vec<int>& A):N(r-l),l(l),r(r),val(A),s(A){
            sort(all(s));
        }

        int query(int a,int b,int x){
            if(invalid(a,b)) return inf;
            if(a<=l && r<=b) return query_all(x);
            else return query_partial(max(l,a)-l,min(r,b)-l,x);
        }
    };

    int N;
    vec<bucket> B;

public:
    SqrtDecomposition(int N,vec<int> A):N(N){
        for(int l=0;l<N;l+=si){
            int r = min(N,si+l);
            vec<int> val;
            for(int i=l;i<r;i++) val.push_back(A[i]);
            bucket b(l,r,val);
            B.push_back(b);
        }
    }

    int query(int l,int r,int x){
        int res = inf;
        for(auto& b:B) chmin(res,b.query(l,r,x));
        return res;
    }

};


int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N;
    cin >> N;
    vec<int> X(N);
    rep(i,N) cin >> X[i];
    SqrtDecomposition D(N,X);
    int Q;
    cin >> Q;
    rep(_,Q){
        int l,r,x;
        cin >> l >> r >> x;
        l--,r--;
        cout << D.query(l,r+1,x) << "\n";
    }
}
0