Submission #646851

# Submission time Handle Problem Language Result Execution time Memory
646851 2022-09-30T21:44:19 Z chenwz Abracadabra (CEOI22_abracadabra) C++11
100 / 100
608 ms 34240 KB
#include <bits/stdc++.h>
#define sz(v) ((int)(v).size())
using namespace std;
typedef long long lint;
typedef pair<int, int> pi;
using VI = vector<int>;
const int NN = 2e5 + 4;
struct BIT {
  int C[NN];
  void add(int x, int v) {
    while (x < NN) 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 i = 17; i >= 0; i--)
      if (pos + (1 << i) < NN && C[pos + (1 << i)] < x)
        pos += (1 << i), x -= C[pos];
    return pos + 1;
  }
};
BIT S;
int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  int n, q;
  cin >> n >> q;
  VI a(n), rev(n + 1), ans(q);
  vector<vector<pi>> v(n + 1);
  for (int i = 0; i < n; i++) cin >> a[i], rev[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();
    if (!v2.empty())
      nxt[i] = v2.back();
    else
      nxt[i] = 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 = rev[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[rev[qos] + pos - sum];
    }
  }
  for (int x : ans) cout << x << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 241 ms 18372 KB Output is correct
2 Correct 263 ms 17216 KB Output is correct
3 Correct 255 ms 16784 KB Output is correct
4 Correct 202 ms 15536 KB Output is correct
5 Correct 216 ms 18872 KB Output is correct
6 Correct 202 ms 19548 KB Output is correct
7 Correct 222 ms 19896 KB Output is correct
8 Correct 210 ms 17828 KB Output is correct
9 Correct 200 ms 16624 KB Output is correct
10 Correct 220 ms 16596 KB Output is correct
11 Correct 217 ms 16888 KB Output is correct
12 Correct 195 ms 13992 KB Output is correct
13 Correct 212 ms 15764 KB Output is correct
14 Correct 214 ms 18380 KB Output is correct
15 Correct 218 ms 15760 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 155 ms 8140 KB Output is correct
18 Correct 189 ms 11688 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 303 ms 27148 KB Output is correct
2 Correct 295 ms 28740 KB Output is correct
3 Correct 240 ms 23760 KB Output is correct
4 Correct 227 ms 22920 KB Output is correct
5 Correct 241 ms 23460 KB Output is correct
6 Correct 232 ms 22608 KB Output is correct
7 Correct 281 ms 26956 KB Output is correct
8 Correct 278 ms 25856 KB Output is correct
9 Correct 244 ms 23272 KB Output is correct
10 Correct 273 ms 25964 KB Output is correct
11 Correct 217 ms 23216 KB Output is correct
12 Correct 225 ms 22596 KB Output is correct
13 Correct 271 ms 26024 KB Output is correct
14 Correct 227 ms 23324 KB Output is correct
15 Correct 272 ms 26544 KB Output is correct
16 Correct 26 ms 8868 KB Output is correct
17 Correct 196 ms 19348 KB Output is correct
18 Correct 219 ms 21844 KB Output is correct
19 Correct 62 ms 10724 KB Output is correct
20 Correct 85 ms 11924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 63 ms 7896 KB Output is correct
2 Correct 57 ms 7536 KB Output is correct
3 Correct 60 ms 7436 KB Output is correct
4 Correct 43 ms 6876 KB Output is correct
5 Correct 53 ms 7248 KB Output is correct
6 Correct 42 ms 6776 KB Output is correct
7 Correct 53 ms 7156 KB Output is correct
8 Correct 47 ms 6732 KB Output is correct
9 Correct 47 ms 7144 KB Output is correct
10 Correct 37 ms 6568 KB Output is correct
11 Correct 42 ms 6864 KB Output is correct
12 Correct 39 ms 6712 KB Output is correct
13 Correct 40 ms 6528 KB Output is correct
14 Correct 40 ms 6812 KB Output is correct
15 Correct 38 ms 6632 KB Output is correct
16 Correct 13 ms 4564 KB Output is correct
17 Correct 31 ms 5904 KB Output is correct
18 Correct 32 ms 5536 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 241 ms 18372 KB Output is correct
2 Correct 263 ms 17216 KB Output is correct
3 Correct 255 ms 16784 KB Output is correct
4 Correct 202 ms 15536 KB Output is correct
5 Correct 216 ms 18872 KB Output is correct
6 Correct 202 ms 19548 KB Output is correct
7 Correct 222 ms 19896 KB Output is correct
8 Correct 210 ms 17828 KB Output is correct
9 Correct 200 ms 16624 KB Output is correct
10 Correct 220 ms 16596 KB Output is correct
11 Correct 217 ms 16888 KB Output is correct
12 Correct 195 ms 13992 KB Output is correct
13 Correct 212 ms 15764 KB Output is correct
14 Correct 214 ms 18380 KB Output is correct
15 Correct 218 ms 15760 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 155 ms 8140 KB Output is correct
18 Correct 189 ms 11688 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 303 ms 27148 KB Output is correct
22 Correct 295 ms 28740 KB Output is correct
23 Correct 240 ms 23760 KB Output is correct
24 Correct 227 ms 22920 KB Output is correct
25 Correct 241 ms 23460 KB Output is correct
26 Correct 232 ms 22608 KB Output is correct
27 Correct 281 ms 26956 KB Output is correct
28 Correct 278 ms 25856 KB Output is correct
29 Correct 244 ms 23272 KB Output is correct
30 Correct 273 ms 25964 KB Output is correct
31 Correct 217 ms 23216 KB Output is correct
32 Correct 225 ms 22596 KB Output is correct
33 Correct 271 ms 26024 KB Output is correct
34 Correct 227 ms 23324 KB Output is correct
35 Correct 272 ms 26544 KB Output is correct
36 Correct 26 ms 8868 KB Output is correct
37 Correct 196 ms 19348 KB Output is correct
38 Correct 219 ms 21844 KB Output is correct
39 Correct 62 ms 10724 KB Output is correct
40 Correct 85 ms 11924 KB Output is correct
41 Correct 63 ms 7896 KB Output is correct
42 Correct 57 ms 7536 KB Output is correct
43 Correct 60 ms 7436 KB Output is correct
44 Correct 43 ms 6876 KB Output is correct
45 Correct 53 ms 7248 KB Output is correct
46 Correct 42 ms 6776 KB Output is correct
47 Correct 53 ms 7156 KB Output is correct
48 Correct 47 ms 6732 KB Output is correct
49 Correct 47 ms 7144 KB Output is correct
50 Correct 37 ms 6568 KB Output is correct
51 Correct 42 ms 6864 KB Output is correct
52 Correct 39 ms 6712 KB Output is correct
53 Correct 40 ms 6528 KB Output is correct
54 Correct 40 ms 6812 KB Output is correct
55 Correct 38 ms 6632 KB Output is correct
56 Correct 13 ms 4564 KB Output is correct
57 Correct 31 ms 5904 KB Output is correct
58 Correct 32 ms 5536 KB Output is correct
59 Correct 0 ms 340 KB Output is correct
60 Correct 0 ms 340 KB Output is correct
61 Correct 608 ms 34240 KB Output is correct
62 Correct 497 ms 33564 KB Output is correct
63 Correct 513 ms 32464 KB Output is correct
64 Correct 378 ms 31708 KB Output is correct
65 Correct 405 ms 33124 KB Output is correct
66 Correct 387 ms 31608 KB Output is correct
67 Correct 343 ms 31368 KB Output is correct
68 Correct 351 ms 29860 KB Output is correct
69 Correct 370 ms 32100 KB Output is correct
70 Correct 341 ms 28768 KB Output is correct
71 Correct 297 ms 31616 KB Output is correct
72 Correct 327 ms 29324 KB Output is correct
73 Correct 327 ms 30280 KB Output is correct
74 Correct 329 ms 31568 KB Output is correct
75 Correct 321 ms 30332 KB Output is correct
76 Correct 26 ms 8780 KB Output is correct
77 Correct 218 ms 19688 KB Output is correct
78 Correct 242 ms 21752 KB Output is correct
79 Correct 0 ms 340 KB Output is correct
80 Correct 0 ms 340 KB Output is correct