結果
問題 | No.1332 Range Nearest Query |
ユーザー | yuto1115 |
提出日時 | 2020-10-31 00:02:26 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 489 ms / 2,500 ms |
コード長 | 4,943 bytes |
コンパイル時間 | 2,171 ms |
コンパイル使用メモリ | 213,624 KB |
実行使用メモリ | 12,196 KB |
最終ジャッジ日時 | 2024-07-22 10:38:38 |
合計ジャッジ時間 | 20,767 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 476 ms
11,776 KB |
testcase_04 | AC | 448 ms
11,732 KB |
testcase_05 | AC | 451 ms
11,772 KB |
testcase_06 | AC | 358 ms
12,064 KB |
testcase_07 | AC | 366 ms
12,064 KB |
testcase_08 | AC | 359 ms
12,060 KB |
testcase_09 | AC | 368 ms
11,932 KB |
testcase_10 | AC | 357 ms
12,196 KB |
testcase_11 | AC | 357 ms
12,064 KB |
testcase_12 | AC | 355 ms
11,932 KB |
testcase_13 | AC | 359 ms
11,936 KB |
testcase_14 | AC | 366 ms
11,936 KB |
testcase_15 | AC | 371 ms
12,064 KB |
testcase_16 | AC | 483 ms
12,192 KB |
testcase_17 | AC | 487 ms
12,064 KB |
testcase_18 | AC | 489 ms
11,844 KB |
testcase_19 | AC | 473 ms
11,840 KB |
testcase_20 | AC | 475 ms
12,064 KB |
testcase_21 | AC | 482 ms
12,064 KB |
testcase_22 | AC | 470 ms
12,192 KB |
testcase_23 | AC | 483 ms
12,064 KB |
testcase_24 | AC | 483 ms
12,060 KB |
testcase_25 | AC | 467 ms
11,940 KB |
testcase_26 | AC | 150 ms
12,064 KB |
testcase_27 | AC | 274 ms
12,068 KB |
testcase_28 | AC | 62 ms
5,764 KB |
testcase_29 | AC | 63 ms
5,696 KB |
testcase_30 | AC | 61 ms
5,760 KB |
testcase_31 | AC | 53 ms
5,628 KB |
testcase_32 | AC | 62 ms
5,568 KB |
testcase_33 | AC | 63 ms
5,696 KB |
testcase_34 | AC | 54 ms
5,888 KB |
testcase_35 | AC | 61 ms
5,764 KB |
testcase_36 | AC | 59 ms
5,756 KB |
testcase_37 | AC | 62 ms
5,692 KB |
testcase_38 | AC | 383 ms
8,868 KB |
testcase_39 | AC | 295 ms
6,872 KB |
testcase_40 | AC | 465 ms
11,988 KB |
testcase_41 | AC | 320 ms
8,076 KB |
testcase_42 | AC | 381 ms
8,812 KB |
testcase_43 | AC | 346 ms
8,364 KB |
testcase_44 | AC | 423 ms
11,492 KB |
testcase_45 | AC | 417 ms
11,284 KB |
testcase_46 | AC | 358 ms
8,692 KB |
testcase_47 | AC | 396 ms
10,976 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++; } } for(int i : ans) cout << i << '\n'; }