結果
| 問題 |
No.1332 Range Nearest Query
|
| コンテスト | |
| ユーザー |
Nachia
|
| 提出日時 | 2021-01-12 21:43:33 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 422 ms / 2,500 ms |
| コード長 | 2,600 bytes |
| コンパイル時間 | 2,186 ms |
| コンパイル使用メモリ | 202,456 KB |
| 最終ジャッジ日時 | 2025-01-17 16:53:02 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 48 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:100:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
100 | scanf("%d",&N);
| ~~~~~^~~~~~~~~
main.cpp:101:17: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
101 | rep(i,N) scanf("%d",X+i);
| ~~~~~^~~~~~~~~~
main.cpp:105:15: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
105 | int Q; scanf("%d",&Q);
| ~~~~~^~~~~~~~~
main.cpp:107:21: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
107 | int l,r,x; scanf("%d%d%d",&l,&r,&x); l--;
| ~~~~~^~~~~~~~~~~~~~~~~~~
ソースコード
#include<bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
#define rep(i,n) for(int i=0; i<(n); i++)
template<
class S,
S(*op)(S a,S b),
S(*e)()
>
struct segtree {
int N;
vector<S> V;
segtree(int n=0){
N=1; while(N<n) N*=2;
V.assign(N*2,e());
}
segtree(const vector<S>& v)
: segtree(v.size()){
rep(i,v.size()) V[N+i]=v[i];
for(int i=N-1; i>=1; i--) V[i]=op(V[i*2],V[i*2+1]);
}
void set(int p,S v){
p+=N; V[p]=v;
while(p!=1){ p/=2; V[p]=op(V[p*2],V[p*2+1]); }
}
S get(int p){ return V[N+p]; }
S prod(int l,int r){
l+=N; r+=N;
S resl=e(), resr=e();
while(r-l){
if(l&1) resl=op(resl,V[l++]);
if(r&1) resr=op(V[--r],resr);
l/=2; r/=2;
}
return op(resl,resr);
}
template<class Cmp>
int max_right(int l,Cmp cmp){
S x=e(); l+=N; int r=N+N;
while(l<r){
if(l&1){
S tmp=op(x,V[l]);
if(cmp(tmp)) break; else{ l++; x=tmp; }
}
l/=2; r/=2;
}
if(l>=r) return N;
while(l<N){
S tmp=op(x,V[l*=2]);
if(!cmp(tmp)){ l++; x=tmp; }
}
return l-N;
}
template<class Cmp>
int min_left(int r,Cmp cmp){
S x=e(); r+=N; int l=N;
while(l<r && r!=1){
if(r&1){
S tmp=op(V[r-1],x);
if(cmp(tmp)) break; else{ r--; x=tmp; }
}
l/=2; r/=2;
}
if(l>=r) return 0;
while(r<=N){
S tmp=op(V[(r*=2)-1],x);
if(!cmp(tmp)){ r--; x=tmp; }
}
return r-N;
}
};
const int INF = 2000000000;
using RQS=int;
RQS RMQop(RQS l,RQS r) { return max(l,r); }
RQS RMQe(){ return -1; }
using RMQ=segtree<RQS,RMQop,RMQe>;
int N;
int X[300000];
pair<int,int> I[300000];
struct QueryNode{ int l,x,i; };
vector<QueryNode> queries[300000];
int ans[100000];
int binsearch_GV(int x){
int l=0, r=N+1;
while(r-l>1){
int m=(l+r)/2;
if(X[I[m-1].second]<x) l=m; else r=m;
}
return l;
}
int main(){
scanf("%d",&N);
rep(i,N) scanf("%d",X+i);
rep(i,N) I[i]={X[i],i}; sort(I,I+N);
rep(i,N) I[I[i].second].first=i;
RMQ G(N);
int Q; scanf("%d",&Q);
rep(i,Q){
int l,r,x; scanf("%d%d%d",&l,&r,&x); l--;
queries[r-1].push_back({l,x,i});
}
rep(i,N){
G.set(I[i].first,i);
for(auto& q:queries[i]){
int tmp=INF;
int m = binsearch_GV(q.x);
int ri = G.max_right(m,[&q](int x)->bool{return q.l<=x;}); if(ri<N) tmp=min(tmp,X[G.get(ri)]-q.x);
int li = G.min_left(m,[&q](int x)->bool{return q.l<=x;}); if(li>0) tmp=min(tmp,q.x-X[G.get(li-1)]);
ans[q.i]=tmp;
}
}
rep(i,Q) printf("%d\n",ans[i]);
return 0;
}
Nachia