結果

問題 No.1332 Range Nearest Query
ユーザー 沙耶花沙耶花
提出日時 2021-01-08 22:07:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,146 bytes
コンパイル時間 2,574 ms
コンパイル使用メモリ 220,800 KB
実行使用メモリ 13,888 KB
最終ジャッジ日時 2024-04-28 03:14:22
合計ジャッジ時間 7,966 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
13,760 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf 1000000000000

int main(){

	int N;
	scanf("%d",&N);
	
	vector<long long> X(N);
	rep(i,N){
		scanf("%lld",&X[i]);
	}
	
	int Q;
	scanf("%d",&Q);
	
	vector<int> l(Q),r(Q);
	vector<long long> x(Q);
	
	int sz = 550;
	
	vector q(sz,vector<pair<int,int>>());
	
	rep(i,Q){
		scanf("%d %d %lld",&l[i],&r[i],&x[i]);
		l[i]--;
		r[i]--;
		
		q[l[i]/sz].emplace_back(r[i],i);
	}
	
	vector<int> ans(Q,1000000005);
	
	rep(i,sz){
		if(q[i].size()==0)continue;
		sort(q[i].begin(),q[i].end());
		
		int ll = 0,rr = 0;
		set<int> S;
		
		rep(j,q[i].size()){
			int ind = q[i][j].second;
			int R = q[i][j].first;
			int L = l[ind];
			
			while(rr<=R){
				S.insert(X[rr]);
				rr++;
			}
			
			while(L>ll){
				S.erase(X[ll]);
				ll++;
			}
			
			while(L<ll){
				ll--;
				S.insert(X[ll]);
			}
			
			long long xx = x[ind];
			auto it = S.upper_bound(xx);
			if(it!=S.end())ans[ind] = abs((*it)-xx);
			if(it!=S.begin()){
				it--;
				ans[ind] = abs((*it)-xx);
			}
		}
	}
	
	rep(i,Q){
		printf("%d\n",ans[i]);
	}
	
    return 0;
}
0