結果
| 問題 |
No.1332 Range Nearest Query
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-01-08 23:15:54 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,960 bytes |
| コンパイル時間 | 1,355 ms |
| コンパイル使用メモリ | 92,060 KB |
| 実行使用メモリ | 140,288 KB |
| 最終ジャッジ日時 | 2024-11-16 17:25:23 |
| 合計ジャッジ時間 | 119,332 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 16 TLE * 32 |
コンパイルメッセージ
a.cpp:7:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
ソースコード
#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;
}
}