Submission #761027

# Submission time Handle Problem Language Result Execution time Memory
761027 2023-06-19T04:59:14 Z SanguineChameleon Dancing Elephants (IOI11_elephants) C++17
100 / 100
1791 ms 14228 KB
#include "elephants.h"
#include <bits/stdc++.h>
using namespace std;

const int maxN = 1.5e5 + 20;
const int block_size = 400;
vector<int> pos[maxN];
vector<pair<int, int>> dp[maxN];
int A[maxN];
int N, L;
int block_cnt;

void calc_block(int id) {
	vector<int> &pos_block = pos[id];
	vector<pair<int, int>> &dp_block = dp[id];
	int sz = pos_block.size();
	dp_block.resize(sz);
	int rt = sz;
	for (int i = sz - 1; i >= 0; i--) {
		while (pos_block[rt - 1] > pos_block[i] + L) {
			rt--;
		}
		if (rt == sz) {
			dp_block[i].first = 1;
			dp_block[i].second = pos_block[i];
		}
		else {
			dp_block[i].first = dp_block[rt].first + 1;
			dp_block[i].second = dp_block[rt].second;
		}
	}
}

int calc_all() {
	int res = dp[0][0].first;
	int last = dp[0][0].second;
	for (int id = 1; id < block_cnt; id++) {
		if (pos[id].back() > last + L) {
			int i = upper_bound(pos[id].begin(), pos[id].end(), last + L) - pos[id].begin();
			res += dp[id][i].first;
			last = dp[id][i].second;
		}
	}
	return res;
}

void merge_blocks() {
	for (int id = 0; id < block_cnt - 1; id++) {
		if ((int)pos[id].size() + (int)pos[id + 1].size() < block_size * 2) {
			pos[id].insert(pos[id].end(), pos[id + 1].begin(), pos[id + 1].end());
			pos[id + 1].clear();
			dp[id + 1].clear();
			for (int i = id + 2; i < block_cnt; i++) {
				swap(pos[i - 1], pos[i]);
				swap(dp[i - 1], dp[i]);
			}
			block_cnt--;
			calc_block(id);
			merge_blocks();
			return;
		}
	}
}

void rem(int X) {
	int id = 0;
	while (pos[id].back() < X) {
		id++;
	}
	pos[id].erase(lower_bound(pos[id].begin(), pos[id].end(), X));
	if (pos[id].empty()) {
		for (int i = id + 1; i < block_cnt; i++) {
			swap(pos[i - 1], pos[i]);
			swap(dp[i - 1], dp[i]);
		}
		block_cnt--;
	}
	else {
		calc_block(id);
	}
	merge_blocks();
}

void add(int X) {
	int id = 0;
	while (id < block_cnt - 1 && pos[id].back() < X) {
		id++;
	}
	pos[id].insert(upper_bound(pos[id].begin(), pos[id].end(), X), X);
	if ((int)pos[id].size() == block_size * 2) {
		for (int i = block_cnt - 1; i > id; i--) {
			swap(pos[i + 1], pos[i]);
			swap(dp[i + 1], dp[i]);
		}
		pos[id + 1].insert(pos[id + 1].begin(), pos[id].begin() + block_size, pos[id].end());
		pos[id].erase(pos[id].begin() + block_size, pos[id].end());
		block_cnt++;
		calc_block(id);
		calc_block(id + 1);
	}
	else {
		calc_block(id);
	}
	merge_blocks();
}

void init(int _N, int _L, int _A[]) {
	N = _N;
	L = _L;
	block_cnt = (N - 1) / block_size + 1;
	for (int i = 0; i < N; i++) {
		A[i] = _A[i];
		pos[i / block_size].push_back(A[i]);
	}
	for (int id = 0; id < block_cnt; id++) {
		calc_block(id);
	}
}

