結果

問題 No.1332 Range Nearest Query
ユーザー milanis48663220milanis48663220
提出日時 2021-01-08 22:43:52
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,209 bytes
コンパイル時間 1,458 ms
コンパイル使用メモリ 121,512 KB
実行使用メモリ 25,612 KB
最終ジャッジ日時 2024-04-28 04:12:26
合計ジャッジ時間 12,121 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
13,884 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 3 ms
6,944 KB
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 AC 189 ms
9,452 KB
testcase_29 AC 192 ms
9,464 KB
testcase_30 AC 192 ms
9,340 KB
testcase_31 AC 187 ms
11,356 KB
testcase_32 AC 188 ms
9,464 KB
testcase_33 AC 189 ms
9,460 KB
testcase_34 AC 190 ms
10,740 KB
testcase_35 AC 189 ms
9,364 KB
testcase_36 AC 188 ms
9,484 KB
testcase_37 AC 191 ms
9,468 KB
testcase_38 TLE -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
権限があれば一括ダウンロードができます

ソースコード

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;

int n;
ll X[300000];
int q;
ll ans[100000];
vector<vector<int>> query[400]; // {r, l, x, idx}

void input(){
    cin >> n;
    for(int i = 0; i < n; i++) cin >> X[i];
    int sq = sqrt(n)+10;
    cin >> q;
    for(int i = 0; i < q; i++){
        int l, r, x; cin >> l >> r >> x;
        l--; r--;
        query[l/sq].push_back({r, l, x, i});
    }
    for(int i = 0; i <= sq; i++){
        sort(query[i].begin(), query[i].end());
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    input();
    if(n >= 200000) return -1;
    multiset<ll> st;
    int cur_l = 0, cur_r = 0;
    st.insert(X[0]);
    for(int s = 0; s*s <= n; s++){
        for(auto v : query[s]){
            int r = v[0], l = v[1], idx = v[3];
            ll x = v[2];
            while(cur_r < r){
                cur_r++;
                st.insert(X[cur_r]);
            }
            while(cur_r > r){
                st.erase(st.find(X[cur_r]));
                cur_r--;
            }
            while(cur_l < l){
                st.erase(st.find(X[cur_l]));
                cur_l++;
            }
            while(cur_l > l){
                cur_l--;
                st.insert(X[cur_l]);
            }
            auto p = st.lower_bound(x);
            ll a = 1e15;
            if(p != st.end()) chmin(a, abs(*p-(x)));
            if(p != st.begin()){
                p--;
                chmin(a, abs(*p-(x)));
            }
            ans[idx] = a;
            // cout << l << ' ' << r << ' ' << st.size() << endl;
        }
    }
    for(int i = 0; i < q; i++) cout << ans[i] << endl;
}
0