Submission #789380

# Submission time Handle Problem Language Result Execution time Memory
789380 2023-07-21T10:01:51 Z ymm Abracadabra (CEOI22_abracadabra) C++17
100 / 100
660 ms 70804 KB
#include <bits/stdc++.h>
#define Loop(x,l,r) for (ll x = (l); x < (r); ++x)
#define LoopR(x,l,r) for (ll x = (r)-1; x >= (l); --x)
typedef long long ll;
typedef std::pair<int,int> pii;
using namespace std;

vector<int> shf(vector<int> vec)
{
	int n = vec.size();
	vector<int> ans;
	int p0 = 0, p1 = n/2;
	Loop (_,0,n) {
		if (p1 == n || (p0 < n/2 && vec[p0] < vec[p1]))
			ans.push_back(vec[p0++]);
		else
			ans.push_back(vec[p1++]);
	}
	return ans;
}

const int N = 1'000'010;
void shf2(int *a, int n)
{
	static int tmp[N];
	int p = n/2;
	while (p < n && a[p] < a[0])
		++p;
	memcpy(tmp, a+n/2, (p-n/2)*sizeof(*a));
	memmove(a+p-n/2, a, (n/2)*sizeof(*a));
	memcpy(a, tmp, (p-n/2)*sizeof(*a));
}

int ans[N];
vector<pii> Q[N];

int *ptr[N];

namespace fen {
	const int lg = 21;
	int a[1<<lg];
	void add(int i, int x) {
		++i;
		while (i < (1<<lg)) {
			a[i] += x;
			i += i&-i;
		}
	}
	int get(int r) {
		int ans = 0;
		while (r > 0) {
			ans += a[r];
			r -= r&-r;
		}
		return ans;
	}
	int get(int l, int r) { return get(r) - get(l); }
	pii bin(int x) {
		int ans = 0;
		int sum = 0;
		LoopR (i,0,lg) {
			if (sum + a[ans + (1<<i)] <= x) {
				sum += a[ans + (1<<i)];
				ans += (1<<i);
			}
		}
		return {ans, sum};
	}
}

int sz[N];
int nxt[N];

