結果

問題 No.1332 Range Nearest Query
ユーザー kotatsugamekotatsugame
提出日時 2021-01-08 23:29:12
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 514 ms / 2,500 ms
コード長 2,052 bytes
コンパイル時間 1,105 ms
コンパイル使用メモリ 91,292 KB
実行使用メモリ 21,428 KB
最終ジャッジ日時 2023-08-10 12:29:55
合計ジャッジ時間 23,311 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 472 ms
15,972 KB
testcase_04 AC 470 ms
15,688 KB
testcase_05 AC 473 ms
16,116 KB
testcase_06 AC 446 ms
19,792 KB
testcase_07 AC 445 ms
19,520 KB
testcase_08 AC 447 ms
19,792 KB
testcase_09 AC 443 ms
19,656 KB
testcase_10 AC 443 ms
19,888 KB
testcase_11 AC 449 ms
20,152 KB
testcase_12 AC 451 ms
21,420 KB
testcase_13 AC 445 ms
20,336 KB
testcase_14 AC 445 ms
20,076 KB
testcase_15 AC 446 ms
20,616 KB
testcase_16 AC 509 ms
20,668 KB
testcase_17 AC 513 ms
21,000 KB
testcase_18 AC 513 ms
21,428 KB
testcase_19 AC 503 ms
19,808 KB
testcase_20 AC 509 ms
19,448 KB
testcase_21 AC 508 ms
20,076 KB
testcase_22 AC 512 ms
20,668 KB
testcase_23 AC 513 ms
20,292 KB
testcase_24 AC 513 ms
20,140 KB
testcase_25 AC 510 ms
20,212 KB
testcase_26 AC 452 ms
20,844 KB
testcase_27 AC 412 ms
20,892 KB
testcase_28 AC 188 ms
5,052 KB
testcase_29 AC 188 ms
5,056 KB
testcase_30 AC 191 ms
5,344 KB
testcase_31 AC 187 ms
5,168 KB
testcase_32 AC 192 ms
5,236 KB
testcase_33 AC 188 ms
5,068 KB
testcase_34 AC 189 ms
5,100 KB
testcase_35 AC 189 ms
5,236 KB
testcase_36 AC 189 ms
5,388 KB
testcase_37 AC 191 ms
5,112 KB
testcase_38 AC 383 ms
11,792 KB
testcase_39 AC 259 ms
5,744 KB
testcase_40 AC 514 ms
20,644 KB
testcase_41 AC 300 ms
9,616 KB
testcase_42 AC 371 ms
11,972 KB
testcase_43 AC 331 ms
10,636 KB
testcase_44 AC 452 ms
16,552 KB
testcase_45 AC 420 ms
15,740 KB
testcase_46 AC 361 ms
9,976 KB
testcase_47 AC 403 ms
16,036 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
a.cpp:8:1: 警告: ISO C++ では型の無い ‘main’ の宣言を禁止しています [-Wreturn-type]

ソースコード

diff #

#line 1 "a.cpp"
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
#line 2 "/home/kotatsugame/library/datastructure/segtree.cpp"
#include<functional>
#include<limits>
template<typename T>
struct segtree{
	using F=function<T(T,T)>;
	const F calcfn,updatefn;
	int n;
	T defvalue;
	vector<T>dat;
	segtree(int n_=0,T defvalue_=numeric_limits<T>::max(),
		const F calcfn_=[](T a,T b){return a<b?a:b;},
		const F updatefn_=[](T a,T b){return b;}
	):defvalue(defvalue_),calcfn(calcfn_),updatefn(updatefn_)
	{
		n=1;
		while(n<n_)n<<=1;
		dat.assign(2*n-1,defvalue);
	}
	void copy(const vector<T>&v)
	{
		for(int i=0;i<v.size();i++)dat[i+n-1]=v[i];
		for(int i=n-2;i>=0;i--)dat[i]=calcfn(dat[i*2+1],dat[i*2+2]);
	}
	void update(int i,T a)
	{
		i+=n-1;
		dat[i]=updatefn(dat[i],a);
		while(i>0)
		{
			i=(i-1)/2;
			dat[i]=calcfn(dat[2*i+1],dat[2*i+2]);
		}
	}
	T query(int a,int b)//[a,b)
	{
		int L=(a<0?0:a>n?n:a)+n-1;
		int R=(b<0?0:b>n?n:b)+n-1;
		T lret=defvalue,rret=defvalue;
		for(;L<R;L>>=1,R>>=1)
		{
			if(!(L&1))lret=calcfn(lret,dat[L]);
			if(!(R&1))rret=calcfn(dat[--R],rret);
		}
		return calcfn(lret,rret);
	}
};
#line 6 "a.cpp"
int N;
int ans[1<<17];
main()
{
	cin>>N;
	vector<pair<int,pair<int,pair<int,int> > > >X;
	segtree<int>L(N,-1e9,[](int a,int b){return a<b?b:a;},[](int a,int b){return b;});
	segtree<int>R(N,2e9,[](int a,int b){return a<b?a:b;},[](int a,int b){return b;});
	for(int i=0;i<N;i++)
	{
		int x;cin>>x;
		X.push_back(make_pair(x,make_pair(i,make_pair(-1,-1))));
		R.update(i,x);
	}
	int Q;cin>>Q;
	for(int i=0;i<Q;i++)
	{
		int l,r,x;cin>>l>>r>>x;
		l--;
		X.push_back(make_pair(x,make_pair(i,make_pair(l,r))));
	}
	sort(X.begin(),X.end());
	for(int i=0;i<X.size();i++)
	{
		int x=X[i].first;
		int id=X[i].second.first;
		if(X[i].second.second.first<0)
		{
			R.update(id,2e9);
			L.update(id,x);
		}
		else
		{
			int il=X[i].second.second.first,ir=X[i].second.second.second;
			int l=L.query(il,ir),r=R.query(il,ir);
			ans[id]=min(x-l,r-x);
		}
	}
	for(int i=0;i<Q;i++)cout<<ans[i]<<endl;
}
0