結果

問題 No.1332 Range Nearest Query
ユーザー leaf_1415leaf_1415
提出日時 2021-01-08 22:09:44
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 924 ms / 2,500 ms
コード長 1,853 bytes
コンパイル時間 2,163 ms
コンパイル使用メモリ 89,732 KB
実行使用メモリ 88,848 KB
最終ジャッジ日時 2024-04-28 03:21:30
合計ジャッジ時間 27,913 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 25 ms
27,648 KB
testcase_01 AC 25 ms
27,648 KB
testcase_02 AC 26 ms
27,648 KB
testcase_03 AC 840 ms
81,036 KB
testcase_04 AC 853 ms
81,040 KB
testcase_05 AC 840 ms
81,036 KB
testcase_06 AC 405 ms
88,716 KB
testcase_07 AC 403 ms
88,720 KB
testcase_08 AC 402 ms
88,724 KB
testcase_09 AC 403 ms
88,720 KB
testcase_10 AC 403 ms
88,724 KB
testcase_11 AC 404 ms
88,724 KB
testcase_12 AC 400 ms
88,716 KB
testcase_13 AC 407 ms
88,720 KB
testcase_14 AC 405 ms
88,724 KB
testcase_15 AC 410 ms
88,716 KB
testcase_16 AC 914 ms
88,724 KB
testcase_17 AC 900 ms
88,720 KB
testcase_18 AC 911 ms
88,720 KB
testcase_19 AC 924 ms
88,716 KB
testcase_20 AC 910 ms
88,724 KB
testcase_21 AC 917 ms
88,716 KB
testcase_22 AC 916 ms
88,720 KB
testcase_23 AC 918 ms
88,848 KB
testcase_24 AC 907 ms
88,716 KB
testcase_25 AC 918 ms
88,720 KB
testcase_26 AC 341 ms
88,720 KB
testcase_27 AC 331 ms
88,748 KB
testcase_28 AC 68 ms
27,648 KB
testcase_29 AC 69 ms
27,776 KB
testcase_30 AC 71 ms
27,648 KB
testcase_31 AC 67 ms
27,648 KB
testcase_32 AC 72 ms
27,648 KB
testcase_33 AC 72 ms
27,648 KB
testcase_34 AC 68 ms
27,648 KB
testcase_35 AC 69 ms
27,648 KB
testcase_36 AC 69 ms
27,776 KB
testcase_37 AC 69 ms
27,648 KB
testcase_38 AC 615 ms
58,892 KB
testcase_39 AC 192 ms
29,952 KB
testcase_40 AC 898 ms
86,804 KB
testcase_41 AC 363 ms
39,176 KB
testcase_42 AC 584 ms
57,100 KB
testcase_43 AC 464 ms
46,476 KB
testcase_44 AC 769 ms
73,484 KB
testcase_45 AC 709 ms
68,364 KB
testcase_46 AC 551 ms
54,024 KB
testcase_47 AC 671 ms
63,628 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cassert>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <string>
#include <algorithm>
#include <utility>
#include <complex>
#define rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))
#define all(x) (x).begin(),(x).end()
#define inf 1e18
#define mod 1000000007

using namespace std;

typedef long long llint;
typedef long long ll;
typedef pair<llint, llint> P;

struct SegTree{
	int size;
	vector<vector<llint> > seg;
	
	SegTree(){}
	SegTree(int size){
		this->size = size;
		seg.resize(1<<(size+1));
	}
	
	void init()
	{
		for(int i = 0; i < (1<<(size+1)); i++) sort(all(seg[i]));
	}
	
	void update(int i, llint val)
	{
		i += (1 << size);
		seg[i].push_back(val);
		while(i > 1){
			i /= 2;
			seg[i].push_back(val);
		}
	}
	
	llint query(int a, int b, int k, int l, int r, llint x)
	{
		if(b < l || r < a) return inf;
		if(a <= l && r <= b){
			
			llint ret = inf;
			int p = lower_bound(all(seg[k]), x) - seg[k].begin();
			if(p < seg[k].size()) chmin(ret, abs(seg[k][p] - x));
			if(p > 0) chmin(ret, abs(seg[k][p-1] - x));
			return ret;
		}
		llint lval = query(a, b, k*2, l, (l+r)/2, x);
		llint rval = query(a, b, k*2+1, (l+r)/2+1, r, x);
		return min(lval, rval);
	}
	llint query(int a, int b, ll x)
	{
		return query(a, b, 1, 0, (1<<size)-1, x);
	}
};

ll n, Q;
ll x[300005];
SegTree seg(19);

int main(void)
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> n;
	rep(i, 1, n){
		cin >> x[i];
		seg.update(i, x[i]);
	}
	seg.init();
	
	cin >> Q;
	ll l, r, x;
	rep(i, 1, Q){
		cin >> l >> r >> x;
		cout << seg.query(l, r, x) << "\n";
	}
	flush(cout);
	
	return 0;
}
0