結果

問題 No.1332 Range Nearest Query
ユーザー chocoruskchocorusk
提出日時 2021-01-08 21:38:09
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 925 ms / 2,500 ms
コード長 1,649 bytes
コンパイル時間 1,130 ms
コンパイル使用メモリ 136,828 KB
実行使用メモリ 67,988 KB
最終ジャッジ日時 2024-04-28 02:25:41
合計ジャッジ時間 29,609 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 779 ms
49,508 KB
testcase_04 AC 767 ms
49,392 KB
testcase_05 AC 775 ms
49,388 KB
testcase_06 AC 486 ms
67,760 KB
testcase_07 AC 487 ms
67,852 KB
testcase_08 AC 508 ms
67,856 KB
testcase_09 AC 484 ms
67,856 KB
testcase_10 AC 491 ms
67,804 KB
testcase_11 AC 485 ms
67,856 KB
testcase_12 AC 494 ms
67,988 KB
testcase_13 AC 494 ms
67,856 KB
testcase_14 AC 501 ms
67,856 KB
testcase_15 AC 501 ms
67,844 KB
testcase_16 AC 849 ms
67,764 KB
testcase_17 AC 862 ms
67,856 KB
testcase_18 AC 866 ms
67,980 KB
testcase_19 AC 861 ms
67,856 KB
testcase_20 AC 856 ms
67,852 KB
testcase_21 AC 864 ms
67,856 KB
testcase_22 AC 925 ms
67,852 KB
testcase_23 AC 915 ms
67,856 KB
testcase_24 AC 907 ms
67,724 KB
testcase_25 AC 849 ms
67,860 KB
testcase_26 AC 452 ms
67,856 KB
testcase_27 AC 456 ms
67,856 KB
testcase_28 AC 161 ms
5,376 KB
testcase_29 AC 162 ms
5,376 KB
testcase_30 AC 161 ms
5,376 KB
testcase_31 AC 155 ms
5,376 KB
testcase_32 AC 165 ms
5,376 KB
testcase_33 AC 161 ms
5,376 KB
testcase_34 AC 158 ms
5,376 KB
testcase_35 AC 161 ms
5,376 KB
testcase_36 AC 168 ms
5,376 KB
testcase_37 AC 159 ms
5,376 KB
testcase_38 AC 583 ms
35,344 KB
testcase_39 AC 275 ms
5,376 KB
testcase_40 AC 848 ms
66,512 KB
testcase_41 AC 392 ms
13,056 KB
testcase_42 AC 564 ms
34,260 KB
testcase_43 AC 466 ms
20,912 KB
testcase_44 AC 707 ms
44,616 KB
testcase_45 AC 668 ms
41,344 KB
testcase_46 AC 543 ms
25,568 KB
testcase_47 AC 633 ms
38,332 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <stack>
#include <array>
#include <list>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;

int main()
{
    int n; cin>>n;
    ll a[300030];
    for(int i=0; i<n; i++) cin>>a[i];
    int sz=1;
    while(sz<n) sz<<=1;
    vector<vector<int>> seg(sz*2);
    for(int i=0; i<n; i++){
        int k=sz+i;
		seg[k].push_back(a[i]);
		while(k>1){
			k>>=1;
			seg[k].push_back(a[i]);
		}
    }
    for(int i=0; i<2*sz; i++){
        sort(seg[i].begin(), seg[i].end());
    }
    int q; cin>>q;
    while(q--){
        int l, r, x; cin>>l>>r>>x;
        l--;
        int l1=l, r1=r;
        l+=sz, r+=sz;
        int ans=1e9+7;
        for(;l<r; l>>=1, r>>=1){
			if(r&1){
                r--;
                int k=lower_bound(seg[r].begin(), seg[r].end(), x)-seg[r].begin();
                if(k<seg[r].size()) ans=min(ans, seg[r][k]-x);
                if(k-1>=0) ans=min(ans, x-seg[r][k-1]);
            }
			if(l&1){
                int k=lower_bound(seg[l].begin(), seg[l].end(), x)-seg[l].begin();
                if(k<seg[l].size()) ans=min(ans, seg[l][k]-x);
                if(k-1>=0) ans=min(ans, x-seg[l][k-1]);
                l++;
            }
		}
        cout<<ans<<endl;
    }
    return 0;
}
0