int update(int i, int X) {
	rem(A[i]);
	A[i] = X;
	add(A[i]);
	return calc_all();
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7380 KB Output is correct
2 Correct 3 ms 7380 KB Output is correct
3 Correct 3 ms 7380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7380 KB Output is correct
2 Correct 3 ms 7380 KB Output is correct
3 Correct 3 ms 7380 KB Output is correct
4 Correct 3 ms 7380 KB Output is correct
5 Correct 3 ms 7380 KB Output is correct
6 Correct 3 ms 7380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7380 KB Output is correct
2 Correct 3 ms 7380 KB Output is correct
3 Correct 3 ms 7380 KB Output is correct
4 Correct 3 ms 7380 KB Output is correct
5 Correct 3 ms 7380 KB Output is correct
6 Correct 3 ms 7380 KB Output is correct
7 Correct 152 ms 8368 KB Output is correct
8 Correct 154 ms 8524 KB Output is correct
9 Correct 248 ms 9584 KB Output is correct
10 Correct 211 ms 9548 KB Output is correct
11 Correct 168 ms 9548 KB Output is correct
12 Correct 359 ms 9548 KB Output is correct
13 Correct 192 ms 9588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7380 KB Output is correct
2 Correct 3 ms 7380 KB Output is correct
3 Correct 3 ms 7380 KB Output is correct
4 Correct 3 ms 7380 KB Output is correct
5 Correct 3 ms 7380 KB Output is correct
6 Correct 3 ms 7380 KB Output is correct
7 Correct 152 ms 8368 KB Output is correct
8 Correct 154 ms 8524 KB Output is correct
9 Correct 248 ms 9584 KB Output is correct
10 Correct 211 ms 9548 KB Output is correct
11 Correct 168 ms 9548 KB Output is correct
12 Correct 359 ms 9548 KB Output is correct
13 Correct 192 ms 9588 KB Output is correct
14 Correct 214 ms 8704 KB Output is correct
15 Correct 185 ms 8940 KB Output is correct
16 Correct 702 ms 9868 KB Output is correct
17 Correct 659 ms 10500 KB Output is correct
18 Correct 710 ms 10396 KB Output is correct
19 Correct 274 ms 10432 KB Output is correct
20 Correct 639 ms 10496 KB Output is correct
21 Correct 553 ms 10432 KB Output is correct
22 Correct 317 ms 10512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7380 KB Output is correct
2 Correct 3 ms 7380 KB Output is correct
3 Correct 3 ms 7380 KB Output is correct
4 Correct 3 ms 7380 KB Output is correct
5 Correct 3 ms 7380 KB Output is correct
6 Correct 3 ms 7380 KB Output is correct
7 Correct 152 ms 8368 KB Output is correct
8 Correct 154 ms 8524 KB Output is correct
9 Correct 248 ms 9584 KB Output is correct
10 Correct 211 ms 9548 KB Output is correct
11 Correct 168 ms 9548 KB Output is correct
12 Correct 359 ms 9548 KB Output is correct
13 Correct 192 ms 9588 KB Output is correct
14 Correct 214 ms 8704 KB Output is correct
15 Correct 185 ms 8940 KB Output is correct
16 Correct 702 ms 9868 KB Output is correct
17 Correct 659 ms 10500 KB Output is correct
18 Correct 710 ms 10396 KB Output is correct
19 Correct 274 ms 10432 KB Output is correct
20 Correct 639 ms 10496 KB Output is correct
21 Correct 553 ms 10432 KB Output is correct
22 Correct 317 ms 10512 KB Output is correct
23 Correct 1478 ms 14096 KB Output is correct
24 Correct 1652 ms 14008 KB Output is correct
25 Correct 1319 ms 13988 KB Output is correct
26 Correct 1045 ms 14228 KB Output is correct
27 Correct 998 ms 13996 KB Output is correct
28 Correct 984 ms 9276 KB Output is correct
29 Correct 882 ms 9272 KB Output is correct
30 Correct 981 ms 9272 KB Output is correct
31 Correct 913 ms 9268 KB Output is correct
32 Correct 861 ms 14164 KB Output is correct
33 Correct 811 ms 14180 KB Output is correct
34 Correct 1083 ms 14052 KB Output is correct
35 Correct 832 ms 12364 KB Output is correct
36 Correct 1094 ms 12236 KB Output is correct
37 Correct 1106 ms 14056 KB Output is correct
38 Correct 1065 ms 14040 KB Output is correct
39 Correct 932 ms 13912 KB Output is correct
40 Correct 1177 ms 14148 KB Output is correct
41 Correct 1695 ms 14136 KB Output is correct
42 Correct 1791 ms 14100 KB Output is correct