Submission #947324

# Submission time Handle Problem Language Result Execution time Memory
947324 2024-03-15T21:59:38 Z MinaRagy06 Dancing Elephants (IOI11_elephants) C++17
100 / 100
2589 ms 23136 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define SZ(x) (int) x.size()

int n, l;
const int N = 150'005, B = 400;
struct block {
	int n;
	vector<int> v;
	vector<array<int, 2>> dp;
	void build() {
		n = v.size();
		dp.resize(n);
		int p = n;
		for (int i = n - 1; i >= 0; i--) {
			while (p - 1 >= 0 && v[p - 1] >= v[i] + l) {
				p--;
			}
			if (v[i] + l <= v.back()) {
				dp[i] = {dp[p][0] + 1, dp[p][1]};
			} else {
				dp[i] = {1, v[i] + l};
			}
		}
	}
	void insert(int x) {
		v.push_back(x);
		for (int i = v.size() - 1; i - 1 >= 0; i--) {
			if (v[i] <= v[i - 1]) {
				swap(v[i], v[i - 1]);
			} else {
				break;
			}
		}
		build();
	}
	void erase(int x) {
		bool done = 0;
		vector<int> newv;
		for (int i = 0; i < SZ(v); i++) {
			if (!done && v[i] == x) {
				done = 1;
				continue;
			}
			newv.push_back(v[i]);
		}
		v = newv;
		build();
	}
	block(vector<int> &_v) {
		v = _v;
		n = v.size();
		build();
	}
};
vector<block> b;
void insert(int x) {
	for (int i = 0; i < SZ(b); i++) {
		if (i == SZ(b) - 1 || (b[i].v.back() <= x && x <= b[i + 1].v[0]) || (x <= b[i].v.back())) {
			b[i].insert(x);
			break;
		}
	}
}
void erase(int x) {
	for (int i = 0; i < SZ(b); i++) {
		if (b[i].v.empty()) continue;
		if (b[i].v[0] <= x && x <= b[i].v.back()) {
			b[i].erase(x);
			break;
		}
	}
}
int query() {
	int ans = 0, prv = 0;
	for (int i = 0; i < SZ(b); i++) {
		if (b[i].v.empty()) continue;
		if (prv <= b[i].v.back()) {
			int p = lower_bound(b[i].v.begin(), b[i].v.end(), prv) - b[i].v.begin();
			ans += b[i].dp[p][0];
			prv = b[i].dp[p][1];
		}
	}
	return ans;
}
void build(vector<int> &a) {
	vector<int> cur;
	for (int i = 0; i < n; i++) {
		if (cur.size() == B) {
			b.push_back(cur);
			cur.clear();
		}
		cur.push_back(a[i]);
	}
	b.push_back(cur);	
}
void rebuild() {
	vector<int> cur;
	for (int i = 0; i < SZ(b); i++) {
		for (auto j : b[i].v) {
			cur.push_back(j);
		}
	}
	b.clear();
	build(cur);
}
vector<int> a;
void init(int _n, int _l, int _a[]) {
	n = _n, l = _l + 1;
	a.resize(n);
	for (int i = 0; i < n; i++) {
		a[i] = _a[i];
	}
	build(a);
}
int cnt = 0;
int update(int x, int y) {
	if (cnt == B) {
		rebuild();
		cnt = 0;
	}
	erase(a[x]);
	a[x] = y;
	insert(y);
	cnt++;
	return query();
}
#ifdef MINA
int main() {
	ios_base::sync_with_stdio(0), cin.tie(0);
	int _n, _l;
	cin >> _n >> _l;
	int _a[_n];
	for (int i = 0; i < _n; i++) {
		cin >> _a[i];
	}
	init(_n, _l, _a);
	int _q;
	cin >> _q;
	while (_q--) {
		int _i, _y;
		cin >> _i >> _y;
		cout << update(_i, _y) << '\n';
	}
	return 0;
}
#endif
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6744 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6744 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 221 ms 9136 KB Output is correct
8 Correct 231 ms 9308 KB Output is correct
9 Correct 253 ms 10060 KB Output is correct
10 Correct 240 ms 10064 KB Output is correct
11 Correct 242 ms 10148 KB Output is correct
12 Correct 437 ms 10488 KB Output is correct
13 Correct 261 ms 10440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6744 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 221 ms 9136 KB Output is correct
8 Correct 231 ms 9308 KB Output is correct
9 Correct 253 ms 10060 KB Output is correct
10 Correct 240 ms 10064 KB Output is correct
11 Correct 242 ms 10148 KB Output is correct
12 Correct 437 ms 10488 KB Output is correct
13 Correct 261 ms 10440 KB Output is correct
14 Correct 260 ms 9484 KB Output is correct
15 Correct 362 ms 9416 KB Output is correct
16 Correct 779 ms 10568 KB Output is correct
17 Correct 784 ms 11372 KB Output is correct
18 Correct 857 ms 11464 KB Output is correct
19 Correct 423 ms 10732 KB Output is correct
20 Correct 805 ms 11792 KB Output is correct
21 Correct 745 ms 11404 KB Output is correct
22 Correct 439 ms 10728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6744 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 221 ms 9136 KB Output is correct
8 Correct 231 ms 9308 KB Output is correct
9 Correct 253 ms 10060 KB Output is correct
10 Correct 240 ms 10064 KB Output is correct
11 Correct 242 ms 10148 KB Output is correct
12 Correct 437 ms 10488 KB Output is correct
13 Correct 261 ms 10440 KB Output is correct
14 Correct 260 ms 9484 KB Output is correct
15 Correct 362 ms 9416 KB Output is correct
16 Correct 779 ms 10568 KB Output is correct
17 Correct 784 ms 11372 KB Output is correct
18 Correct 857 ms 11464 KB Output is correct
19 Correct 423 ms 10732 KB Output is correct
20 Correct 805 ms 11792 KB Output is correct
21 Correct 745 ms 11404 KB Output is correct
22 Correct 439 ms 10728 KB Output is correct
23 Correct 1966 ms 17268 KB Output is correct
24 Correct 2169 ms 17124 KB Output is correct
25 Correct 1449 ms 17160 KB Output is correct
26 Correct 1820 ms 16980 KB Output is correct
27 Correct 1755 ms 22148 KB Output is correct
28 Correct 1136 ms 16040 KB Output is correct
29 Correct 1066 ms 16208 KB Output is correct
30 Correct 1166 ms 16048 KB Output is correct
31 Correct 1095 ms 16216 KB Output is correct
32 Correct 1348 ms 21504 KB Output is correct
33 Correct 1204 ms 21044 KB Output is correct
34 Correct 1523 ms 21696 KB Output is correct
35 Correct 1172 ms 22156 KB Output is correct
36 Correct 1069 ms 21412 KB Output is correct
37 Correct 1878 ms 22832 KB Output is correct
38 Correct 1614 ms 21080 KB Output is correct
39 Correct 1462 ms 22004 KB Output is correct
40 Correct 1672 ms 20788 KB Output is correct
41 Correct 2549 ms 22700 KB Output is correct
42 Correct 2589 ms 23136 KB Output is correct