Submission #954438

# Submission time Handle Problem Language Result Execution time Memory
954438 2024-03-27T22:15:13 Z islingr Abracadabra (CEOI22_abracadabra) C++17
100 / 100
651 ms 47188 KB
#include "bits/stdc++.h"
using namespace std;

#define rep(i, a, b) for (auto i{a}; i < (b); ++i)
#define per(i, a, b) for (auto i{b}; i-- > (a); )
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

template <class T> bool uin(T& a, const T& b) { return a > b ? a = b, true : false; }
template <class T> bool uax(T& a, const T& b) { return a < b ? a = b, true : false; }

using ll = long long;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

constexpr int N = 2e5 + 10;
int a[N], nxt[N];

struct Node {
	Node *l = 0, *r = 0;
	int idx, len, c; int y;
	Node(int idx, int len) : idx{idx}, len{len}, c{len}, y(rng()) {}
	void pull();
};

int cnt(Node* n) { return n ? n->c : 0; }
void Node::pull() { c = cnt(l) + cnt(r) + len; }

template<class F> void each(Node* n, F f) {
	if (n) { each(n->l, f); f(n); each(n->r, f); }
}

pair<Node*, int> find_idx(Node* n, int k) {
	if (k < cnt(n->l)) return find_idx(n->l, k);
	k -= cnt(n->l);
	if (k < n->len) return {n, k};
	return find_idx(n->r, k - n->len);
}

array<Node*, 3> split_idx(Node* n, int k) {
	if (k < cnt(n->l)) {
		auto L = split_idx(n->l, k);
		n->l = L[2];
		n->pull();
		return {L[0], L[1], n};
	}
	k -= cnt(n->l);
	if (k < n->len) {
		const auto L = n->l, R = n->r;
		n->l = n->r = nullptr;
		n->pull();
		return {L, n, R};
	}
	k -= n->len;
	auto R = split_idx(n->r, k);
	n->r = R[0];
	n->pull();
	return {n, R[1], R[2]};
}

array<Node*, 2> split_val(Node *n, int k) {
	if (!n) return {};
	if (a[n->idx] >= k) {
		auto pa = split_val(n->l, k);
		n->l = pa[1];
		n->pull();
		return {pa[0], n};
	} else {
		auto pa = split_val(n->r, k);
		n->r = pa[0];
		n->pull();
		return {n, pa[1]};
	}
}

/*
   pair<Node*, Node*> split(Node* n, int k) {
   if (!n) return {};
   if (cnt(n->l) >= k) { // "n->val >= k" for lower_bound(k)
   auto pa = split(n->l, k);
   n->l = pa.second;
   n->pull();
   return {pa.first, n};
   } else {
   auto pa = split(n->r, k - cnt(n->l) - 1); // and just "k"
   n->r = pa.first;
   n->pull();
   return {n, pa.second};
   }
   }
   */

Node* merge(Node* l, Node* r) {
	if (!l) return r;
	if (!r) return l;
	if (l->y > r->y) {
		l->r = merge(l->r, r);
		l->pull();
		return l;
	} else {
		r->l = merge(l, r->l);
		r->pull();
		return r;
	}
}

