Submission #798162

# Submission time Handle Problem Language Result Execution time Memory
798162 2023-07-30T12:19:34 Z IvanJ Abracadabra (CEOI22_abracadabra) C++17
100 / 100
647 ms 63392 KB
#include<bits/stdc++.h>

#define pb push_back
#define x first
#define y second
#define all(a) (a).begin(), (a).end()

using namespace std;

typedef long long ll;
typedef pair<int, int> ii;

const int maxn = 2e5 + 5, maxq = 1e6 + 5;

struct Data {
	int v, l, r, id;
	friend bool operator<(Data A, Data B) {
		if(A.v != B.v)
			return A.v < B.v;
		return A.id < B.id;
	}
	int len() {return r - l + 1;}
};

int n, q, m;
int p[maxn];
int ID[maxn * 2];
int nxt[maxn];
int ans[maxq];
vector<ii> qs[maxn];
set<Data> active;
vector<Data> blocks;

struct Seg {
	int pot;
	vector<int> T;
	void init(int sz) {
		pot = 1;
		while(pot < sz) pot <<= 1;
		T = vector<int>((pot << 1), 0);
	}
	
	void update(int x, int v) {
		x += pot;
		T[x] = v, x >>= 1;
		while(x > 0) T[x] = T[x * 2] + T[x * 2 + 1], x >>= 1;
	}
	
	ii query(int x, int lo, int hi, int cnt) {
		if(lo == hi) return {x - pot, cnt};
		int mid = (lo + hi) / 2;
		if(T[x * 2] >= cnt) 
			return query(x * 2, lo, mid, cnt);
		return query(x * 2 + 1, mid + 1, hi, cnt - T[x * 2]);
	}
	
	int get(int x) {
		ii res = query(1, 0, pot - 1, x);
		return p[blocks[res.x].l + res.y - 1];
	}
} S;

void init() {
	int x = 0, idx = 0;
	while(x != n) 
		blocks.pb({p[x], x, nxt[x] - 1, idx++}), x = nxt[x];
}

void build() {
	int sz = n, idx = (int)blocks.size();
	for(Data D : blocks)
		active.insert(D);
	
	int flag = 1;
	while(flag) {
		while(1) {
			auto it = --active.end();
			int len = it->r - it->l + 1;
			if(sz - len >= n / 2) 
				active.erase(it), sz -= len;
			else break;
		}
		
		if(sz == n / 2) flag = 0;
		else {
			auto it = --active.end();
			Data D = *it;
			active.erase(it);
			int nr = D.r - (sz - (n / 2)) + 1;
			Data D1 = {D.v, D.l, nr - 1, idx++};
			active.insert(D1), blocks.pb(D1);
			while(nr <= D.r) {
				int hi = min(nxt[nr] - 1, D.r);
				Data Di = {p[nr], nr, hi, idx++};
				active.insert(Di), blocks.pb(Di);
				nr = hi + 1;
			}
		}
	}
	
	m = (int)blocks.size();
	sort(all(blocks));
	for(int i = 0;i < m;i++) 
		ID[blocks[i].id] = i;
	S.init(m);
}

void solve() {
	int x = 0, idx = 0, sz = n;
	active.clear();
	while(x != n) {
		S.update(ID[idx], nxt[x] - x);
		active.insert({p[x], x, nxt[x] - 1, idx++});
		x = nxt[x];
	}
	
	for(int t = 0;t <= n;t++) {
		for(ii qi : qs[t]) 
			ans[qi.y] = S.get(qi.x);
		
		while(1) {
			auto it = --active.end();
			int len = it->r - it->l + 1;
			if(sz - len >= n / 2) 
				active.erase(it), sz -= len;
			else break;
		}
		
		if(sz > n / 2) {
			auto it = --active.end();
			Data D = *it;
			active.erase(it);
			S.update(ID[D.id], 0);
			int nr = D.r - (sz - (n / 2)) + 1;
			Data D1 = {D.v, D.l, nr - 1, idx++};
			active.insert(D1);
			S.update(ID[D1.id], D1.len());
			while(nr <= D.r) {
				int hi = min(nxt[nr] - 1, D.r);
				Data Di = {p[nr], nr, hi, idx++};
				active.insert(Di);
				S.update(ID[Di.id], Di.len());
				nr = hi + 1;
			}
		}
	}
}

