Submission #716577

# Submission time Handle Problem Language Result Execution time Memory
716577 2023-03-30T12:25:01 Z Arturgo Abracadabra (CEOI22_abracadabra) C++14
100 / 100
2245 ms 55976 KB
#include <bits/stdc++.h>
using namespace std;

struct Arbre {
	int rd, l, r;
	int lb, rb;
	int sz;
	
	Arbre(int _lb, int _rb) {
		rd = rand();
		lb = _lb;
		rb = _rb;
		l = r = -1;
		sz = rb - lb;
	}
};

int root = -1;
vector<Arbre> arbres;

int N, Q;

vector<int> nbs;
vector<int> nextBig;

int sz(int a) {
	if(a == -1) return 0;
	return arbres[a].sz;
}

void pull(int a) {
	arbres[a].sz = (arbres[a].rb - arbres[a].lb) + sz(arbres[a].l) + sz(arbres[a].r);
}

int merge(int a, int b) {
	if(a == -1) return b;
	if(b == -1) return a;
	
	if(arbres[a].rd < arbres[b].rd) {
		arbres[a].r = merge(arbres[a].r, b);
		pull(a);
		return a;
	}
	else {
		arbres[b].l = merge(a, arbres[b].l);
		pull(b);
		return b;
	}
}

pair<int, int> split(int a, int x) {
	if(a == -1) return {-1, -1};
	
	if(nbs[arbres[a].lb] < x) {
		pair<int, int> rht = split(arbres[a].r, x);
		arbres[a].r = -1;
		pull(a);
		return {merge(a, rht.first), rht.second};
	}
	else {
		pair<int, int> lft = split(arbres[a].l, x);
		arbres[a].l = -1;
		pull(a);
		return {lft.first, merge(lft.second, a)};
	}
}

int insert(int root, int lb, int ub) {
	pair<int, int> spl = split(root, nbs[lb]);
	int id = arbres.size();
	arbres.push_back(Arbre(lb, ub));
	return merge(merge(spl.first, id), spl.second);
}

int remove(int root, int x) {
	pair<int, int> spl1 = split(root, x);
	pair<int, int> spl2 = split(spl1.second, x + 1);
	return merge(spl1.first, spl2.second);
}

struct Inter {
	int l, r;
	int li, ri;
};

Inter inter_pos(int a, int pos) {
	if(pos < sz(arbres[a].l)) {
		return inter_pos(arbres[a].l, pos);
	}
	pos -= sz(arbres[a].l);
	
	if(pos < arbres[a].rb - arbres[a].lb) {
		return {sz(arbres[a].l), sz(arbres[a].l) + arbres[a].rb - arbres[a].lb, arbres[a].lb, arbres[a].rb};
	}
	pos -= (arbres[a].rb - arbres[a].lb);
	
	Inter res = inter_pos(arbres[a].r, pos);
	res.l += sz(arbres[a].l) + (arbres[a].rb - arbres[a].lb);
	res.r += sz(arbres[a].l) + (arbres[a].rb - arbres[a].lb);
	return res;
}

void debug(int root) {
	if(root == -1) return;
	debug(arbres[root].l);
	cerr << arbres[root].lb << " " << arbres[root].rb << endl;
	debug(arbres[root].r);
}

