Submission #956407

# Submission time Handle Problem Language Result Execution time Memory
956407 2024-04-01T18:50:47 Z MinaRagy06 The Potion of Great Power (CEOI20_potion) C++17
100 / 100
2778 ms 257772 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define sz(x) (int) x.size()

const int N = 100'005, D = 505;
vector<vector<array<int, 2>>> gud[N];
vector<int> rem[N];
int mx[N];
set<array<int, 2>> adj[N];
int a[N], n, m;
void init(int _n, int d, int h[]) {
	n = _n;
	for (int i = 0; i < n; i++) {
		for (int j = 499; j >= 0; j--) {
			rem[i].push_back(j);
		}
		a[i] = h[i];
	}
}
void upd(int i, int j, int v, int t) {
	gud[i][j].push_back({v, t});
}
void curseChanges(int _m, int A[], int B[]) {
	m = _m;
	for (int i = 0; i < m; i++) {
		int u = A[i], v = B[i];
		auto it = adj[u].lower_bound({v, 0});
		if (it != adj[u].end() && (*it)[0] == v) {
			int x = (*it)[1];
			rem[u].push_back(x);
			adj[u].erase({v, x});

			it = adj[v].lower_bound({u, 0});
			x = (*it)[1];
			rem[v].push_back(x);
			adj[v].erase({u, x});
		} else {
			int x = rem[u].back();
			rem[u].pop_back();
			adj[u].insert({v, x});
			mx[u] = max(mx[u], x + 1);

			x = rem[v].back();
			rem[v].pop_back();
			adj[v].insert({u, x});
			mx[v] = max(mx[v], x + 1);
		}
	}
	for (int i = 0; i < n; i++) {
		adj[i].clear();
		rem[i].clear();
		for (int j = mx[i] - 1; j >= 0; j--) {
			rem[i].push_back(j);
		}
		gud[i].resize(mx[i]);
	}

	for (int i = 0; i < m; i++) {
		int u = A[i], v = B[i];
		auto it = adj[u].lower_bound({v, 0});
		if (it != adj[u].end() && (*it)[0] == v) {
			int x = (*it)[1];
			upd(u, x, -1, i + 1);
			rem[u].push_back(x);
			adj[u].erase({v, x});

			it = adj[v].lower_bound({u, 0});
			x = (*it)[1];
			upd(v, x, -1, i + 1);
			rem[v].push_back(x);
			adj[v].erase({u, x});
		} else {
			int x = rem[u].back();
			upd(u, x, a[v], i + 1);
			rem[u].pop_back();
			adj[u].insert({v, x});

			x = rem[v].back();
			upd(v, x, a[u], i + 1);
			rem[v].pop_back();
			adj[v].insert({u, x});
		}
	}
}
int question(int x, int y, int v) {
	vector<int> v1, v2;
	for (int j = 0; j < max(mx[x], mx[y]); j++) {
		if (j < mx[x]) {
			int l = 0, r = sz(gud[x][j]) - 1;
			while (l <= r) {
				int md = ((l + r) >> 1);
				if (gud[x][j][md][1] <= v) {
					l = md + 1;
				} else {
					r = md - 1;
				}
			}
			if (r >= 0 && gud[x][j][r][0] != -1) {
				v1.push_back(gud[x][j][r][0]);
			}
		}
		if (j < mx[y]) {
			int l = 0, r = sz(gud[y][j]) - 1;
			while (l <= r) {
				int md = ((l + r) >> 1);
				if (gud[y][j][md][1] <= v) {
					l = md + 1;
				} else {
					r = md - 1;
				}
			}
			if (r >= 0 && gud[y][j][r][0] != -1) {
				v2.push_back(gud[y][j][r][0]);
			}
		}
	}
	int ans = 1e9;
	if (v1.empty() || v2.empty()) {
		return ans;
	}
	sort(v1.begin(), v1.end());
	sort(v2.begin(), v2.end());
	int ptr = 0;
	for (int i = 0; i < sz(v1); i++) {
		while (ptr + 1 < sz(v2) && v2[ptr + 1] <= v1[i]) {
			ptr++;
		}
		ans = min(ans, abs(v1[i] - v2[ptr]));
		if (ptr + 1 < sz(v2)) {
			ans = min(ans, abs(v1[i] - v2[ptr + 1]));
		}
	}
	return ans;
}
#ifdef MINA
int main() {
	ios_base::sync_with_stdio(0), cin.tie(0);
	int _N = 6, _D = 5, U = 11;
	int H[] = {2, 42, 1000, 54, 68, 234};
	int A[] = {0, 2, 3, 3, 3, 1, 5, 0, 3, 1, 3};
	int B[] = {1, 0, 4, 5, 5, 3, 3, 5, 0, 3, 5};
	init(_N, _D, H);
	curseChanges(U, A, B);
	cout << question(0, 3, 4) << '\n';
	cout << question(3, 0, 8) << '\n';
	cout << question(0, 5, 5) << '\n';
	cout << question(3, 0, 11) << '\n';
	return 0;
}
#endif
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 12632 KB Output is correct
2 Correct 6 ms 12696 KB Output is correct
3 Correct 6 ms 12632 KB Output is correct
4 Correct 237 ms 213100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 949 ms 257772 KB Output is correct
2 Correct 1020 ms 257624 KB Output is correct
3 Correct 440 ms 220692 KB Output is correct
4 Correct 1827 ms 36028 KB Output is correct
5 Correct 915 ms 72840 KB Output is correct
6 Correct 2778 ms 231936 KB Output is correct
7 Correct 888 ms 232188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 969 ms 257548 KB Output is correct
2 Correct 1708 ms 222064 KB Output is correct
3 Correct 1281 ms 231960 KB Output is correct
4 Correct 1869 ms 232108 KB Output is correct
5 Correct 968 ms 256832 KB Output is correct
6 Correct 2025 ms 232340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 62 ms 32808 KB Output is correct
2 Correct 100 ms 31536 KB Output is correct
3 Correct 170 ms 31216 KB Output is correct
4 Correct 694 ms 32052 KB Output is correct
5 Correct 607 ms 32480 KB Output is correct
6 Correct 80 ms 14360 KB Output is correct
7 Correct 687 ms 31032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10328 KB Output is correct
2 Correct 6 ms 12632 KB Output is correct
3 Correct 6 ms 12696 KB Output is correct
4 Correct 6 ms 12632 KB Output is correct
5 Correct 237 ms 213100 KB Output is correct
6 Correct 949 ms 257772 KB Output is correct
7 Correct 1020 ms 257624 KB Output is correct
8 Correct 440 ms 220692 KB Output is correct
9 Correct 1827 ms 36028 KB Output is correct
10 Correct 915 ms 72840 KB Output is correct
11 Correct 2778 ms 231936 KB Output is correct
12 Correct 888 ms 232188 KB Output is correct
13 Correct 969 ms 257548 KB Output is correct
14 Correct 1708 ms 222064 KB Output is correct
15 Correct 1281 ms 231960 KB Output is correct
16 Correct 1869 ms 232108 KB Output is correct
17 Correct 968 ms 256832 KB Output is correct
18 Correct 2025 ms 232340 KB Output is correct
19 Correct 62 ms 32808 KB Output is correct
20 Correct 100 ms 31536 KB Output is correct
21 Correct 170 ms 31216 KB Output is correct
22 Correct 694 ms 32052 KB Output is correct
23 Correct 607 ms 32480 KB Output is correct
24 Correct 80 ms 14360 KB Output is correct
25 Correct 687 ms 31032 KB Output is correct
26 Correct 965 ms 222700 KB Output is correct
27 Correct 1574 ms 232564 KB Output is correct
28 Correct 1830 ms 246244 KB Output is correct
29 Correct 1544 ms 36284 KB Output is correct
30 Correct 2764 ms 232196 KB Output is correct
31 Correct 2561 ms 222132 KB Output is correct
32 Correct 2469 ms 232204 KB Output is correct