Submission #781179

# Submission time Handle Problem Language Result Execution time Memory
781179 2023-07-12T21:02:01 Z tvladm2009 Abracadabra (CEOI22_abracadabra) C++17
100 / 100
916 ms 53456 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const int N = (int) 2e5 + 7;
const int Q = (int) 1e6 + 7;
const int INF = (int) 1e9;
int a[N], sol[Q];
vector<pair<int, int>> questions[N];
int n, q;

mt19937 rng;

int gen() {
  return uniform_int_distribution<int>(INF)(rng);
}

struct Node {
  int val;
  int sz;
  int prio;
  int mx;
  Node* left;
  Node* right;

  Node(int val_ = 0) {
    val = val_;
    mx = val_;
    sz = 1;
    prio = gen();
    left = NULL;
    right = NULL;
  }
};

struct Treap {
  int getsize(Node* &root) {
    if (root == NULL) {
      return 0;
    } else {
      return root->sz;
    }
  }

  int getmax(Node* &root) {
    if (root == NULL) {
      return 0;
    } else {
      return root->mx;
    }
  }

  void compute(Node* &root) {
    if (root == NULL) {
      return;
    }
    root->sz = 1 + getsize(root->left) + getsize(root->right);
    root->mx = max({getmax(root->left), root->val, getmax(root->right)});
  }

  pair<Node*, Node*> split(Node* &root, int target) { /// dupa val
    if (root == NULL) {
      return {NULL, NULL};
    } else if (max(getmax(root->left), root->val) <= target) {
      pair<Node*, Node*> sol = split(root->right, target);
      root->right = sol.first;
      compute(root);
      return {root, sol.second};
    } else {
      pair<Node*, Node*> sol = split(root->left, target);
      root->left = sol.second;
      compute(root);
      return {sol.first, root};
    }
  }

  pair<Node*, Node*> splitSize(Node* &root, int target) { /// dupa size
    if (root == NULL) {
      return {NULL, NULL};
    }
    int cnt = getsize(root->left) + 1;
    if (cnt <= target) {
      pair<Node*, Node*> sol = splitSize(root->right, target - cnt);
      root->right = sol.first;
      compute(root);
      return {root, sol.second};
    } else {
      pair<Node*, Node*> sol = splitSize(root->left, target);
      root->left = sol.second;
      compute(root);
      return {sol.first, root};
    }
  }

  Node* _merge(Node* &a, Node* &b) {
    if (a == NULL && b == NULL) {
      return NULL;
    } else if (a == NULL) {
      return b;
    } else if (b == NULL) {
      return a;
    } else if (a->prio < b->prio) {
      a->right = _merge(a->right, b);
      compute(a);
      return a;
    } else {
      b->left = _merge(a, b->left);
      compute(b);
      if (b != NULL) {
        b->mx = max({b->val, getmax(b->left), getmax(b->right)});
      }
      return b;
    }
  }

  int _kth(Node* &root, int k) {
    if (root == NULL) {
      return -1;
    }
    int cnt = getsize(root->left) + 1;
    if (cnt == k) {
      return root->val;
    } else if (cnt < k) {
      return _kth(root->right, k - cnt);
    } else {
      return _kth(root->left, k);
    }
  }

  Node* root;
  Treap() {
    root = NULL;
  }
  void _insert(int val) {
    pair<Node*, Node*> sol = split(root, val);
    Node* newnode = new Node(val);
    sol.first = _merge(sol.first, newnode);
    root = _merge(sol.first, sol.second);
  }
  int _query(int val) {
    pair<Node*, Node*> sol = split(root, val - 1);
    int ret = getsize(sol.second);
    root = _merge(sol.first, sol.second);
    return ret;
  }
  pair<Node*, Node*> extractSize(int target) {
    return splitSize(root, target);
  }
  pair<Node*, Node*> extractMax(int target) {
    return split(root, target);
  }
  void mergee(Node *aux) {
    root = _merge(root, aux);
  }
  int kth(int x) {
    return _kth(root, x);
  }
  void del() {
    root = NULL;
  }
};