void init() {
	vector<int> ouverts;
	
	nextBig = vector<int>(N);
	for(int pos = N - 1;pos >= 0;pos--) {
		while(!ouverts.empty() 
		&& nbs[ouverts.back()] <= nbs[pos])
			ouverts.pop_back();
		
		if(ouverts.empty()) nextBig[pos] = N;
		else nextBig[pos] = ouverts.back();
		
		ouverts.push_back(pos);
	}
	
	int start = 0;
	while(start != N) {
		root = insert(root, start, nextBig[start]);
		start = nextBig[start];
	}
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	
	cin >> N >> Q;
	
	for(int i = 0;i < N;i++) {
		int x;
		cin >> x;
		nbs.push_back(x);
	}
	
	init();
	
	vector<vector<pair<int, int>>> reqs(1 + N);
	for(int i = 0;i < Q;i++) {
		int t, j;
		cin >> t >> j;
		reqs[min(N, t)].push_back({i, j - 1});
	}
	
	vector<int> ans(Q);
	
	for(int t = 0;t <= N;t++) {
		// Answer requests
		
		//cerr << "cur: " << t << endl;
		//debug(root);
		
		for(pair<int, int> req : reqs[t]) {
			//cerr << "req: " << req.second << " " << req.first << endl;
			
			Inter inter = inter_pos(root, req.second);
			//cerr << "ans : " << nbs[inter.li + req.second - inter.l] << endl;
			ans[req.first] = nbs[inter.li + req.second - inter.l];
		}
		
		// Split in the middle & reorder
		Inter spl = inter_pos(root, N / 2);
		
		if(spl.l == N / 2) continue;
		
		root = remove(root, nbs[spl.li]);
		
		vector<pair<int, int>> inters;
		inters.push_back({spl.li, spl.li + (N / 2 - spl.l)});
		
		int start = spl.li + N / 2 - spl.l;
		
		while(start != spl.ri) {
			inters.push_back({
				start, 
				min(nextBig[start], spl.ri)
			});
			start = min(nextBig[start], spl.ri);
		}
		
		for(pair<int, int> inter : inters) {
			root = insert(root, inter.first, inter.second);
		}
	}
	
	for(int i = 0;i < Q;i++) {
		cout << ans[i] << endl;
	}
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1357 ms 26232 KB Output is correct
2 Correct 1443 ms 24672 KB Output is correct
3 Correct 1294 ms 24124 KB Output is correct
4 Correct 1218 ms 21956 KB Output is correct
5 Correct 1343 ms 25936 KB Output is correct
6 Correct 1287 ms 25600 KB Output is correct
7 Correct 1317 ms 27292 KB Output is correct
8 Correct 1282 ms 24628 KB Output is correct
9 Correct 1252 ms 23944 KB Output is correct
10 Correct 1373 ms 23060 KB Output is correct
11 Correct 1232 ms 23468 KB Output is correct
12 Correct 1316 ms 21128 KB Output is correct
13 Correct 1263 ms 22272 KB Output is correct
14 Correct 1329 ms 25128 KB Output is correct
15 Correct 1307 ms 22864 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1349 ms 21244 KB Output is correct
18 Correct 1222 ms 21092 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 2 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1893 ms 54716 KB Output is correct
2 Correct 1677 ms 44172 KB Output is correct
3 Correct 1477 ms 38660 KB Output is correct
4 Correct 1237 ms 36864 KB Output is correct
5 Correct 1291 ms 37736 KB Output is correct
6 Correct 1208 ms 36336 KB Output is correct
7 Correct 1499 ms 40580 KB Output is correct
8 Correct 1438 ms 39220 KB Output is correct
9 Correct 1243 ms 36608 KB Output is correct
10 Correct 1382 ms 36032 KB Output is correct
11 Correct 1139 ms 30444 KB Output is correct
12 Correct 1117 ms 31352 KB Output is correct
13 Correct 1318 ms 35028 KB Output is correct
14 Correct 1113 ms 31668 KB Output is correct
15 Correct 1375 ms 36716 KB Output is correct
16 Correct 32 ms 7912 KB Output is correct
17 Correct 1554 ms 37844 KB Output is correct
18 Correct 1008 ms 28028 KB Output is correct
19 Correct 283 ms 12964 KB Output is correct
20 Correct 313 ms 14468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 348 ms 14240 KB Output is correct
2 Correct 275 ms 10656 KB Output is correct
3 Correct 319 ms 13168 KB Output is correct
4 Correct 170 ms 8120 KB Output is correct
5 Correct 251 ms 10716 KB Output is correct
6 Correct 169 ms 8264 KB Output is correct
7 Correct 225 ms 9580 KB Output is correct
8 Correct 188 ms 8620 KB Output is correct
9 Correct 227 ms 10432 KB Output is correct
10 Correct 147 ms 7096 KB Output is correct
11 Correct 159 ms 7680 KB Output is correct
12 Correct 147 ms 7136 KB Output is correct
13 Correct 152 ms 7252 KB Output is correct
14 Correct 158 ms 7504 KB Output is correct
15 Correct 156 ms 7056 KB Output is correct
16 Correct 21 ms 4116 KB Output is correct
17 Correct 202 ms 9192 KB Output is correct
18 Correct 140 ms 6572 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1357 ms 26232 KB Output is correct
2 Correct 1443 ms 24672 KB Output is correct
3 Correct 1294 ms 24124 KB Output is correct
4 Correct 1218 ms 21956 KB Output is correct
5 Correct 1343 ms 25936 KB Output is correct
6 Correct 1287 ms 25600 KB Output is correct
7 Correct 1317 ms 27292 KB Output is correct
8 Correct 1282 ms 24628 KB Output is correct
9 Correct 1252 ms 23944 KB Output is correct
10 Correct 1373 ms 23060 KB Output is correct
11 Correct 1232 ms 23468 KB Output is correct
12 Correct 1316 ms 21128 KB Output is correct
13 Correct 1263 ms 22272 KB Output is correct
14 Correct 1329 ms 25128 KB Output is correct
15 Correct 1307 ms 22864 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1349 ms 21244 KB Output is correct
18 Correct 1222 ms 21092 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 2 ms 340 KB Output is correct
21 Correct 1893 ms 54716 KB Output is correct
22 Correct 1677 ms 44172 KB Output is correct
23 Correct 1477 ms 38660 KB Output is correct
24 Correct 1237 ms 36864 KB Output is correct
25 Correct 1291 ms 37736 KB Output is correct
26 Correct 1208 ms 36336 KB Output is correct
27 Correct 1499 ms 40580 KB Output is correct
28 Correct 1438 ms 39220 KB Output is correct
29 Correct 1243 ms 36608 KB Output is correct
30 Correct 1382 ms 36032 KB Output is correct
31 Correct 1139 ms 30444 KB Output is correct
32 Correct 1117 ms 31352 KB Output is correct
33 Correct 1318 ms 35028 KB Output is correct
34 Correct 1113 ms 31668 KB Output is correct
35 Correct 1375 ms 36716 KB Output is correct
36 Correct 32 ms 7912 KB Output is correct
37 Correct 1554 ms 37844 KB Output is correct
38 Correct 1008 ms 28028 KB Output is correct
39 Correct 283 ms 12964 KB Output is correct
40 Correct 313 ms 14468 KB Output is correct
41 Correct 348 ms 14240 KB Output is correct
42 Correct 275 ms 10656 KB Output is correct
43 Correct 319 ms 13168 KB Output is correct
44 Correct 170 ms 8120 KB Output is correct
45 Correct 251 ms 10716 KB Output is correct
46 Correct 169 ms 8264 KB Output is correct
47 Correct 225 ms 9580 KB Output is correct
48 Correct 188 ms 8620 KB Output is correct
49 Correct 227 ms 10432 KB Output is correct
50 Correct 147 ms 7096 KB Output is correct
51 Correct 159 ms 7680 KB Output is correct
52 Correct 147 ms 7136 KB Output is correct
53 Correct 152 ms 7252 KB Output is correct
54 Correct 158 ms 7504 KB Output is correct
55 Correct 156 ms 7056 KB Output is correct
56 Correct 21 ms 4116 KB Output is correct
57 Correct 202 ms 9192 KB Output is correct
58 Correct 140 ms 6572 KB Output is correct
59 Correct 1 ms 212 KB Output is correct
60 Correct 1 ms 212 KB Output is correct
61 Correct 2245 ms 55976 KB Output is correct
62 Correct 2065 ms 49732 KB Output is correct
63 Correct 2070 ms 49352 KB Output is correct
64 Correct 1595 ms 45260 KB Output is correct
65 Correct 1682 ms 47384 KB Output is correct
66 Correct 1677 ms 45208 KB Output is correct
67 Correct 1599 ms 43712 KB Output is correct
68 Correct 1485 ms 42268 KB Output is correct
69 Correct 1555 ms 44588 KB Output is correct
70 Correct 1396 ms 38956 KB Output is correct
71 Correct 1333 ms 40132 KB Output is correct
72 Correct 1359 ms 39960 KB Output is correct
73 Correct 1338 ms 39496 KB Output is correct
74 Correct 1408 ms 41968 KB Output is correct
75 Correct 1431 ms 40284 KB Output is correct
76 Correct 32 ms 7808 KB Output is correct
77 Correct 1694 ms 38032 KB Output is correct
78 Correct 1258 ms 33632 KB Output is correct
79 Correct 1 ms 212 KB Output is correct
80 Correct 1 ms 212 KB Output is correct