結果

問題 No.1332 Range Nearest Query
ユーザー snow39snow39
提出日時 2021-01-08 22:37:33
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 826 ms / 2,500 ms
コード長 3,807 bytes
コンパイル時間 2,521 ms
コンパイル使用メモリ 119,952 KB
実行使用メモリ 51,604 KB
最終ジャッジ日時 2023-08-10 10:40:45
合計ジャッジ時間 26,423 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 731 ms
46,620 KB
testcase_04 AC 709 ms
46,456 KB
testcase_05 AC 731 ms
46,908 KB
testcase_06 AC 424 ms
49,216 KB
testcase_07 AC 420 ms
49,732 KB
testcase_08 AC 421 ms
49,476 KB
testcase_09 AC 423 ms
49,936 KB
testcase_10 AC 414 ms
49,224 KB
testcase_11 AC 414 ms
49,380 KB
testcase_12 AC 428 ms
49,004 KB
testcase_13 AC 429 ms
49,080 KB
testcase_14 AC 427 ms
49,628 KB
testcase_15 AC 423 ms
49,528 KB
testcase_16 AC 826 ms
50,412 KB
testcase_17 AC 785 ms
50,428 KB
testcase_18 AC 802 ms
50,808 KB
testcase_19 AC 803 ms
51,604 KB
testcase_20 AC 780 ms
50,476 KB
testcase_21 AC 771 ms
50,232 KB
testcase_22 AC 806 ms
50,540 KB
testcase_23 AC 806 ms
50,204 KB
testcase_24 AC 773 ms
50,396 KB
testcase_25 AC 825 ms
51,404 KB
testcase_26 AC 339 ms
41,612 KB
testcase_27 AC 334 ms
48,116 KB
testcase_28 AC 49 ms
6,996 KB
testcase_29 AC 50 ms
6,808 KB
testcase_30 AC 50 ms
7,080 KB
testcase_31 AC 45 ms
6,936 KB
testcase_32 AC 52 ms
6,916 KB
testcase_33 AC 51 ms
6,908 KB
testcase_34 AC 47 ms
7,188 KB
testcase_35 AC 49 ms
6,864 KB
testcase_36 AC 47 ms
6,856 KB
testcase_37 AC 50 ms
6,892 KB
testcase_38 AC 495 ms
32,320 KB
testcase_39 AC 215 ms
15,552 KB
testcase_40 AC 768 ms
50,864 KB
testcase_41 AC 317 ms
21,328 KB
testcase_42 AC 470 ms
31,628 KB
testcase_43 AC 380 ms
25,840 KB
testcase_44 AC 622 ms
43,092 KB
testcase_45 AC 581 ms
39,872 KB
testcase_46 AC 428 ms
29,876 KB
testcase_47 AC 525 ms
37,252 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0