結果

問題 No.1332 Range Nearest Query
ユーザー kotatsugamekotatsugame
提出日時 2021-01-08 23:15:54
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,960 bytes
コンパイル時間 1,335 ms
コンパイル使用メモリ 92,164 KB
実行使用メモリ 56,832 KB
最終ジャッジ日時 2024-04-28 05:23:28
合計ジャッジ時間 7,299 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 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 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
a.cpp:7:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]

ソースコード

diff #

#line 1 "a.cpp"
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
#line 3 "/home/kotatsugame/library/datastructure/rangefreq.cpp"
template<typename T>
struct rangefreq{
	int n;
	vector<vector<T> >dat;
	rangefreq(const vector<T>&v={})
	{
		n=1;
		while(n<v.size())n<<=1;
		dat.assign(2*n-1,{});
		for(int i=0;i<v.size();i++)dat[i+n-1].push_back(v[i]);
		for(int i=n-2;i>=0;i--)
		{
			dat[i].resize(dat[i*2+1].size()+dat[i*2+2].size());
			merge(dat[i*2+1].begin(),dat[i*2+1].end(),
				dat[i*2+2].begin(),dat[i*2+2].end(),
				dat[i].begin()
			);
		}
	}
	int query(int a,int b,T x,int k=0,int l=0,int r=-1)const//[a,b) count(*<x)
	{
		if(r<0)r=n;
		if(b<=l||r<=a)return 0;
		else if(a<=l&&r<=b)return lower_bound(dat[k].begin(),dat[k].end(),x)-dat[k].begin();
		else return query(a,b,x,k*2+1,l,(l+r)/2)+query(a,b,x,k*2+2,(l+r)/2,r);
	}
};
#line 6 "a.cpp"
int N;
main()
{
	cin>>N;
	vector<pair<int,int> >Xi(N);
	for(int i=0;i<N;i++)
	{
		int x;
		cin>>x;
		Xi[i]=make_pair(x,i);
	}
	sort(Xi.begin(),Xi.end());
	vector<int>id(N),X(N);
	for(int i=0;i<N;i++)
	{
		id[i]=Xi[i].second;
		X[i]=Xi[i].first;
	}
	rangefreq<int>P(id);
	int Q;cin>>Q;
	for(;Q--;)
	{
		int l,r,x;cin>>l>>r>>x;l--;
		int ans=1e9;
		{
			auto it=upper_bound(X.begin(),X.end(),x);
			if(it!=X.begin())
			{
				int L=0,R=it-X.begin();
				int pos=R;
				int cnt=P.query(0,pos,r)-P.query(0,pos,l);
				if(cnt>0)
				{
					while(R-L>1)
					{
						int mid=(L+R)/2;
						if(P.query(mid,pos,r)-P.query(mid,pos,l)>0)L=mid;
						else R=mid;
					}
					ans=min(ans,x-X[L]);
				}
			}
		}
		{
			auto it=lower_bound(X.begin(),X.end(),x);
			if(it!=X.end())
			{
				int L=it-X.begin(),R=N;
				int pos=L;
				int cnt=P.query(pos,N,r)-P.query(pos,N,l);
				if(cnt>0)
				{
					while(R-L>1)
					{
						int mid=(L+R)/2;
						if(P.query(pos,mid,r)-P.query(pos,mid,l)>0)R=mid;
						else L=mid;
					}
					ans=min(ans,X[L]-x);
				}
			}
		}
		cout<<ans<<endl;
	}
}
0