結果
問題 | No.1332 Range Nearest Query |
ユーザー | yuto1115 |
提出日時 | 2021-01-08 19:22:12 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 448 ms / 2,500 ms |
コード長 | 4,956 bytes |
コンパイル時間 | 1,962 ms |
コンパイル使用メモリ | 214,948 KB |
実行使用メモリ | 12,712 KB |
最終ジャッジ日時 | 2024-04-27 20:18:21 |
合計ジャッジ時間 | 19,607 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,812 KB |
testcase_01 | AC | 2 ms
6,940 KB |
testcase_02 | AC | 1 ms
6,940 KB |
testcase_03 | AC | 419 ms
11,756 KB |
testcase_04 | AC | 413 ms
11,768 KB |
testcase_05 | AC | 420 ms
11,640 KB |
testcase_06 | AC | 333 ms
11,880 KB |
testcase_07 | AC | 327 ms
12,052 KB |
testcase_08 | AC | 331 ms
12,148 KB |
testcase_09 | AC | 335 ms
11,936 KB |
testcase_10 | AC | 327 ms
12,048 KB |
testcase_11 | AC | 329 ms
11,876 KB |
testcase_12 | AC | 333 ms
12,052 KB |
testcase_13 | AC | 331 ms
12,020 KB |
testcase_14 | AC | 343 ms
12,008 KB |
testcase_15 | AC | 345 ms
12,712 KB |
testcase_16 | AC | 439 ms
12,020 KB |
testcase_17 | AC | 435 ms
11,880 KB |
testcase_18 | AC | 434 ms
11,996 KB |
testcase_19 | AC | 439 ms
12,044 KB |
testcase_20 | AC | 432 ms
12,028 KB |
testcase_21 | AC | 444 ms
12,020 KB |
testcase_22 | AC | 434 ms
12,036 KB |
testcase_23 | AC | 434 ms
12,004 KB |
testcase_24 | AC | 430 ms
12,048 KB |
testcase_25 | AC | 448 ms
12,032 KB |
testcase_26 | AC | 156 ms
12,064 KB |
testcase_27 | AC | 257 ms
12,060 KB |
testcase_28 | AC | 60 ms
6,944 KB |
testcase_29 | AC | 60 ms
6,944 KB |
testcase_30 | AC | 61 ms
6,940 KB |
testcase_31 | AC | 50 ms
6,940 KB |
testcase_32 | AC | 63 ms
6,944 KB |
testcase_33 | AC | 62 ms
6,944 KB |
testcase_34 | AC | 56 ms
6,940 KB |
testcase_35 | AC | 56 ms
6,944 KB |
testcase_36 | AC | 56 ms
6,944 KB |
testcase_37 | AC | 63 ms
6,940 KB |
testcase_38 | AC | 368 ms
8,740 KB |
testcase_39 | AC | 286 ms
6,940 KB |
testcase_40 | AC | 435 ms
12,120 KB |
testcase_41 | AC | 310 ms
8,072 KB |
testcase_42 | AC | 356 ms
8,680 KB |
testcase_43 | AC | 330 ms
8,364 KB |
testcase_44 | AC | 405 ms
11,492 KB |
testcase_45 | AC | 395 ms
11,240 KB |
testcase_46 | AC | 365 ms
8,692 KB |
testcase_47 | AC | 365 ms
11,100 KB |
ソースコード
//@formatter:off #include<bits/stdc++.h> #define rep(i,n) for (int i = 0; i < int(n); ++i) #define rrep(i,n) for (int i = int(n)-1; i >= 0; i--) #define rep2(i,s,n) for (int i = int(s); i < int(n); ++i) #define all(a) a.begin(),a.end() #define rall(a) a.rbegin(),a.rend() #define pb push_back #define eb emplace_back #define vi vector<int> #define vvi vector<vector<int>> #define vl vector<ll> #define vvl vector<vector<ll>> #define vd vector<double> #define vvd vector<vector<double>> #define vs vector<string> #define vc vector<char> #define vvc vector<vector<char>> #define vb vector<bool> #define vvb vector<vector<bool>> #define vp vector<P> #define vvp vector<vector<P>> using namespace std; using ll = long long; using P = pair<int,int>; using LP = pair<ll,ll>; template<class S,class T> istream& operator>>(istream &is,pair<S,T> &p) { return is >> p.first >> p.second; } template<class S,class T> ostream& operator<<(ostream &os,const pair<S,T> &p) { return os<<'{'<<p.first<<","<<p.second<<'}'; } template<class T> istream& operator>>(istream &is,vector<T> &v) { for(T &t:v){is>>t;} return is; } template<class T> ostream& operator<<(ostream &os,const vector<T> &v) { os<<'[';rep(i,v.size())os<<v[i]<<(i==int(v.size()-1)?"":","); return os<<']'; } void Yes(bool b) { cout << (b ? "Yes" : "No") << '\n'; } void YES(bool b) { cout << (b ? "YES" : "NO") << '\n'; } template<class T> bool chmin(T& a,T b) {if(a > b){a = b; return true;} return false;} template<class T> bool chmax(T& a,T b) {if(a < b){a = b; return true;} return false;} const int inf = 1001001001; const ll linf = 1001001001001001001; //@formatter:on template<class Monoid> class segtree { using T = typename Monoid::value_type; int n; vector<T> val; public: constexpr segtree(int _n) { n = 1; while (n < _n) n *= 2; val = vector<T>(2 * n, Monoid::identity); } constexpr segtree(vector<T> init) { int _n = init.size(); n = 1; while (n < _n) n *= 2; val = vector<T>(2 * n, Monoid::identity); rep(i, _n) val[i + n] = init[i]; rrep(i, n) val[i] = Monoid::operate(val[i * 2], val[i * 2 + 1]); } // segment [l,r) T fold(int l, int r) { l += n, r += n; T fold_l = Monoid::identity; T fold_r = Monoid::identity; while (l < r) { if (l & 1) fold_l = Monoid::operate(fold_l, val[l++]); if (r & 1) fold_r = Monoid::operate(val[--r], fold_r); l >>= 1, r >>= 1; } return Monoid::operate(fold_l, fold_r); } template<class F> void update(int i, const F &f) { i += n; val[i] = f(val[i]); while (i > 1) { i >>= 1; val[i] = Monoid::operate(val[i * 2], val[i * 2 + 1]); } } }; class Node { public: using value_type = int; value_type value; Node(value_type value) : value(value) {} static constexpr value_type identity = -1; static constexpr value_type operate(const value_type &l, const value_type &r) { return max(l,r); } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int n; cin >> n; vi X(n); cin >> X; vi pr = X; int q; cin >> q; vi l(q), r(q), x(q); rep(i, q) { cin >> l[i] >> r[i] >> x[i]; l[i]--; r[i]--; pr.pb(x[i]); } sort(all(pr)); pr.erase(unique(all(pr)),pr.end()); int sz = pr.size(); vi ord(q); iota(all(ord), 0); sort(all(ord), [&](int i,int j){ return r[i] < r[j]; }); segtree<Node> st(sz); vi ans(q,inf); int now = 0; rep(i,n) { X[i] = lower_bound(all(pr), X[i]) - pr.begin(); st.update(X[i], [&](auto) { return i; }); while(now < q and r[ord[now]] == i) { int nl = l[ord[now]]; int nr = r[ord[now]]; int nx = x[ord[now]]; nx = lower_bound(all(pr),nx)-pr.begin(); if(st.fold(nx,sz) >= nl) { int ok = sz, ng = nx; auto f = [&](int t) -> bool { return st.fold(nx,t) >= nl; }; while(abs(ok-ng) > 1) { int mid = (ng+ok)/2; if(f(mid)) ok = mid; else ng = mid; } chmin(ans[ord[now]],pr[ok-1]-pr[nx]); } if(st.fold(0,nx+1) >= nl) { int ok = 0, ng = nx+1; auto f = [&](int t) -> bool { return st.fold(t,nx+1) >= nl; }; while(abs(ok-ng) > 1) { int mid = (ng+ok)/2; if(f(mid)) ok = mid; else ng = mid; } chmin(ans[ord[now]],pr[nx]-pr[ok]); } now++; } } rep(i,q) cout << ans[i] << (i < q-1 ? "\n" : ""); }