signed main() {
	cin.tie(nullptr)->sync_with_stdio(false);

	int n, q;
	cin >> n >> q;

	rep(i, 0, n) cin >> a[i];

	per(i, 0, n) {
		nxt[i] = i + 1;
		while (nxt[i] < n and a[nxt[i]] < a[i])
			nxt[i] = nxt[nxt[i]];
	}

	Node* root = nullptr;
	for (int i = 0; i < n; i = nxt[i]) {
		auto node = new Node(i, nxt[i] - i);
		root = merge(root, node);
	}


	int steps = 0;
	auto do_op = [&]() -> bool {
		++steps;

		const auto [__, s] = find_idx(root, n / 2);
		if (s == 0) return true; // no change happens

		auto [L, X, R] = split_idx(root, n / 2);
		assert(X == __);

		const int r = X->idx + X->len;
		Node* LL = nullptr;

		for (int i = X->idx + s; i < r; i = nxt[i]) {
			const auto len = min(nxt[i], r) - i;
			Node *node = new Node(i, len);
			auto tmp = split_val(L, a[i]);

			LL = merge(LL, merge(tmp[0], node));
			L = tmp[1];
		}

		X->len = s;
		X->pull();
		root = merge(LL, merge(L, merge(X, R)));

		return false;
	};

	/*
	auto f = [&](Node* n) {
		rep(i, n->idx, n->idx + n->len)
			cerr << a[i] << ' ';
	};
	*/

	vector<tuple<int, int, int>> query(q);
	{
		int ctr = 0;
		for (auto &[t, i, idx] : query)
			cin >> t >> i, --i, idx = ctr++;
	}

	sort(all(query));
	vector<int> ans(q, -1);

	bool stable = false;
	for (auto [t, i, idx] : query) {
		while (not stable and steps < t)
			stable = do_op();

		const auto [node, s] = find_idx(root, i);
		ans[idx] = a[node->idx + s];
	}

	for (auto x : ans)
		cout << x << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 330 ms 27724 KB Output is correct
2 Correct 293 ms 27360 KB Output is correct
3 Correct 278 ms 26448 KB Output is correct
4 Correct 256 ms 25216 KB Output is correct
5 Correct 264 ms 26956 KB Output is correct
6 Correct 250 ms 25428 KB Output is correct
7 Correct 279 ms 27164 KB Output is correct
8 Correct 255 ms 25684 KB Output is correct
9 Correct 261 ms 25180 KB Output is correct
10 Correct 262 ms 25680 KB Output is correct
11 Correct 255 ms 25624 KB Output is correct
12 Correct 256 ms 24656 KB Output is correct
13 Correct 259 ms 25720 KB Output is correct
14 Correct 265 ms 26400 KB Output is correct
15 Correct 273 ms 26248 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 254 ms 24920 KB Output is correct
18 Correct 247 ms 24660 KB Output is correct
19 Correct 0 ms 344 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 329 ms 45652 KB Output is correct
2 Correct 323 ms 46028 KB Output is correct
3 Correct 274 ms 36692 KB Output is correct
4 Correct 270 ms 31724 KB Output is correct
5 Correct 277 ms 33108 KB Output is correct
6 Correct 240 ms 30536 KB Output is correct
7 Correct 281 ms 38516 KB Output is correct
8 Correct 261 ms 37048 KB Output is correct
9 Correct 266 ms 32600 KB Output is correct
10 Correct 246 ms 34896 KB Output is correct
11 Correct 238 ms 28920 KB Output is correct
12 Correct 226 ms 28940 KB Output is correct
13 Correct 238 ms 34128 KB Output is correct
14 Correct 229 ms 30032 KB Output is correct
15 Correct 249 ms 35920 KB Output is correct
16 Correct 15 ms 3416 KB Output is correct
17 Correct 279 ms 41336 KB Output is correct
18 Correct 192 ms 26388 KB Output is correct
19 Correct 54 ms 9156 KB Output is correct
20 Correct 62 ms 10712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 75 ms 9812 KB Output is correct
2 Correct 74 ms 9556 KB Output is correct
3 Correct 78 ms 9296 KB Output is correct
4 Correct 39 ms 5204 KB Output is correct
5 Correct 68 ms 7192 KB Output is correct
6 Correct 42 ms 5464 KB Output is correct
7 Correct 64 ms 6400 KB Output is correct
8 Correct 49 ms 5896 KB Output is correct
9 Correct 59 ms 6444 KB Output is correct
10 Correct 34 ms 4700 KB Output is correct
11 Correct 38 ms 4948 KB Output is correct
12 Correct 34 ms 4716 KB Output is correct
13 Correct 37 ms 4944 KB Output is correct
14 Correct 35 ms 4948 KB Output is correct
15 Correct 33 ms 4748 KB Output is correct
16 Correct 8 ms 1884 KB Output is correct
17 Correct 44 ms 9040 KB Output is correct
18 Correct 33 ms 4548 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 330 ms 27724 KB Output is correct
2 Correct 293 ms 27360 KB Output is correct
3 Correct 278 ms 26448 KB Output is correct
4 Correct 256 ms 25216 KB Output is correct
5 Correct 264 ms 26956 KB Output is correct
6 Correct 250 ms 25428 KB Output is correct
7 Correct 279 ms 27164 KB Output is correct
8 Correct 255 ms 25684 KB Output is correct
9 Correct 261 ms 25180 KB Output is correct
10 Correct 262 ms 25680 KB Output is correct
11 Correct 255 ms 25624 KB Output is correct
12 Correct 256 ms 24656 KB Output is correct
13 Correct 259 ms 25720 KB Output is correct
14 Correct 265 ms 26400 KB Output is correct
15 Correct 273 ms 26248 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 254 ms 24920 KB Output is correct
18 Correct 247 ms 24660 KB Output is correct
19 Correct 0 ms 344 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 329 ms 45652 KB Output is correct
22 Correct 323 ms 46028 KB Output is correct
23 Correct 274 ms 36692 KB Output is correct
24 Correct 270 ms 31724 KB Output is correct
25 Correct 277 ms 33108 KB Output is correct
26 Correct 240 ms 30536 KB Output is correct
27 Correct 281 ms 38516 KB Output is correct
28 Correct 261 ms 37048 KB Output is correct
29 Correct 266 ms 32600 KB Output is correct
30 Correct 246 ms 34896 KB Output is correct
31 Correct 238 ms 28920 KB Output is correct
32 Correct 226 ms 28940 KB Output is correct
33 Correct 238 ms 34128 KB Output is correct
34 Correct 229 ms 30032 KB Output is correct
35 Correct 249 ms 35920 KB Output is correct
36 Correct 15 ms 3416 KB Output is correct
37 Correct 279 ms 41336 KB Output is correct
38 Correct 192 ms 26388 KB Output is correct
39 Correct 54 ms 9156 KB Output is correct
40 Correct 62 ms 10712 KB Output is correct
41 Correct 75 ms 9812 KB Output is correct
42 Correct 74 ms 9556 KB Output is correct
43 Correct 78 ms 9296 KB Output is correct
44 Correct 39 ms 5204 KB Output is correct
45 Correct 68 ms 7192 KB Output is correct
46 Correct 42 ms 5464 KB Output is correct
47 Correct 64 ms 6400 KB Output is correct
48 Correct 49 ms 5896 KB Output is correct
49 Correct 59 ms 6444 KB Output is correct
50 Correct 34 ms 4700 KB Output is correct
51 Correct 38 ms 4948 KB Output is correct
52 Correct 34 ms 4716 KB Output is correct
53 Correct 37 ms 4944 KB Output is correct
54 Correct 35 ms 4948 KB Output is correct
55 Correct 33 ms 4748 KB Output is correct
56 Correct 8 ms 1884 KB Output is correct
57 Correct 44 ms 9040 KB Output is correct
58 Correct 33 ms 4548 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 0 ms 344 KB Output is correct
61 Correct 534 ms 47188 KB Output is correct
62 Correct 651 ms 46676 KB Output is correct
63 Correct 586 ms 45104 KB Output is correct
64 Correct 365 ms 37884 KB Output is correct
65 Correct 382 ms 39852 KB Output is correct
66 Correct 367 ms 37952 KB Output is correct
67 Correct 353 ms 37968 KB Output is correct
68 Correct 364 ms 37204 KB Output is correct
69 Correct 393 ms 37460 KB Output is correct
70 Correct 329 ms 35408 KB Output is correct
71 Correct 301 ms 34384 KB Output is correct
72 Correct 323 ms 35372 KB Output is correct
73 Correct 316 ms 34812 KB Output is correct
74 Correct 322 ms 35764 KB Output is correct
75 Correct 335 ms 36132 KB Output is correct
76 Correct 20 ms 3160 KB Output is correct
77 Correct 336 ms 41532 KB Output is correct
78 Correct 277 ms 32336 KB Output is correct
79 Correct 0 ms 344 KB Output is correct
80 Correct 0 ms 344 KB Output is correct