int main() {
	scanf("%d%d", &n, &q);
	for(int i = 0;i < n;i++) 
		scanf("%d", p + i);
	for(int i = 0;i < q;i++) {
		int t, x;
		scanf("%d%d", &t, &x);
		qs[min(t, n)].pb({x, i});
	}
	
	p[n] = n + 1;
	vector<int> v = {n};
	for(int i = n - 1;i >= 0;i--) {
		while(p[i] > p[v.back()]) v.pop_back();
		nxt[i] = v.back(), v.pb(i);
	}
	
	init();
	build();
	memset(ans, -1, sizeof ans);
	solve();
	for(int i = 0;i < q;i++) 
		printf("%d\n", ans[i]);
	return 0;
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:150:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  150 |  scanf("%d%d", &n, &q);
      |  ~~~~~^~~~~~~~~~~~~~~~
Main.cpp:152:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  152 |   scanf("%d", p + i);
      |   ~~~~~^~~~~~~~~~~~~
Main.cpp:155:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  155 |   scanf("%d%d", &t, &x);
      |   ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 237 ms 23084 KB Output is correct
2 Correct 282 ms 21784 KB Output is correct
3 Correct 236 ms 21708 KB Output is correct
4 Correct 202 ms 20580 KB Output is correct
5 Correct 234 ms 23628 KB Output is correct
6 Correct 197 ms 24296 KB Output is correct
7 Correct 221 ms 24140 KB Output is correct
8 Correct 206 ms 22780 KB Output is correct
9 Correct 202 ms 22172 KB Output is correct
10 Correct 205 ms 21492 KB Output is correct
11 Correct 211 ms 22020 KB Output is correct
12 Correct 202 ms 20192 KB Output is correct
13 Correct 210 ms 20928 KB Output is correct
14 Correct 260 ms 23268 KB Output is correct
15 Correct 218 ms 21460 KB Output is correct
16 Correct 4 ms 8916 KB Output is correct
17 Correct 272 ms 20372 KB Output is correct
18 Correct 218 ms 20300 KB Output is correct
19 Correct 4 ms 8916 KB Output is correct
20 Correct 4 ms 8916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 419 ms 42860 KB Output is correct
2 Correct 422 ms 55012 KB Output is correct
3 Correct 398 ms 50508 KB Output is correct
4 Correct 257 ms 38404 KB Output is correct
5 Correct 307 ms 39452 KB Output is correct
6 Correct 286 ms 38140 KB Output is correct
7 Correct 319 ms 41112 KB Output is correct
8 Correct 337 ms 40740 KB Output is correct
9 Correct 290 ms 38756 KB Output is correct
10 Correct 274 ms 36692 KB Output is correct
11 Correct 232 ms 31228 KB Output is correct
12 Correct 276 ms 32540 KB Output is correct
13 Correct 242 ms 35348 KB Output is correct
14 Correct 209 ms 32524 KB Output is correct
15 Correct 271 ms 37088 KB Output is correct
16 Correct 22 ms 11780 KB Output is correct
17 Correct 360 ms 51752 KB Output is correct
18 Correct 189 ms 29124 KB Output is correct
19 Correct 67 ms 16244 KB Output is correct
20 Correct 84 ms 18172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 109 ms 21480 KB Output is correct
2 Correct 112 ms 21952 KB Output is correct
3 Correct 117 ms 21680 KB Output is correct
4 Correct 49 ms 14420 KB Output is correct
5 Correct 84 ms 19320 KB Output is correct
6 Correct 51 ms 14792 KB Output is correct
7 Correct 68 ms 17192 KB Output is correct
8 Correct 66 ms 16036 KB Output is correct
9 Correct 76 ms 17848 KB Output is correct
10 Correct 37 ms 13004 KB Output is correct
11 Correct 39 ms 13460 KB Output is correct
12 Correct 38 ms 13056 KB Output is correct
13 Correct 39 ms 13260 KB Output is correct
14 Correct 39 ms 13260 KB Output is correct
15 Correct 38 ms 12864 KB Output is correct
16 Correct 14 ms 10520 KB Output is correct
17 Correct 83 ms 21548 KB Output is correct
18 Correct 33 ms 12372 KB Output is correct
19 Correct 4 ms 8912 KB Output is correct
20 Correct 4 ms 8916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 237 ms 23084 KB Output is correct
2 Correct 282 ms 21784 KB Output is correct
3 Correct 236 ms 21708 KB Output is correct
4 Correct 202 ms 20580 KB Output is correct
5 Correct 234 ms 23628 KB Output is correct
6 Correct 197 ms 24296 KB Output is correct
7 Correct 221 ms 24140 KB Output is correct
8 Correct 206 ms 22780 KB Output is correct
9 Correct 202 ms 22172 KB Output is correct
10 Correct 205 ms 21492 KB Output is correct
11 Correct 211 ms 22020 KB Output is correct
12 Correct 202 ms 20192 KB Output is correct
13 Correct 210 ms 20928 KB Output is correct
14 Correct 260 ms 23268 KB Output is correct
15 Correct 218 ms 21460 KB Output is correct
16 Correct 4 ms 8916 KB Output is correct
17 Correct 272 ms 20372 KB Output is correct
18 Correct 218 ms 20300 KB Output is correct
19 Correct 4 ms 8916 KB Output is correct
20 Correct 4 ms 8916 KB Output is correct
21 Correct 419 ms 42860 KB Output is correct
22 Correct 422 ms 55012 KB Output is correct
23 Correct 398 ms 50508 KB Output is correct
24 Correct 257 ms 38404 KB Output is correct
25 Correct 307 ms 39452 KB Output is correct
26 Correct 286 ms 38140 KB Output is correct
27 Correct 319 ms 41112 KB Output is correct
28 Correct 337 ms 40740 KB Output is correct
29 Correct 290 ms 38756 KB Output is correct
30 Correct 274 ms 36692 KB Output is correct
31 Correct 232 ms 31228 KB Output is correct
32 Correct 276 ms 32540 KB Output is correct
33 Correct 242 ms 35348 KB Output is correct
34 Correct 209 ms 32524 KB Output is correct
35 Correct 271 ms 37088 KB Output is correct
36 Correct 22 ms 11780 KB Output is correct
37 Correct 360 ms 51752 KB Output is correct
38 Correct 189 ms 29124 KB Output is correct
39 Correct 67 ms 16244 KB Output is correct
40 Correct 84 ms 18172 KB Output is correct
41 Correct 109 ms 21480 KB Output is correct
42 Correct 112 ms 21952 KB Output is correct
43 Correct 117 ms 21680 KB Output is correct
44 Correct 49 ms 14420 KB Output is correct
45 Correct 84 ms 19320 KB Output is correct
46 Correct 51 ms 14792 KB Output is correct
47 Correct 68 ms 17192 KB Output is correct
48 Correct 66 ms 16036 KB Output is correct
49 Correct 76 ms 17848 KB Output is correct
50 Correct 37 ms 13004 KB Output is correct
51 Correct 39 ms 13460 KB Output is correct
52 Correct 38 ms 13056 KB Output is correct
53 Correct 39 ms 13260 KB Output is correct
54 Correct 39 ms 13260 KB Output is correct
55 Correct 38 ms 12864 KB Output is correct
56 Correct 14 ms 10520 KB Output is correct
57 Correct 83 ms 21548 KB Output is correct
58 Correct 33 ms 12372 KB Output is correct
59 Correct 4 ms 8912 KB Output is correct
60 Correct 4 ms 8916 KB Output is correct
61 Correct 647 ms 63392 KB Output is correct
62 Correct 559 ms 59440 KB Output is correct
63 Correct 631 ms 58160 KB Output is correct
64 Correct 422 ms 47340 KB Output is correct
65 Correct 427 ms 49328 KB Output is correct
66 Correct 405 ms 47288 KB Output is correct
67 Correct 366 ms 43528 KB Output is correct
68 Correct 355 ms 43096 KB Output is correct
69 Correct 378 ms 46172 KB Output is correct
70 Correct 339 ms 39052 KB Output is correct
71 Correct 286 ms 39956 KB Output is correct
72 Correct 324 ms 39656 KB Output is correct
73 Correct 303 ms 39584 KB Output is correct
74 Correct 304 ms 41672 KB Output is correct
75 Correct 305 ms 40264 KB Output is correct
76 Correct 22 ms 11804 KB Output is correct
77 Correct 438 ms 51996 KB Output is correct
78 Correct 237 ms 33324 KB Output is correct
79 Correct 4 ms 8916 KB Output is correct
80 Correct 4 ms 8916 KB Output is correct