結果
問題 | No.1332 Range Nearest Query |
ユーザー | snow39 |
提出日時 | 2021-01-08 22:37:33 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 695 ms / 2,500 ms |
コード長 | 3,807 bytes |
コンパイル時間 | 1,687 ms |
コンパイル使用メモリ | 122,112 KB |
実行使用メモリ | 50,612 KB |
最終ジャッジ日時 | 2024-04-28 04:05:49 |
合計ジャッジ時間 | 22,596 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,812 KB |
testcase_01 | AC | 2 ms
6,940 KB |
testcase_02 | AC | 1 ms
6,940 KB |
testcase_03 | AC | 583 ms
46,508 KB |
testcase_04 | AC | 570 ms
46,452 KB |
testcase_05 | AC | 582 ms
46,420 KB |
testcase_06 | AC | 380 ms
49,220 KB |
testcase_07 | AC | 373 ms
49,228 KB |
testcase_08 | AC | 363 ms
49,096 KB |
testcase_09 | AC | 377 ms
49,228 KB |
testcase_10 | AC | 373 ms
49,300 KB |
testcase_11 | AC | 370 ms
49,104 KB |
testcase_12 | AC | 380 ms
49,208 KB |
testcase_13 | AC | 373 ms
49,352 KB |
testcase_14 | AC | 380 ms
49,104 KB |
testcase_15 | AC | 369 ms
49,224 KB |
testcase_16 | AC | 657 ms
50,476 KB |
testcase_17 | AC | 646 ms
50,544 KB |
testcase_18 | AC | 642 ms
50,512 KB |
testcase_19 | AC | 650 ms
50,608 KB |
testcase_20 | AC | 659 ms
50,472 KB |
testcase_21 | AC | 630 ms
50,484 KB |
testcase_22 | AC | 657 ms
50,476 KB |
testcase_23 | AC | 657 ms
50,612 KB |
testcase_24 | AC | 650 ms
50,608 KB |
testcase_25 | AC | 695 ms
50,452 KB |
testcase_26 | AC | 308 ms
41,556 KB |
testcase_27 | AC | 308 ms
48,468 KB |
testcase_28 | AC | 45 ms
7,016 KB |
testcase_29 | AC | 46 ms
6,900 KB |
testcase_30 | AC | 46 ms
7,080 KB |
testcase_31 | AC | 42 ms
6,944 KB |
testcase_32 | AC | 47 ms
7,132 KB |
testcase_33 | AC | 45 ms
7,124 KB |
testcase_34 | AC | 41 ms
7,344 KB |
testcase_35 | AC | 47 ms
7,020 KB |
testcase_36 | AC | 42 ms
7,016 KB |
testcase_37 | AC | 46 ms
7,020 KB |
testcase_38 | AC | 410 ms
32,552 KB |
testcase_39 | AC | 190 ms
15,820 KB |
testcase_40 | AC | 631 ms
49,332 KB |
testcase_41 | AC | 252 ms
21,640 KB |
testcase_42 | AC | 390 ms
31,544 KB |
testcase_43 | AC | 306 ms
25,760 KB |
testcase_44 | AC | 494 ms
42,412 KB |
testcase_45 | AC | 496 ms
39,684 KB |
testcase_46 | AC | 350 ms
29,980 KB |
testcase_47 | AC | 429 ms
37,172 KB |
ソースコード
#include <iostream> #include <algorithm> #include <string> #include <vector> #include <cmath> #include <map> #include <queue> #include <iomanip> #include <set> #include <tuple> #define mkp make_pair #define mkt make_tuple #define rep(i,n) for(int i = 0; i < (n); ++i) #define all(v) v.begin(),v.end() using namespace std; typedef long long ll; const ll MOD=1e9+7; template<class T> void chmin(T &a,const T &b){if(a>b) a=b;} template<class T> void chmax(T &a,const T &b){if(a<b) a=b;} #include <functional> #include <climits> //SegmentTree<int> seg(N,[](int a,int b){return min(a,b);},INT_MAX); template< typename T> class SegmentTree{ private: void resize(int size){ n=1; while(n<size) n*=2; tree.resize(2*n-1,def); } public: using F = function<T(T,T)>; int n; vector<T> tree; F operation; T def; SegmentTree(){} SegmentTree(int size,F _operation,T _def):operation(_operation),def(_def){ resize(size); } void embody(int size,F _operation,T _def){ operation=_operation; def=_def; resize(size); } void initialize(const vector<T> &v){ int size=v.size(); resize(size); for(int i=0;i<size;i++) tree[i+n-1]=v[i]; for(int i=n-2;i>=0;i--) tree[i]=operation(tree[2*i+1],tree[2*i+2]); } void update(int index,T value){ index+=n-1; tree[index]=value; while(index>0){ index=(index-1)/2; tree[index]=operation(tree[2*index+1],tree[2*index+2]); } } T query(int a,int b,int k=0,int l=0,int r=-1){//[a,b) if(r<0) r=n; if(r<=a||b<=l) return def; else if(a<=l&&r<=b) return tree[k]; else{ T lval=query(a,b,2*k+1,l,(l+r)/2); T rval=query(a,b,2*k+2,(l+r)/2,r); return operation(lval,rval); } } T get(int index){ return tree[index+n-1]; } int find(int x,int k=0,int l=0,int r=-1){// a[0]+...+a[i]>=x (i:minimal) if(r<0) r=n; if(tree[k]<x) return -1; if(r-l==1) return k-(n-1); if(tree[2*k+1]>=x) return find(x,2*k+1,l,(l+r)/2); else return find(x-tree[2*k+1],2*k+2,(l+r)/2,r); } //right -> change vl to vr (notice doesn't contain b. range [a,b)) int find_left(int a,int b,T x,int k=0,int l=0,int r=-1){// min(a[a],...,a[i])<=x (i:minimal) if(r<0) r=n; if(tree[k]>x||r<=a||b<=l) return -1; if(r-l==1) return k-(n-1); int vl=find_left(a,b,x,2*k+1,l,(l+r)/2); if(vl>=0) return vl; return find_left(a,b,x,2*k+2,(l+r)/2,r); } int find_right(int a,int b,T x,int k=0,int l=0,int r=-1){// min(a[i],...,a[b-1])<=x (i:minimal) if(r<0) r=n; if(tree[k]>x||r<=a||b<=l) return -1; if(r-l==1) return k-(n-1); int vr=find_right(a,b,x,2*k+2,(l+r)/2,r); if(vr>=0) return vr; return find_right(a,b,x,2*k+1,l,(l+r)/2); } }; const ll INF=1e18; int main(){ cin.tie(0); ios::sync_with_stdio(false); int N; cin>>N; vector<ll> X(N); rep(i,N) cin>>X[i]; int Q; cin>>Q; vector<int> L(Q),R(Q); vector<ll> Z(Q); rep(i,Q) cin>>L[i]>>R[i]>>Z[i]; rep(i,Q){ L[i]--;R[i]--; } vector<vector<int>> qrys(N); for(int i=0;i<Q;i++) qrys[L[i]].push_back(i); vector<ll> v=X; rep(i,Q) v.push_back(Z[i]); sort(all(v)); v.erase(unique(all(v)),v.end()); int V=v.size(); map<ll,int> mp; rep(i,V) mp[v[i]]=i; SegmentTree<int> seg(V,[](int a,int b){ return min(a,b); },N+1); seg.initialize(vector<int> (V,N+1)); vector<ll> ans(Q,INF); for(int left=N-1;left>=0;left--){ seg.update(mp[X[left]],left); for(auto i:qrys[left]){ int l=L[i]; int r=R[i]; int z=Z[i]; int lid=seg.find_right(0,mp[z]+1,r); int rid=seg.find_left(mp[z],V,r); if(lid!=-1) chmin(ans[i],abs(z-v[lid])); if(rid!=-1) chmin(ans[i],abs(z-v[rid])); } } rep(i,Q) cout<<ans[i]<<"\n"; return 0; }