int main() {
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

  cin >> n >> q;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= q; i++) {
    int t, x;
    cin >> t >> x;
    questions[min(t, n)].push_back({x, i});
  }
  Treap t;
  for (int i = 1; i <= n; i++) {
    Node* aux;
    aux = new Node(a[i]);
    t.mergee(aux);
  }
  for (int lap = 0; lap <= n; lap++) {
    if (lap != 0) {
      Treap x, y;
      pair<Node*, Node*> aux = t.extractSize(n / 2);
      x.root = aux.first;
      y.root = aux.second;
      t.del();
      while (x.root != NULL && y.root != NULL) {
        int vv = y.kth(1);
        Treap z, zz;
        if (vv < x.root->mx) {
          pair<Node*, Node*> aux = x.extractMax(vv);
          z.root = aux.first;
          x.root = aux.second;
          int big = INF;
          if (y.root != NULL) {
            big = x.kth(1);
          }
          aux = y.extractMax(big);
          zz.root = aux.first;
          y.root = aux.second;

          t.mergee(z.root);
          t.mergee(zz.root);
        } else {
          break;
        }
      }
      t.mergee(x.root);
      t.mergee(y.root);
    }
    for (auto &it : questions[lap]) {
      sol[it.second] = t.kth(it.first);
    }
  }
  for (int i = 1; i <= q; i++) {
    cout << sol[i] << "\n";
  }
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 223 ms 25452 KB Output is correct
2 Correct 214 ms 24236 KB Output is correct
3 Correct 203 ms 23736 KB Output is correct
4 Correct 197 ms 23600 KB Output is correct
5 Correct 209 ms 30668 KB Output is correct
6 Correct 197 ms 30368 KB Output is correct
7 Correct 215 ms 31556 KB Output is correct
8 Correct 208 ms 29220 KB Output is correct
9 Correct 200 ms 27872 KB Output is correct
10 Correct 235 ms 27812 KB Output is correct
11 Correct 196 ms 28264 KB Output is correct
12 Correct 192 ms 25732 KB Output is correct
13 Correct 201 ms 27204 KB Output is correct
14 Correct 207 ms 29748 KB Output is correct
15 Correct 208 ms 27640 KB Output is correct
16 Correct 3 ms 5032 KB Output is correct
17 Correct 193 ms 25916 KB Output is correct
18 Correct 193 ms 25744 KB Output is correct
19 Correct 2 ms 5032 KB Output is correct
20 Correct 2 ms 4912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 408 ms 35640 KB Output is correct
2 Correct 328 ms 35696 KB Output is correct
3 Correct 295 ms 31276 KB Output is correct
4 Correct 327 ms 31468 KB Output is correct
5 Correct 319 ms 40656 KB Output is correct
6 Correct 295 ms 39340 KB Output is correct
7 Correct 383 ms 46664 KB Output is correct
8 Correct 360 ms 44844 KB Output is correct
9 Correct 384 ms 40500 KB Output is correct
10 Correct 333 ms 43656 KB Output is correct
11 Correct 270 ms 38904 KB Output is correct
12 Correct 309 ms 38404 KB Output is correct
13 Correct 320 ms 43180 KB Output is correct
14 Correct 281 ms 39504 KB Output is correct
15 Correct 327 ms 44908 KB Output is correct
16 Correct 97 ms 16264 KB Output is correct
17 Correct 275 ms 41340 KB Output is correct
18 Correct 273 ms 36424 KB Output is correct
19 Correct 197 ms 21384 KB Output is correct
20 Correct 148 ms 22604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 141 ms 14872 KB Output is correct
2 Correct 127 ms 14156 KB Output is correct
3 Correct 111 ms 14188 KB Output is correct
4 Correct 99 ms 13700 KB Output is correct
5 Correct 150 ms 14404 KB Output is correct
6 Correct 116 ms 13716 KB Output is correct
7 Correct 134 ms 14264 KB Output is correct
8 Correct 128 ms 13704 KB Output is correct
9 Correct 163 ms 14140 KB Output is correct
10 Correct 90 ms 13388 KB Output is correct
11 Correct 98 ms 13692 KB Output is correct
12 Correct 78 ms 13408 KB Output is correct
13 Correct 88 ms 13332 KB Output is correct
14 Correct 90 ms 13596 KB Output is correct
15 Correct 88 ms 13468 KB Output is correct
16 Correct 43 ms 10588 KB Output is correct
17 Correct 78 ms 12900 KB Output is correct
18 Correct 75 ms 12892 KB Output is correct
19 Correct 3 ms 5024 KB Output is correct
20 Correct 3 ms 5032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 223 ms 25452 KB Output is correct
2 Correct 214 ms 24236 KB Output is correct
3 Correct 203 ms 23736 KB Output is correct
4 Correct 197 ms 23600 KB Output is correct
5 Correct 209 ms 30668 KB Output is correct
6 Correct 197 ms 30368 KB Output is correct
7 Correct 215 ms 31556 KB Output is correct
8 Correct 208 ms 29220 KB Output is correct
9 Correct 200 ms 27872 KB Output is correct
10 Correct 235 ms 27812 KB Output is correct
11 Correct 196 ms 28264 KB Output is correct
12 Correct 192 ms 25732 KB Output is correct
13 Correct 201 ms 27204 KB Output is correct
14 Correct 207 ms 29748 KB Output is correct
15 Correct 208 ms 27640 KB Output is correct
16 Correct 3 ms 5032 KB Output is correct
17 Correct 193 ms 25916 KB Output is correct
18 Correct 193 ms 25744 KB Output is correct
19 Correct 2 ms 5032 KB Output is correct
20 Correct 2 ms 4912 KB Output is correct
21 Correct 408 ms 35640 KB Output is correct
22 Correct 328 ms 35696 KB Output is correct
23 Correct 295 ms 31276 KB Output is correct
24 Correct 327 ms 31468 KB Output is correct
25 Correct 319 ms 40656 KB Output is correct
26 Correct 295 ms 39340 KB Output is correct
27 Correct 383 ms 46664 KB Output is correct
28 Correct 360 ms 44844 KB Output is correct
29 Correct 384 ms 40500 KB Output is correct
30 Correct 333 ms 43656 KB Output is correct
31 Correct 270 ms 38904 KB Output is correct
32 Correct 309 ms 38404 KB Output is correct
33 Correct 320 ms 43180 KB Output is correct
34 Correct 281 ms 39504 KB Output is correct
35 Correct 327 ms 44908 KB Output is correct
36 Correct 97 ms 16264 KB Output is correct
37 Correct 275 ms 41340 KB Output is correct
38 Correct 273 ms 36424 KB Output is correct
39 Correct 197 ms 21384 KB Output is correct
40 Correct 148 ms 22604 KB Output is correct
41 Correct 141 ms 14872 KB Output is correct
42 Correct 127 ms 14156 KB Output is correct
43 Correct 111 ms 14188 KB Output is correct
44 Correct 99 ms 13700 KB Output is correct
45 Correct 150 ms 14404 KB Output is correct
46 Correct 116 ms 13716 KB Output is correct
47 Correct 134 ms 14264 KB Output is correct
48 Correct 128 ms 13704 KB Output is correct
49 Correct 163 ms 14140 KB Output is correct
50 Correct 90 ms 13388 KB Output is correct
51 Correct 98 ms 13692 KB Output is correct
52 Correct 78 ms 13408 KB Output is correct
53 Correct 88 ms 13332 KB Output is correct
54 Correct 90 ms 13596 KB Output is correct
55 Correct 88 ms 13468 KB Output is correct
56 Correct 43 ms 10588 KB Output is correct
57 Correct 78 ms 12900 KB Output is correct
58 Correct 75 ms 12892 KB Output is correct
59 Correct 3 ms 5024 KB Output is correct
60 Correct 3 ms 5032 KB Output is correct
61 Correct 916 ms 53456 KB Output is correct
62 Correct 781 ms 50936 KB Output is correct
63 Correct 793 ms 49868 KB Output is correct
64 Correct 778 ms 49364 KB Output is correct
65 Correct 777 ms 50972 KB Output is correct
66 Correct 804 ms 49012 KB Output is correct
67 Correct 741 ms 48748 KB Output is correct
68 Correct 780 ms 46968 KB Output is correct
69 Correct 774 ms 49340 KB Output is correct
70 Correct 586 ms 45820 KB Output is correct
71 Correct 547 ms 46748 KB Output is correct
72 Correct 699 ms 45904 KB Output is correct
73 Correct 575 ms 46600 KB Output is correct
74 Correct 618 ms 48772 KB Output is correct
75 Correct 652 ms 46920 KB Output is correct
76 Correct 86 ms 16332 KB Output is correct
77 Correct 519 ms 41536 KB Output is correct
78 Correct 514 ms 41432 KB Output is correct
79 Correct 3 ms 4948 KB Output is correct
80 Correct 2 ms 4948 KB Output is correct