結果
問題 | No.1332 Range Nearest Query |
ユーザー | SSRS |
提出日時 | 2021-01-08 23:16:52 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 2,779 bytes |
コンパイル時間 | 3,257 ms |
コンパイル使用メモリ | 202,152 KB |
実行使用メモリ | 94,388 KB |
最終ジャッジ日時 | 2024-11-16 17:36:37 |
合計ジャッジ時間 | 113,243 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
10,496 KB |
testcase_01 | AC | 2 ms
45,972 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,505 ms
48,604 KB |
testcase_27 | AC | 1,576 ms
71,812 KB |
testcase_28 | AC | 165 ms
13,844 KB |
testcase_29 | AC | 176 ms
45,140 KB |
testcase_30 | AC | 184 ms
14,500 KB |
testcase_31 | AC | 121 ms
35,860 KB |
testcase_32 | AC | 183 ms
9,000 KB |
testcase_33 | AC | 194 ms
9,256 KB |
testcase_34 | AC | 142 ms
8,852 KB |
testcase_35 | AC | 169 ms
8,700 KB |
testcase_36 | AC | 158 ms
8,208 KB |
testcase_37 | AC | 182 ms
8,652 KB |
testcase_38 | TLE | - |
testcase_39 | AC | 1,078 ms
11,416 KB |
testcase_40 | TLE | - |
testcase_41 | AC | 1,819 ms
16,512 KB |
testcase_42 | TLE | - |
testcase_43 | AC | 2,292 ms
20,864 KB |
testcase_44 | TLE | - |
testcase_45 | TLE | - |
testcase_46 | TLE | - |
testcase_47 | TLE | - |
ソースコード
#pragma GCC target("avx2") #pragma GCC optimize("unroll-loops") #pragma GCC optimize("Ofast") #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(){ ios::sync_with_stdio(false); cin.tie(nullptr); 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, -2); 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] != -2){ 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] != -2){ if (cnt[i] == 0){ fv[i] = mid[i]; } else { tv[i] = mid[i]; } } } } for (int i = 0; i < Q; i++){ cout << tv[i] << "\n"; } }