Submission #862897

# Submission time Handle Problem Language Result Execution time Memory
862897 2023-10-19T10:18:50 Z Trisanu_Das Abracadabra (CEOI22_abracadabra) C++17
100 / 100
545 ms 48192 KB
#include <bits/stdc++.h>
using namespace std;
using VI = vector<int>;
struct BIT {
  VI C;
  int n;
  BIT(int _n) : C(_n + 1), n(_n + 1) {}
  void add(int x, int v) {
    while (x < n) C[x] += v, x += x & -x;
  }
  int sum(int x) {
    int ret = 0;
    while (x) ret += C[x], x -= x & -x;
    return ret;
  }
  int kth(int x) {
    int pos = 0;
    for (int b = 1 << 17; b >= 1; b >>= 1)
      if (pos + b < n && C[pos + b] < x) pos += b, x -= C[pos];
    return pos + 1;
  }
};
int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  int N, q;
  cin >> N >> q;
  BIT S(N);
  VI A(N), P(N + 1), Ans(q);
  vector<vector<pair<int, int>>> v(N + 1);
  for (int i = 0; i < N; i++) cin >> A[i], P[A[i]] = i;
  for (int i = 0, t, x; i < q; i++) {
    cin >> t >> x, t = min(t, N);
    if (t == 0)
      Ans[i] = A[x - 1];
    else
      v[t].emplace_back(x - 1, i);
  }
  VI nxt(N), ds(N + 1);
  auto SetVal = [&](int x, int v) { S.add(x, v - ds[x]), ds[x] = v; };
  for (int i = 0; i < N;) {
    int j = i + 1;
    while (j < N && A[j] < A[i]) j++;
    SetVal(A[i], j - i), i = j;
  }
  VI v2;
  for (int i = N - 1; i >= 0; i--) {
    while (!v2.empty() && A[v2.back()] < A[i]) v2.pop_back();
    nxt[i] = !v2.empty() ? v2.back() : N, v2.push_back(i);
  }
  for (int i = 1; i <= N; i++) {
    int itf = S.kth(N / 2 + 1), its = ds[itf], sum = S.sum(itf - 1);
    if (sum < N / 2) {
      int l = P[itf], r = l + its;
      SetVal(A[l], N / 2 - sum);
      l += N / 2 - sum;
      for (int i = l; i < r; i = nxt[i]) SetVal(A[i], min(r, nxt[i]) - i);
    }
    for (auto p : v[i]) {
      int pos = p.first, idx = p.second, qos = S.kth(pos + 1),
          sum = S.sum(qos - 1);
      Ans[idx] = A[P[qos] + pos - sum];
    }
  }
  for (int x : Ans) cout << x << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 199 ms 26192 KB Output is correct
