Submission #971688

# Submission time Handle Problem Language Result Execution time Memory
971688 2024-04-29T07:49:19 Z nguyentunglam Abracadabra (CEOI22_abracadabra) C++17
100 / 100
651 ms 77524 KB
#include<bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define endl "\n"
using namespace std;

const int N = 1e6 + 10;

int n, q;

int a[N], ans[N], nxt[N], rev_a[N], ed[N];

vector<pair<int, int> > query[N];

int T[N << 2];

void up(int s, int l, int r, int pos, int val) {
  if (l == r) {
    T[s] = val;
    return;
  }
  int mid = l + r >> 1;
  if (pos <= mid) up(s << 1, l, mid, pos, val);
  else up(s << 1 | 1, mid + 1, r, pos, val);
  T[s] = T[s << 1] + T[s << 1 | 1];
}

int get(int s, int l, int r, int from, int to) {
  if (l > to || r < from) return 0;
  if (from <= l && r <= to) return T[s];
  int mid = l + r >> 1;
  return get(s << 1, l, mid, from, to) + get(s << 1 | 1, mid + 1, r, from, to);
}

int walk_on_tree (int s, int l, int r, int val) {
  if (l == r) return l;
  int mid = l + r >> 1;
  if (T[s << 1] >= val) return walk_on_tree(s << 1, l, mid, val);
  return walk_on_tree(s << 1 | 1, mid + 1, r, val - T[s << 1]);
}

int32_t main() {
  #define task ""

  cin.tie(0) -> sync_with_stdio(0);

  if (fopen("task.inp", "r")) {
    freopen("task.inp", "r", stdin);
    freopen("task.out", "w", stdout);
  }

  if (fopen(task".inp", "r")) {
    freopen (task".inp", "r", stdin);
    freopen (task".out", "w", stdout);
  }

  cin >> n >> q;

  for(int i = 1; i <= n; i++) cin >> a[i], rev_a[a[i]] = i;

  for(int i = 1; i <= q; i++) {
    int t, x; cin >> t >> x;
    query[min(n, t)].emplace_back(x, i);
  }

//  exit(0);
  stack<int> st;
  for(int i = n; i >= 1; i--) {
    while (!st.empty() && a[st.top()] < a[i]) st.pop();
    nxt[i] = st.empty() ? n + 1 : st.top();
    st.push(i);
  }

  auto add = [&] (int start, int reach) {
    int u = start;
    while (u <= reach) {
      ed[u] = min(nxt[u] - 1, reach);
      up(1, 1, n, a[u], ed[u] - u + 1);
      u = ed[u] + 1;
    }
  };

  add(1, n);

  for(int turn = 0, change = 1; turn <= n; turn++) {
    for(auto [x, i] : query[turn]) {
      int pos = walk_on_tree(1, 1, n, x);
      int pre = get(1, 1, n, 1, pos - 1);
      pos = rev_a[pos];
      ans[i] = a[pos + x - pre - 1];
    }
    if (change == 0) continue;
    int pos = walk_on_tree(1, 1, n, n / 2);
    int pre = get(1, 1, n, 1, pos - 1);
    pos = rev_a[pos];
    if (pre + ed[pos] - pos + 1 == n / 2) {
      change = 0;
      continue;
    }
    int old = ed[pos];
    ed[pos] = pos + n / 2 - pre - 1;
    up(1, 1, n, a[pos], ed[pos] - pos + 1);
    add(ed[pos] + 1, old);
  }

  for(int i = 1; i <= q; i++) cout << ans[i] << endl;
}

Compilation message

Main.cpp: In function 'void up(int, int, int, int, int)':
Main.cpp:21:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   21 |   int mid = l + r >> 1;
      |             ~~^~~
Main.cpp: In function 'int get(int, int, int, int, int)':
Main.cpp:30:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   30 |   int mid = l + r >> 1;
      |             ~~^~~
Main.cpp: In function 'int walk_on_tree(int, int, int, int)':
Main.cpp:36:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   36 |   int mid = l + r >> 1;
      |             ~~^~~
