結果

問題 No.1332 Range Nearest Query
ユーザー milanis48663220milanis48663220
提出日時 2021-01-09 00:38:57
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,910 ms / 2,500 ms
コード長 4,145 bytes
コンパイル時間 1,482 ms
コンパイル使用メモリ 134,868 KB
実行使用メモリ 50,692 KB
最終ジャッジ日時 2024-04-28 07:00:56
合計ジャッジ時間 59,734 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 1,762 ms
46,572 KB
testcase_04 AC 1,762 ms
46,648 KB
testcase_05 AC 1,772 ms
46,700 KB
testcase_06 AC 1,119 ms
49,796 KB
testcase_07 AC 1,134 ms
49,676 KB
testcase_08 AC 1,128 ms
49,532 KB
testcase_09 AC 1,176 ms
49,552 KB
testcase_10 AC 1,170 ms
49,792 KB
testcase_11 AC 1,115 ms
49,532 KB
testcase_12 AC 1,133 ms
49,536 KB
testcase_13 AC 1,285 ms
49,924 KB
testcase_14 AC 1,126 ms
49,664 KB
testcase_15 AC 1,129 ms
49,792 KB
testcase_16 AC 1,896 ms
50,560 KB
testcase_17 AC 1,910 ms
50,436 KB
testcase_18 AC 1,834 ms
50,560 KB
testcase_19 AC 1,890 ms
50,428 KB
testcase_20 AC 1,867 ms
50,428 KB
testcase_21 AC 1,873 ms
50,384 KB
testcase_22 AC 1,857 ms
50,428 KB
testcase_23 AC 1,831 ms
50,524 KB
testcase_24 AC 1,875 ms
50,692 KB
testcase_25 AC 1,830 ms
50,688 KB
testcase_26 AC 474 ms
40,704 KB
testcase_27 AC 989 ms
50,432 KB
testcase_28 AC 239 ms
10,228 KB
testcase_29 AC 245 ms
10,100 KB
testcase_30 AC 238 ms
10,096 KB
testcase_31 AC 229 ms
10,224 KB
testcase_32 AC 239 ms
10,108 KB
testcase_33 AC 241 ms
10,220 KB
testcase_34 AC 228 ms
10,224 KB
testcase_35 AC 234 ms
10,228 KB
testcase_36 AC 231 ms
10,228 KB
testcase_37 AC 240 ms
10,220 KB
testcase_38 AC 1,451 ms
35,532 KB
testcase_39 AC 1,111 ms
21,016 KB
testcase_40 AC 1,801 ms
49,504 KB
testcase_41 AC 1,218 ms
25,548 KB
testcase_42 AC 1,423 ms
34,732 KB
testcase_43 AC 1,341 ms
29,272 KB
testcase_44 AC 1,665 ms
44,640 KB
testcase_45 AC 1,588 ms
40,192 KB
testcase_46 AC 1,404 ms
33,388 KB
testcase_47 AC 1,584 ms
40,160 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cassert>
#include <cmath>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

template <typename T>
struct segtree{
    int n;
    T UNIT;
    vector<T> dat;
    T (*calc)(T, T);

    segtree(int n_, T unit, T (*_calc)(T, T)){
        UNIT = unit;
        calc = _calc;
        n = 1;
        while(n < n_) n *= 2;
        dat = vector<T>(2*n);
        for(int i = 0; i < 2*n; i++) dat[i] = UNIT;
    }

    void insert(int k, T a){
        dat[k+n-1] = a;
    }
    void update_all(){
        for(int i = n-2; i >= 0; i--){
            dat[i] = calc(dat[i*2+1], dat[i*2+2]);
        }
    }
    //k番目の値(0-indexed)をaに変更
    void update(int k, T a){
        k += n-1;
        dat[k] = a;
        while(k > 0){
            k = (k-1)/2;
            dat[k] = calc(dat[k*2+1], dat[k*2+2]);
        }
    }

    //[a, b)
    //区間[a, b]へのクエリに対してはquery(a, b+1)と呼ぶ
    T query(int a, int b, int k=0, int l=0, int r=-1){
        if(r < 0) r = n;
        if(r <= a || b <= l) return UNIT;
        if(a <= l && r <= b) return dat[k];
        else{
            T vl = query(a, b, k*2+1, l, (l+r)/2);
            T vr = query(a, b, k*2+2, (l+r)/2, r);
            return calc(vl, vr);
        }
    }
};

int n;
int X[300000];
int q;
int l[100000], r[100000], x[100000], q_idx[100000];
map<int, int> compress;
vector<int> recover;

void input(){
    cin >> n;
    for(int i = 0; i < n; i++) cin >> X[i];
    cin >> q;
    vector<vector<int>> v;
    for(int i = 0; i < q; i++){
        int l, r, x;
        cin >> l >> r >> x; l--; r--;
        v.push_back({r, l, x, i});
    }
    sort(v.begin(), v.end());
    for(int i = 0; i < q; i++){
        l[i] = v[i][1], r[i] = v[i][0], x[i] = v[i][2], q_idx[i] = v[i][3];
    }
    {
        set<int> st;
        for(int i = 0; i < n; i++) st.insert(X[i]);
        for(int i = 0; i < q; i++) st.insert(x[i]);
        int i = 0;
        for(int y : st){
            compress[y] = i;
            recover.push_back(y);
            i++;
        }
    }
}

int calc_max(int a, int b){ return max(a, b); } 

const int INF = 2e9;

void solve(){
    int m = compress.size();
    segtree<int> sgt(m, -INF, calc_max);
    int cur_r = -1;
    vector<int> ret(q);
    for(int i = 0; i < q; i++){
        // cout << l[i] << ',' << r[i] << endl;
        int ans = INF;
        while(cur_r < r[i]){
            cur_r++;
            sgt.update(compress[X[cur_r]], cur_r);
        }
        int idx = compress[x[i]];
        // idxより小さい最大の値
        if(sgt.query(0, idx+1) >= l[i]){
            if(sgt.query(idx, idx+1) >= l[i]){
                ans = 0;
            }else{
                int lb = 0, rb = idx;
                while(rb-lb > 1){
                    int c = (lb+rb)/2;
                    if(sgt.query(c, idx+1) >= l[i]) lb = c;
                    else rb = c;
                }
                chmin(ans, abs(x[i]-recover[lb]));
            }
        }
        // idxより大きい最小の値
        if(sgt.query(idx, m) >= l[i]){
            if(sgt.query(idx, idx+1) >= l[i]){
                ans = 0;
            }else{
                int lb = idx, rb = m;
                while(rb-lb > 1){
                    int c = (lb+rb)/2;
                    if(sgt.query(idx, c+1) >= l[i]) rb = c;
                    else lb = c;
                }
                chmin(ans, abs(x[i]-recover[rb]));
            }   
        }
        ret[q_idx[i]] = ans;
    }
    for(int i = 0; i < q; i++) cout << ret[i] << endl;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    input();
    solve();
}
0