結果

問題 No.1332 Range Nearest Query
ユーザー kk
提出日時 2021-04-21 01:14:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 838 ms / 2,500 ms
コード長 1,258 bytes
コンパイル時間 2,267 ms
コンパイル使用メモリ 202,192 KB
実行使用メモリ 192,056 KB
最終ジャッジ日時 2023-09-17 09:18:56
合計ジャッジ時間 35,417 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 64 ms
144,016 KB
testcase_01 AC 64 ms
143,992 KB
testcase_02 AC 87 ms
143,956 KB
testcase_03 AC 752 ms
177,096 KB
testcase_04 AC 746 ms
177,016 KB
testcase_05 AC 751 ms
177,000 KB
testcase_06 AC 559 ms
191,852 KB
testcase_07 AC 571 ms
191,944 KB
testcase_08 AC 569 ms
191,792 KB
testcase_09 AC 559 ms
191,912 KB
testcase_10 AC 553 ms
191,952 KB
testcase_11 AC 550 ms
191,908 KB
testcase_12 AC 565 ms
191,940 KB
testcase_13 AC 568 ms
191,812 KB
testcase_14 AC 564 ms
191,912 KB
testcase_15 AC 564 ms
191,944 KB
testcase_16 AC 823 ms
191,948 KB
testcase_17 AC 828 ms
191,884 KB
testcase_18 AC 838 ms
191,792 KB
testcase_19 AC 834 ms
191,904 KB
testcase_20 AC 831 ms
191,940 KB
testcase_21 AC 818 ms
192,056 KB
testcase_22 AC 825 ms
191,880 KB
testcase_23 AC 830 ms
191,916 KB
testcase_24 AC 826 ms
191,884 KB
testcase_25 AC 824 ms
191,880 KB
testcase_26 AC 417 ms
191,872 KB
testcase_27 AC 416 ms
191,804 KB
testcase_28 AC 221 ms
144,176 KB
testcase_29 AC 216 ms
143,964 KB
testcase_30 AC 221 ms
143,964 KB
testcase_31 AC 217 ms
144,004 KB
testcase_32 AC 229 ms
144,028 KB
testcase_33 AC 218 ms
144,008 KB
testcase_34 AC 214 ms
143,944 KB
testcase_35 AC 215 ms
144,236 KB
testcase_36 AC 218 ms
144,080 KB
testcase_37 AC 218 ms
144,012 KB
testcase_38 AC 640 ms
167,488 KB
testcase_39 AC 367 ms
145,380 KB
testcase_40 AC 795 ms
190,900 KB
testcase_41 AC 502 ms
150,720 KB
testcase_42 AC 630 ms
166,624 KB
testcase_43 AC 558 ms
156,664 KB
testcase_44 AC 726 ms
174,192 KB
testcase_45 AC 686 ms
171,908 KB
testcase_46 AC 596 ms
159,532 KB
testcase_47 AC 664 ms
169,688 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

const int INF = 1<<30;
vector<int> t[6000000];

void build(const vector<int> &x, int k, int l, int r) {
  if (l < r) {
    if (l + 1 == r) {
      t[k].push_back(x[l]);
    } else {
      int m = (l + r) / 2;
      build(x, 2 * k + 1, l, m);
      build(x, 2 * k + 2, m, r);
      merge(t[2*k+1].begin(), t[2*k+1].end(),
            t[2*k+2].begin(), t[2*k+2].end(), back_inserter(t[k]));
    }
  }
}

int query(int k, int l, int r, int a, int b, int y) {
  if (b <= l || r <= a) return INF;
  if (a <= l && r <= b) {
    int ret = INF;
    int pos = lower_bound(t[k].begin(), t[k].end(), y) - t[k].begin();
    if (pos < t[k].size())
      ret = min(ret, abs(t[k][pos] - y));
    if (--pos >= 0)
      ret = min(ret, abs(t[k][pos] - y));
    return ret;
  }
  int mi = (l + r) / 2;
  return min(query(2 * k + 1, l, mi, a, b, y), query(2 * k + 2, mi, r, a, b, y));
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int n;
  cin >> n;
  vector<int> x(n);
  for (int i = 0; i < n; i++)
    cin >> x[i];

  build(x, 0, 0, n);
  
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int l, r, y;
    cin >> l >> r >> y;
    --l;
    cout << query(0, 0, n, l, r, y) << endl;
  }
  
  return 0;
}
0