結果

問題 No.1332 Range Nearest Query
ユーザー chocoruskchocorusk
提出日時 2021-01-08 21:38:09
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 974 ms / 2,500 ms
コード長 1,649 bytes
コンパイル時間 1,343 ms
コンパイル使用メモリ 134,500 KB
実行使用メモリ 67,756 KB
最終ジャッジ日時 2023-08-10 08:58:58
合計ジャッジ時間 33,493 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 886 ms
49,268 KB
testcase_04 AC 852 ms
49,344 KB
testcase_05 AC 856 ms
49,192 KB
testcase_06 AC 558 ms
67,660 KB
testcase_07 AC 538 ms
67,692 KB
testcase_08 AC 564 ms
67,660 KB
testcase_09 AC 553 ms
67,548 KB
testcase_10 AC 538 ms
67,580 KB
testcase_11 AC 546 ms
67,636 KB
testcase_12 AC 556 ms
67,580 KB
testcase_13 AC 561 ms
67,744 KB
testcase_14 AC 544 ms
67,728 KB
testcase_15 AC 540 ms
67,620 KB
testcase_16 AC 957 ms
67,756 KB
testcase_17 AC 957 ms
67,568 KB
testcase_18 AC 949 ms
67,584 KB
testcase_19 AC 970 ms
67,604 KB
testcase_20 AC 967 ms
67,756 KB
testcase_21 AC 958 ms
67,628 KB
testcase_22 AC 974 ms
67,708 KB
testcase_23 AC 969 ms
67,708 KB
testcase_24 AC 970 ms
67,756 KB
testcase_25 AC 964 ms
67,580 KB
testcase_26 AC 498 ms
67,712 KB
testcase_27 AC 491 ms
67,604 KB
testcase_28 AC 186 ms
4,388 KB
testcase_29 AC 188 ms
4,384 KB
testcase_30 AC 179 ms
4,380 KB
testcase_31 AC 169 ms
4,384 KB
testcase_32 AC 173 ms
4,380 KB
testcase_33 AC 171 ms
4,380 KB
testcase_34 AC 178 ms
4,380 KB
testcase_35 AC 180 ms
4,384 KB
testcase_36 AC 176 ms
4,380 KB
testcase_37 AC 175 ms
4,384 KB
testcase_38 AC 668 ms
36,176 KB
testcase_39 AC 297 ms
4,980 KB
testcase_40 AC 945 ms
66,320 KB
testcase_41 AC 428 ms
13,184 KB
testcase_42 AC 638 ms
34,164 KB
testcase_43 AC 522 ms
20,584 KB
testcase_44 AC 789 ms
44,416 KB
testcase_45 AC 754 ms
41,032 KB
testcase_46 AC 605 ms
25,408 KB
testcase_47 AC 705 ms
38,948 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