int main()
{
	cin.tie(0) -> sync_with_stdio(false);
	int n, q;
	cin >> n >> q;
	vector<int> vec(n);
	//iota(vec.begin(), vec.end(), 0);
	//auto pleh = vec;
	//int cntt = 0;
	//for (;;) {
	//	int cnt = 0;
	//	vec = pleh;
	//	for (;;) {
	//		auto tmp = shf(vec);
	//		if (tmp == vec)
	//			break;
	//		vec = tmp;
	//		++cnt;
	//	}
	//	cntt = max(cntt, cnt);
	//	if (!next_permutation(pleh.begin(), pleh.end()))
	//		break;
	//}
	//cout << cntt << '\n';
	//return 0;
	//mt19937_64 rd(chrono::high_resolution_clock::now().time_since_epoch().count());
	//iota(vec.begin(), vec.end(), 0);
	//shuffle(vec.begin(), vec.end(), rd);
	for (int &x : vec)
		cin >> x;
	Loop (i,0,q) {
		int t, p;
		cin >> t >> p;
		--p;
		t = min(t, n);
		Q[t].push_back({p, i});
	}
	for (auto [p, j] : Q[0])
		ans[j] = vec[p];
	int mx = -1;
	Loop (i,0,n/2) {
		if (vec[i] > mx) {
			mx = vec[i];
			ptr[mx] = &vec[i];
		}
		sz[mx]++;
	}
	mx = -1;
	Loop (i,n/2,n) {
		if (vec[i] > mx) {
			mx = vec[i];
			ptr[mx] = &vec[i];
		}
		sz[mx]++;
	}
	vector<int> st;
	LoopR (i,0,n) {
		while (st.size() && vec[st.back()] < vec[i])
			st.pop_back();
		if (!st.size())
			nxt[i] = N;
		else
			nxt[i] = st.back() - i;
		st.push_back(i);
	}
	Loop (i,0,N)
		fen::add(i, sz[i]);

	//vecs.push_back(vec);
	Loop (i,0,n) {
		for (auto [p, j] : Q[i+1]) {
			auto [y, beforey] = fen::bin(p);
			//cerr << y << ' ' << beforey << '\n';
			ans[j] = ptr[y][p - beforey];
		}
		auto [x, before] = fen::bin(n/2);
		if (before == n/2)
			continue;
		int cnt = (before + sz[x]) - n/2;
		//cerr << x << " to rem " << cnt << ' ' << sz[x] << '\n';
		sz[x] -= cnt;
		fen::add(x, -cnt);
		int *p = ptr[x] + sz[x];
		//cerr << "first to split is " << *p << '\n';
		int j = 0;
		int rj = p - vec.data();
		while (j < cnt) {
			int g = min(cnt-j, nxt[rj]);
			//cerr << p[j] << " added!\n";
			sz[p[j]] += g;
			fen::add(p[j], g);
			ptr[p[j]] = p+j;
			j += g;
			rj += g;
		}
		//cerr << "hola!\n";
	}
	Loop (i,0,q)
		cout << ans[i] << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 269 ms 46016 KB Output is correct
2 Correct 279 ms 45704 KB Output is correct
3 Correct 272 ms 45272 KB Output is correct
4 Correct 260 ms 45040 KB Output is correct
5 Correct 302 ms 47728 KB Output is correct
6 Correct 251 ms 47800 KB Output is correct
7 Correct 275 ms 48076 KB Output is correct
8 Correct 268 ms 46560 KB Output is correct
9 Correct 250 ms 45408 KB Output is correct
10 Correct 254 ms 45260 KB Output is correct
11 Correct 253 ms 45644 KB Output is correct
12 Correct 242 ms 43832 KB Output is correct
13 Correct 256 ms 44656 KB Output is correct
14 Correct 261 ms 46836 KB Output is correct
15 Correct 261 ms 45048 KB Output is correct
16 Correct 36 ms 27772 KB Output is correct
17 Correct 168 ms 43996 KB Output is correct
18 Correct 218 ms 43924 KB Output is correct
19 Correct 36 ms 27724 KB Output is correct
20 Correct 31 ms 27664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 329 ms 50296 KB Output is correct
2 Correct 347 ms 64944 KB Output is correct
3 Correct 276 ms 57252 KB Output is correct
4 Correct 271 ms 56628 KB Output is correct
5 Correct 284 ms 57348 KB Output is correct
6 Correct 286 ms 56332 KB Output is correct
7 Correct 328 ms 63256 KB Output is correct
8 Correct 320 ms 61652 KB Output is correct
9 Correct 288 ms 57184 KB Output is correct
10 Correct 310 ms 60592 KB Output is correct
11 Correct 263 ms 55208 KB Output is correct
12 Correct 264 ms 55460 KB Output is correct
13 Correct 339 ms 60028 KB Output is correct
14 Correct 275 ms 56264 KB Output is correct
15 Correct 318 ms 61524 KB Output is correct
16 Correct 62 ms 32664 KB Output is correct
17 Correct 206 ms 59988 KB Output is correct
18 Correct 246 ms 50996 KB Output is correct
19 Correct 94 ms 37604 KB Output is correct
20 Correct 145 ms 39452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 93 ms 33260 KB Output is correct
2 Correct 82 ms 32960 KB Output is correct
3 Correct 87 ms 33000 KB Output is correct
4 Correct 81 ms 32368 KB Output is correct
5 Correct 90 ms 32944 KB Output is correct
6 Correct 97 ms 32480 KB Output is correct
7 Correct 91 ms 32912 KB Output is correct
8 Correct 81 ms 32664 KB Output is correct
9 Correct 85 ms 32760 KB Output is correct
10 Correct 74 ms 32444 KB Output is correct
11 Correct 85 ms 32596 KB Output is correct
12 Correct 86 ms 32420 KB Output is correct
13 Correct 80 ms 32384 KB Output is correct
14 Correct 78 ms 32532 KB Output is correct
15 Correct 83 ms 32176 KB Output is correct
16 Correct 50 ms 30280 KB Output is correct
17 Correct 80 ms 32868 KB Output is correct
18 Correct 68 ms 30928 KB Output is correct
19 Correct 36 ms 27652 KB Output is correct
20 Correct 36 ms 27652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 269 ms 46016 KB Output is correct
2 Correct 279 ms 45704 KB Output is correct
3 Correct 272 ms 45272 KB Output is correct
4 Correct 260 ms 45040 KB Output is correct
5 Correct 302 ms 47728 KB Output is correct
6 Correct 251 ms 47800 KB Output is correct
7 Correct 275 ms 48076 KB Output is correct
8 Correct 268 ms 46560 KB Output is correct
9 Correct 250 ms 45408 KB Output is correct
10 Correct 254 ms 45260 KB Output is correct
11 Correct 253 ms 45644 KB Output is correct
12 Correct 242 ms 43832 KB Output is correct
13 Correct 256 ms 44656 KB Output is correct
14 Correct 261 ms 46836 KB Output is correct
15 Correct 261 ms 45048 KB Output is correct
16 Correct 36 ms 27772 KB Output is correct
17 Correct 168 ms 43996 KB Output is correct
18 Correct 218 ms 43924 KB Output is correct
19 Correct 36 ms 27724 KB Output is correct
20 Correct 31 ms 27664 KB Output is correct
21 Correct 329 ms 50296 KB Output is correct
22 Correct 347 ms 64944 KB Output is correct
23 Correct 276 ms 57252 KB Output is correct
24 Correct 271 ms 56628 KB Output is correct
25 Correct 284 ms 57348 KB Output is correct
26 Correct 286 ms 56332 KB Output is correct
27 Correct 328 ms 63256 KB Output is correct
28 Correct 320 ms 61652 KB Output is correct
29 Correct 288 ms 57184 KB Output is correct
30 Correct 310 ms 60592 KB Output is correct
31 Correct 263 ms 55208 KB Output is correct
32 Correct 264 ms 55460 KB Output is correct
33 Correct 339 ms 60028 KB Output is correct
34 Correct 275 ms 56264 KB Output is correct
35 Correct 318 ms 61524 KB Output is correct
36 Correct 62 ms 32664 KB Output is correct
37 Correct 206 ms 59988 KB Output is correct
38 Correct 246 ms 50996 KB Output is correct
39 Correct 94 ms 37604 KB Output is correct
40 Correct 145 ms 39452 KB Output is correct
41 Correct 93 ms 33260 KB Output is correct
42 Correct 82 ms 32960 KB Output is correct
43 Correct 87 ms 33000 KB Output is correct
44 Correct 81 ms 32368 KB Output is correct
45 Correct 90 ms 32944 KB Output is correct
46 Correct 97 ms 32480 KB Output is correct
47 Correct 91 ms 32912 KB Output is correct
48 Correct 81 ms 32664 KB Output is correct
49 Correct 85 ms 32760 KB Output is correct
50 Correct 74 ms 32444 KB Output is correct
51 Correct 85 ms 32596 KB Output is correct
52 Correct 86 ms 32420 KB Output is correct
53 Correct 80 ms 32384 KB Output is correct
54 Correct 78 ms 32532 KB Output is correct
55 Correct 83 ms 32176 KB Output is correct
56 Correct 50 ms 30280 KB Output is correct
57 Correct 80 ms 32868 KB Output is correct
58 Correct 68 ms 30928 KB Output is correct
59 Correct 36 ms 27652 KB Output is correct
60 Correct 36 ms 27652 KB Output is correct
61 Correct 660 ms 70804 KB Output is correct
62 Correct 572 ms 69492 KB Output is correct
63 Correct 569 ms 68192 KB Output is correct
64 Correct 439 ms 67276 KB Output is correct
65 Correct 469 ms 69040 KB Output is correct
66 Correct 521 ms 67100 KB Output is correct
67 Correct 431 ms 66760 KB Output is correct
68 Correct 418 ms 65140 KB Output is correct
69 Correct 464 ms 67304 KB Output is correct
70 Correct 374 ms 63588 KB Output is correct
71 Correct 371 ms 64676 KB Output is correct
72 Correct 356 ms 63868 KB Output is correct
73 Correct 360 ms 64400 KB Output is correct
74 Correct 409 ms 66412 KB Output is correct
75 Correct 349 ms 64972 KB Output is correct
76 Correct 66 ms 32644 KB Output is correct
77 Correct 230 ms 60112 KB Output is correct
78 Correct 271 ms 56016 KB Output is correct
79 Correct 32 ms 27652 KB Output is correct
80 Correct 32 ms 27708 KB Output is correct