2 Correct 225 ms 24660 KB Output is correct
3 Correct 204 ms 23892 KB Output is correct
4 Correct 163 ms 21768 KB Output is correct
5 Correct 182 ms 25940 KB Output is correct
6 Correct 163 ms 25588 KB Output is correct
7 Correct 184 ms 26964 KB Output is correct
8 Correct 175 ms 24272 KB Output is correct
9 Correct 204 ms 22964 KB Output is correct
10 Correct 176 ms 23036 KB Output is correct
11 Correct 167 ms 23252 KB Output is correct
12 Correct 158 ms 19508 KB Output is correct
13 Correct 172 ms 21784 KB Output is correct
14 Correct 176 ms 25124 KB Output is correct
15 Correct 177 ms 22184 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 132 ms 13652 KB Output is correct
18 Correct 145 ms 17336 KB Output is correct
19 Correct 0 ms 452 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 263 ms 41612 KB Output is correct
2 Correct 249 ms 42600 KB Output is correct
3 Correct 202 ms 34844 KB Output is correct
4 Correct 188 ms 35788 KB Output is correct
5 Correct 191 ms 36432 KB Output is correct
6 Correct 198 ms 33328 KB Output is correct
7 Correct 226 ms 40480 KB Output is correct
8 Correct 227 ms 38660 KB Output is correct
9 Correct 193 ms 35548 KB Output is correct
10 Correct 240 ms 37676 KB Output is correct
11 Correct 170 ms 33360 KB Output is correct
12 Correct 179 ms 33584 KB Output is correct
13 Correct 208 ms 37052 KB Output is correct
14 Correct 178 ms 34792 KB Output is correct
15 Correct 219 ms 39100 KB Output is correct
16 Correct 23 ms 10324 KB Output is correct
17 Correct 161 ms 28624 KB Output is correct
18 Correct 155 ms 31672 KB Output is correct
19 Correct 52 ms 13772 KB Output is correct
20 Correct 72 ms 16476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 51 ms 9520 KB Output is correct
2 Correct 46 ms 9420 KB Output is correct
3 Correct 46 ms 9180 KB Output is correct
4 Correct 38 ms 8532 KB Output is correct
5 Correct 40 ms 9152 KB Output is correct
6 Correct 38 ms 8528 KB Output is correct
7 Correct 39 ms 8948 KB Output is correct
8 Correct 51 ms 8384 KB Output is correct
9 Correct 40 ms 8848 KB Output is correct
10 Correct 31 ms 8016 KB Output is correct
11 Correct 33 ms 8528 KB Output is correct
12 Correct 32 ms 8176 KB Output is correct
13 Correct 33 ms 8020 KB Output is correct
14 Correct 33 ms 8288 KB Output is correct
15 Correct 32 ms 8240 KB Output is correct
16 Correct 11 ms 5212 KB Output is correct
17 Correct 25 ms 7300 KB Output is correct
18 Correct 27 ms 7124 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 199 ms 26192 KB Output is correct
2 Correct 225 ms 24660 KB Output is correct
3 Correct 204 ms 23892 KB Output is correct
4 Correct 163 ms 21768 KB Output is correct
5 Correct 182 ms 25940 KB Output is correct
6 Correct 163 ms 25588 KB Output is correct
7 Correct 184 ms 26964 KB Output is correct
8 Correct 175 ms 24272 KB Output is correct
9 Correct 204 ms 22964 KB Output is correct
10 Correct 176 ms 23036 KB Output is correct
11 Correct 167 ms 23252 KB Output is correct
12 Correct 158 ms 19508 KB Output is correct
13 Correct 172 ms 21784 KB Output is correct
14 Correct 176 ms 25124 KB Output is correct
15 Correct 177 ms 22184 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 132 ms 13652 KB Output is correct
18 Correct 145 ms 17336 KB Output is correct
19 Correct 0 ms 452 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 263 ms 41612 KB Output is correct
22 Correct 249 ms 42600 KB Output is correct
23 Correct 202 ms 34844 KB Output is correct
24 Correct 188 ms 35788 KB Output is correct
25 Correct 191 ms 36432 KB Output is correct
26 Correct 198 ms 33328 KB Output is correct
27 Correct 226 ms 40480 KB Output is correct
28 Correct 227 ms 38660 KB Output is correct
29 Correct 193 ms 35548 KB Output is correct
30 Correct 240 ms 37676 KB Output is correct
31 Correct 170 ms 33360 KB Output is correct
32 Correct 179 ms 33584 KB Output is correct
33 Correct 208 ms 37052 KB Output is correct
34 Correct 178 ms 34792 KB Output is correct
35 Correct 219 ms 39100 KB Output is correct
36 Correct 23 ms 10324 KB Output is correct
37 Correct 161 ms 28624 KB Output is correct
38 Correct 155 ms 31672 KB Output is correct
39 Correct 52 ms 13772 KB Output is correct
40 Correct 72 ms 16476 KB Output is correct
41 Correct 51 ms 9520 KB Output is correct
42 Correct 46 ms 9420 KB Output is correct
43 Correct 46 ms 9180 KB Output is correct
44 Correct 38 ms 8532 KB Output is correct
45 Correct 40 ms 9152 KB Output is correct
46 Correct 38 ms 8528 KB Output is correct
47 Correct 39 ms 8948 KB Output is correct
48 Correct 51 ms 8384 KB Output is correct
49 Correct 40 ms 8848 KB Output is correct
50 Correct 31 ms 8016 KB Output is correct
51 Correct 33 ms 8528 KB Output is correct
52 Correct 32 ms 8176 KB Output is correct
53 Correct 33 ms 8020 KB Output is correct
54 Correct 33 ms 8288 KB Output is correct
55 Correct 32 ms 8240 KB Output is correct
56 Correct 11 ms 5212 KB Output is correct
57 Correct 25 ms 7300 KB Output is correct
58 Correct 27 ms 7124 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 0 ms 348 KB Output is correct
61 Correct 545 ms 48192 KB Output is correct
62 Correct 398 ms 46960 KB Output is correct
63 Correct 420 ms 45512 KB Output is correct
64 Correct 292 ms 44524 KB Output is correct
65 Correct 313 ms 46416 KB Output is correct
66 Correct 352 ms 44488 KB Output is correct
67 Correct 274 ms 44104 KB Output is correct
68 Correct 280 ms 42352 KB Output is correct
69 Correct 307 ms 44984 KB Output is correct
70 Correct 267 ms 40788 KB Output is correct
71 Correct 244 ms 42676 KB Output is correct
72 Correct 260 ms 41204 KB Output is correct
73 Correct 258 ms 41552 KB Output is correct
74 Correct 259 ms 43600 KB Output is correct
75 Correct 292 ms 42356 KB Output is correct
76 Correct 23 ms 10076 KB Output is correct
77 Correct 178 ms 28936 KB Output is correct
78 Correct 202 ms 31948 KB Output is correct
79 Correct 0 ms 344 KB Output is correct
80 Correct 0 ms 344 KB Output is correct