結果

問題 No.1332 Range Nearest Query
ユーザー fumofumofunifumofumofuni
提出日時 2021-01-08 23:26:52
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,806 ms / 2,500 ms
コード長 1,968 bytes
コンパイル時間 3,553 ms
コンパイル使用メモリ 206,304 KB
実行使用メモリ 10,056 KB
最終ジャッジ日時 2023-08-10 12:27:54
合計ジャッジ時間 53,090 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
9,872 KB
testcase_01 AC 9 ms
9,904 KB
testcase_02 AC 8 ms
9,832 KB
testcase_03 AC 1,580 ms
9,872 KB
testcase_04 AC 1,571 ms
9,820 KB
testcase_05 AC 1,589 ms
9,836 KB
testcase_06 AC 568 ms
9,880 KB
testcase_07 AC 566 ms
9,804 KB
testcase_08 AC 558 ms
9,872 KB
testcase_09 AC 567 ms
9,876 KB
testcase_10 AC 568 ms
9,860 KB
testcase_11 AC 566 ms
9,868 KB
testcase_12 AC 566 ms
9,920 KB
testcase_13 AC 560 ms
9,832 KB
testcase_14 AC 562 ms
10,032 KB
testcase_15 AC 562 ms
9,912 KB
testcase_16 AC 1,771 ms
9,888 KB
testcase_17 AC 1,806 ms
9,876 KB
testcase_18 AC 1,788 ms
9,884 KB
testcase_19 AC 1,788 ms
9,832 KB
testcase_20 AC 1,788 ms
9,920 KB
testcase_21 AC 1,778 ms
9,864 KB
testcase_22 AC 1,796 ms
9,876 KB
testcase_23 AC 1,792 ms
9,868 KB
testcase_24 AC 1,799 ms
9,916 KB
testcase_25 AC 1,795 ms
9,876 KB
testcase_26 AC 1,243 ms
9,876 KB
testcase_27 AC 1,076 ms
9,868 KB
testcase_28 AC 286 ms
9,768 KB
testcase_29 AC 293 ms
9,908 KB
testcase_30 AC 288 ms
9,768 KB
testcase_31 AC 290 ms
9,864 KB
testcase_32 AC 289 ms
9,832 KB
testcase_33 AC 293 ms
9,880 KB
testcase_34 AC 289 ms
10,056 KB
testcase_35 AC 291 ms
9,932 KB
testcase_36 AC 289 ms
9,908 KB
testcase_37 AC 289 ms
9,912 KB
testcase_38 AC 1,063 ms
9,876 KB
testcase_39 AC 430 ms
9,900 KB
testcase_40 AC 1,702 ms
9,868 KB
testcase_41 AC 636 ms
9,832 KB
testcase_42 AC 1,028 ms
9,876 KB
testcase_43 AC 792 ms
9,796 KB
testcase_44 AC 1,416 ms
9,876 KB
testcase_45 AC 1,308 ms
9,840 KB
testcase_46 AC 960 ms
9,836 KB
testcase_47 AC 1,197 ms
9,936 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=(n)-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define rev(x) reverse(x);
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
const ll dy[8]={-1,0,1,0,1,1,-1,-1};
const ll dx[8]={0,-1,0,1,1,-1,1,-1};
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
ll near(vl &v,ll t){
    if(v.empty())return INF;
    auto p=lower_bound(all(v),t);
    ll ret=INF;
    if(p!=v.end()){
        chmin(ret,abs(t-*p));
    }
    if(p!=v.begin()){
        p--;
        chmin(ret,abs(t-*p));
    }
    return ret;
}

int main(){
    ll n;cin >> n;ll m=550,K=550;
    vl X(m*K,INF);rep(i,n)cin >> X[i];
    vvl b(m);
    rep(i,X.size()){
        b[i/K].pb(X[i]);
    }
    rep(i,m)sort(all(b[i]));
    ll q;cin >> q;
    while(q--){
        ll x,y,t;cin >>x >> y >> t;x--;
        ll ans=INF;
        for (ll k = 0; k < m; ++k) {
            ll l = k * K, r = (k + 1) * K;
            if (r <= x || y <= l)
                continue;
            if (x <= l && r <= y) {
                chmin(ans,near(b[k],t));
            } 
            else {
                for (int i = max(x, l); i < min(y, r); ++i) {
                    chmin(ans,abs(t-X[i]));
                }
            }
        }
        cout << ans <<endl;
    }
}  
0