結果
問題 | No.1332 Range Nearest Query |
ユーザー | SSRS |
提出日時 | 2021-01-08 23:11:21 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 2,634 bytes |
コンパイル時間 | 2,166 ms |
コンパイル使用メモリ | 185,368 KB |
実行使用メモリ | 94,368 KB |
最終ジャッジ日時 | 2024-11-16 15:58:39 |
合計ジャッジ時間 | 122,301 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
10,496 KB |
testcase_01 | AC | 2 ms
10,496 KB |
testcase_02 | AC | 2 ms
10,496 KB |
testcase_03 | TLE | - |
testcase_04 | TLE | - |
testcase_05 | TLE | - |
testcase_06 | TLE | - |
testcase_07 | TLE | - |
testcase_08 | TLE | - |
testcase_09 | TLE | - |
testcase_10 | TLE | - |
testcase_11 | TLE | - |
testcase_12 | TLE | - |
testcase_13 | TLE | - |
testcase_14 | TLE | - |
testcase_15 | TLE | - |
testcase_16 | TLE | - |
testcase_17 | TLE | - |
testcase_18 | TLE | - |
testcase_19 | TLE | - |
testcase_20 | TLE | - |
testcase_21 | TLE | - |
testcase_22 | TLE | - |
testcase_23 | TLE | - |
testcase_24 | TLE | - |
testcase_25 | TLE | - |
testcase_26 | AC | 1,838 ms
48,452 KB |
testcase_27 | AC | 1,846 ms
71,508 KB |
testcase_28 | AC | 306 ms
13,988 KB |
testcase_29 | AC | 317 ms
53,816 KB |
testcase_30 | AC | 326 ms
14,612 KB |
testcase_31 | AC | 260 ms
35,740 KB |
testcase_32 | AC | 325 ms
14,352 KB |
testcase_33 | AC | 330 ms
45,964 KB |
testcase_34 | AC | 287 ms
14,080 KB |
testcase_35 | AC | 307 ms
42,368 KB |
testcase_36 | AC | 295 ms
13,696 KB |
testcase_37 | AC | 314 ms
8,892 KB |
testcase_38 | TLE | - |
testcase_39 | AC | 1,271 ms
11,484 KB |
testcase_40 | TLE | - |
testcase_41 | AC | 2,101 ms
16,384 KB |
testcase_42 | TLE | - |
testcase_43 | TLE | - |
testcase_44 | TLE | - |
testcase_45 | TLE | - |
testcase_46 | TLE | - |
testcase_47 | TLE | - |
ソースコード
#include <bits/stdc++.h> using namespace std; const int INF = 1000000000; template <typename T> struct binary_indexed_tree{ int N; vector<T> BIT; binary_indexed_tree(int n){ N = 1; while (N < n){ N *= 2; } BIT = vector<T>(N + 1, 0); } void add(int i, T x){ i++; while (i <= N){ BIT[i] += x; i += i & -i; } } T sum(int i){ T ans = 0; while (i > 0){ ans += BIT[i]; i -= i & -i; } return ans; } T sum(int L, int R){ return sum(R) - sum(L); } T all(){ return BIT[N]; } }; int main(){ int N; cin >> N; vector<int> X(N); for (int i = 0; i < N; i++){ cin >> X[i]; } vector<int> X2 = X; sort(X2.begin(), X2.end()); int Q; cin >> Q; vector<int> l(Q), r(Q), x(Q); for (int i = 0; i < Q; i++){ cin >> l[i] >> r[i] >> x[i]; l[i]--; } vector<int> tv(Q, INF), fv(Q, -1); while (true){ vector<int> mid(Q, -1); bool update = false; for (int i = 0; i < Q; i++){ if (tv[i] - fv[i] > 1){ mid[i] = (tv[i] + fv[i] + 2) / 2 - 1; update = true; } } if (!update){ break; } vector<vector<int>> padd(N); for (int i = 0; i < N; i++){ int p = lower_bound(X2.begin(), X2.end(), X[i]) - X2.begin(); padd[i].push_back(p); } vector<vector<tuple<int, int, int>>> add(N); vector<vector<tuple<int, int, int>>> sub(N); for (int i = 0; i < Q; i++){ if (mid[i] != -1){ int L = x[i] - mid[i]; int R = x[i] + mid[i]; int Lp = lower_bound(X2.begin(), X2.end(), L) - X2.begin(); int Rp = upper_bound(X2.begin(), X2.end(), R) - X2.begin(); if (l[i] > 0){ sub[l[i] - 1].push_back(make_tuple(Lp, Rp, i)); } add[r[i] - 1].push_back(make_tuple(Lp, Rp, i)); } } binary_indexed_tree<int> BIT(N); vector<int> cnt(Q, 0); for (int i = 0; i < N; i++){ for (int j : padd[i]){ BIT.add(j, 1); } int addcnt = add[i].size(); for (int j = 0; j < addcnt; j++){ int L = get<0>(add[i][j]); int R = get<1>(add[i][j]); int id = get<2>(add[i][j]); cnt[id] += BIT.sum(L, R); } int subcnt = sub[i].size(); for (int j = 0; j < subcnt; j++){ int L = get<0>(sub[i][j]); int R = get<1>(sub[i][j]); int id = get<2>(sub[i][j]); cnt[id] -= BIT.sum(L, R); } } for (int i = 0; i < Q; i++){ if (mid[i] != -1){ if (cnt[i] == 0){ fv[i] = mid[i]; } else { tv[i] = mid[i]; } } } } for (int i = 0; i < Q; i++){ cout << tv[i] << endl; } }