結果
問題 | No.1332 Range Nearest Query |
ユーザー | yuto1115 |
提出日時 | 2021-01-08 19:22:12 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 590 ms / 2,500 ms |
コード長 | 4,956 bytes |
コンパイル時間 | 2,749 ms |
コンパイル使用メモリ | 213,464 KB |
実行使用メモリ | 12,104 KB |
最終ジャッジ日時 | 2024-11-16 01:38:44 |
合計ジャッジ時間 | 24,273 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 2 ms
5,248 KB |
testcase_03 | AC | 550 ms
11,776 KB |
testcase_04 | AC | 545 ms
11,716 KB |
testcase_05 | AC | 544 ms
11,772 KB |
testcase_06 | AC | 401 ms
12,104 KB |
testcase_07 | AC | 405 ms
12,064 KB |
testcase_08 | AC | 403 ms
12,064 KB |
testcase_09 | AC | 402 ms
12,068 KB |
testcase_10 | AC | 404 ms
12,068 KB |
testcase_11 | AC | 405 ms
12,068 KB |
testcase_12 | AC | 404 ms
12,064 KB |
testcase_13 | AC | 405 ms
11,976 KB |
testcase_14 | AC | 402 ms
12,060 KB |
testcase_15 | AC | 403 ms
11,936 KB |
testcase_16 | AC | 571 ms
11,936 KB |
testcase_17 | AC | 565 ms
11,872 KB |
testcase_18 | AC | 560 ms
11,936 KB |
testcase_19 | AC | 570 ms
12,008 KB |
testcase_20 | AC | 568 ms
12,036 KB |
testcase_21 | AC | 576 ms
12,068 KB |
testcase_22 | AC | 566 ms
11,964 KB |
testcase_23 | AC | 573 ms
12,004 KB |
testcase_24 | AC | 563 ms
12,064 KB |
testcase_25 | AC | 590 ms
12,064 KB |
testcase_26 | AC | 174 ms
12,060 KB |
testcase_27 | AC | 300 ms
11,980 KB |
testcase_28 | AC | 68 ms
5,888 KB |
testcase_29 | AC | 71 ms
5,824 KB |
testcase_30 | AC | 73 ms
5,760 KB |
testcase_31 | AC | 59 ms
5,892 KB |
testcase_32 | AC | 75 ms
5,824 KB |
testcase_33 | AC | 74 ms
5,568 KB |
testcase_34 | AC | 63 ms
5,764 KB |
testcase_35 | AC | 67 ms
5,768 KB |
testcase_36 | AC | 67 ms
5,888 KB |
testcase_37 | AC | 72 ms
5,696 KB |
testcase_38 | AC | 442 ms
8,872 KB |
testcase_39 | AC | 341 ms
6,872 KB |
testcase_40 | AC | 561 ms
11,988 KB |
testcase_41 | AC | 373 ms
8,080 KB |
testcase_42 | AC | 448 ms
8,808 KB |
testcase_43 | AC | 402 ms
8,368 KB |
testcase_44 | AC | 512 ms
11,488 KB |
testcase_45 | AC | 483 ms
11,160 KB |
testcase_46 | AC | 431 ms
8,688 KB |
testcase_47 | AC | 463 ms
10,980 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" : ""); }