Main.cpp: In function 'int32_t main()':
Main.cpp:47:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   47 |     freopen("task.inp", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:48:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   48 |     freopen("task.out", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:52:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   52 |     freopen (task".inp", "r", stdin);
      |     ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:53:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   53 |     freopen (task".out", "w", stdout);
      |     ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 235 ms 57164 KB Output is correct
2 Correct 288 ms 55688 KB Output is correct
3 Correct 283 ms 55208 KB Output is correct
4 Correct 214 ms 53800 KB Output is correct
5 Correct 260 ms 56984 KB Output is correct
6 Correct 226 ms 56952 KB Output is correct
7 Correct 228 ms 58384 KB Output is correct
8 Correct 202 ms 55704 KB Output is correct
9 Correct 210 ms 54508 KB Output is correct
10 Correct 244 ms 54236 KB Output is correct
11 Correct 209 ms 54780 KB Output is correct
12 Correct 197 ms 52060 KB Output is correct
13 Correct 217 ms 53376 KB Output is correct
14 Correct 224 ms 56152 KB Output is correct
15 Correct 214 ms 53844 KB Output is correct
16 Correct 7 ms 33272 KB Output is correct
17 Correct 284 ms 52664 KB Output is correct
18 Correct 210 ms 52340 KB Output is correct
19 Correct 8 ms 33116 KB Output is correct
20 Correct 7 ms 33112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 394 ms 69504 KB Output is correct
2 Correct 363 ms 67448 KB Output is correct
3 Correct 303 ms 61372 KB Output is correct
4 Correct 249 ms 58952 KB Output is correct
5 Correct 267 ms 61732 KB Output is correct
6 Correct 286 ms 60092 KB Output is correct
7 Correct 335 ms 68212 KB Output is correct
8 Correct 305 ms 70068 KB Output is correct
9 Correct 251 ms 66452 KB Output is correct
10 Correct 283 ms 66272 KB Output is correct
11 Correct 224 ms 62388 KB Output is correct
12 Correct 238 ms 62168 KB Output is correct
13 Correct 264 ms 65044 KB Output is correct
14 Correct 234 ms 62036 KB Output is correct
15 Correct 282 ms 68716 KB Output is correct
16 Correct 25 ms 42756 KB Output is correct
17 Correct 322 ms 65752 KB Output is correct
18 Correct 209 ms 62320 KB Output is correct
19 Correct 73 ms 49348 KB Output is correct
20 Correct 78 ms 50884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 94 ms 43952 KB Output is correct
2 Correct 78 ms 43764 KB Output is correct
3 Correct 76 ms 43724 KB Output is correct
4 Correct 49 ms 43088 KB Output is correct
5 Correct 83 ms 43600 KB Output is correct
6 Correct 54 ms 43092 KB Output is correct
7 Correct 62 ms 41804 KB Output is correct
8 Correct 59 ms 39540 KB Output is correct
9 Correct 65 ms 41556 KB Output is correct
10 Correct 49 ms 39252 KB Output is correct
11 Correct 47 ms 41388 KB Output is correct
12 Correct 47 ms 39248 KB Output is correct
13 Correct 56 ms 41144 KB Output is correct
14 Correct 50 ms 39340 KB Output is correct
15 Correct 44 ms 39252 KB Output is correct
16 Correct 15 ms 36172 KB Output is correct
17 Correct 60 ms 40904 KB Output is correct
18 Correct 36 ms 38352 KB Output is correct
19 Correct 7 ms 33112 KB Output is correct
20 Correct 8 ms 33112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 235 ms 57164 KB Output is correct
2 Correct 288 ms 55688 KB Output is correct
3 Correct 283 ms 55208 KB Output is correct
4 Correct 214 ms 53800 KB Output is correct
5 Correct 260 ms 56984 KB Output is correct
6 Correct 226 ms 56952 KB Output is correct
7 Correct 228 ms 58384 KB Output is correct
8 Correct 202 ms 55704 KB Output is correct
9 Correct 210 ms 54508 KB Output is correct
10 Correct 244 ms 54236 KB Output is correct
11 Correct 209 ms 54780 KB Output is correct
12 Correct 197 ms 52060 KB Output is correct
13 Correct 217 ms 53376 KB Output is correct
14 Correct 224 ms 56152 KB Output is correct
15 Correct 214 ms 53844 KB Output is correct
16 Correct 7 ms 33272 KB Output is correct
17 Correct 284 ms 52664 KB Output is correct
18 Correct 210 ms 52340 KB Output is correct
19 Correct 8 ms 33116 KB Output is correct
20 Correct 7 ms 33112 KB Output is correct
21 Correct 394 ms 69504 KB Output is correct
22 Correct 363 ms 67448 KB Output is correct
23 Correct 303 ms 61372 KB Output is correct
24 Correct 249 ms 58952 KB Output is correct
25 Correct 267 ms 61732 KB Output is correct
26 Correct 286 ms 60092 KB Output is correct
27 Correct 335 ms 68212 KB Output is correct
28 Correct 305 ms 70068 KB Output is correct
29 Correct 251 ms 66452 KB Output is correct
30 Correct 283 ms 66272 KB Output is correct
31 Correct 224 ms 62388 KB Output is correct
32 Correct 238 ms 62168 KB Output is correct
33 Correct 264 ms 65044 KB Output is correct
34 Correct 234 ms 62036 KB Output is correct
35 Correct 282 ms 68716 KB Output is correct
36 Correct 25 ms 42756 KB Output is correct
37 Correct 322 ms 65752 KB Output is correct
38 Correct 209 ms 62320 KB Output is correct
39 Correct 73 ms 49348 KB Output is correct
40 Correct 78 ms 50884 KB Output is correct
41 Correct 94 ms 43952 KB Output is correct
42 Correct 78 ms 43764 KB Output is correct
43 Correct 76 ms 43724 KB Output is correct
44 Correct 49 ms 43088 KB Output is correct
45 Correct 83 ms 43600 KB Output is correct
46 Correct 54 ms 43092 KB Output is correct
47 Correct 62 ms 41804 KB Output is correct
48 Correct 59 ms 39540 KB Output is correct
49 Correct 65 ms 41556 KB Output is correct
50 Correct 49 ms 39252 KB Output is correct
51 Correct 47 ms 41388 KB Output is correct
52 Correct 47 ms 39248 KB Output is correct
53 Correct 56 ms 41144 KB Output is correct
54 Correct 50 ms 39340 KB Output is correct
55 Correct 44 ms 39252 KB Output is correct
56 Correct 15 ms 36172 KB Output is correct
57 Correct 60 ms 40904 KB Output is correct
58 Correct 36 ms 38352 KB Output is correct
59 Correct 7 ms 33112 KB Output is correct
60 Correct 8 ms 33112 KB Output is correct
61 Correct 651 ms 77312 KB Output is correct
62 Correct 543 ms 77524 KB Output is correct
63 Correct 627 ms 76428 KB Output is correct
64 Correct 401 ms 75604 KB Output is correct
65 Correct 550 ms 77276 KB Output is correct
66 Correct 406 ms 74300 KB Output is correct
67 Correct 365 ms 74832 KB Output is correct
68 Correct 390 ms 72136 KB Output is correct
69 Correct 418 ms 75552 KB Output is correct
70 Correct 396 ms 72084 KB Output is correct
71 Correct 334 ms 70028 KB Output is correct
72 Correct 362 ms 72548 KB Output is correct
73 Correct 358 ms 71284 KB Output is correct
74 Correct 426 ms 66028 KB Output is correct
75 Correct 417 ms 63228 KB Output is correct
76 Correct 26 ms 30560 KB Output is correct
77 Correct 484 ms 57776 KB Output is correct
78 Correct 281 ms 54180 KB Output is correct
79 Correct 10 ms 24920 KB Output is correct
80 Correct 8 ms 24924 KB Output is correct