Submission #813408

# Submission time Handle Problem Language Result Execution time Memory
813408 2023-08-07T17:20:54 Z prvocislo Comparing Plants (IOI20_plants) C++17
100 / 100
2271 ms 125724 KB
#include "plants.h"
#include <iostream>
#include <algorithm>
#include <set>
#include <string>
#include <vector>
typedef long long ll;
using namespace std;

const int maxn = 1 << 19, logn = 19, inf = 1e9 + 5;
vector<pair<int, int> > mini(maxn * 2, { inf, -1 });
vector<int> l(maxn * 2), r(maxn * 2), lz(maxn * 2, 0);
void add(int li, int ri, int x, int vr = 1)
{
	if (ri < l[vr] || r[vr] < li) return;
	if (li <= l[vr] && r[vr] <= ri) return lz[vr] += x, mini[vr].first += x, void();
	add(li, ri, x, (vr << 1)), add(li, ri, x, (vr << 1) | 1);
	mini[vr] = min(mini[vr << 1], mini[(vr << 1) | 1]), mini[vr].first += lz[vr];
}
int n, k;
pair<int, int> query(int li, int ri, int vr = 1)
{
	if (ri < l[vr] || r[vr] < li) return { inf, -1 };
	if (li <= l[vr] && r[vr] <= ri) return mini[vr];
	return min(query(li, ri, vr << 1), query(li, ri, (vr << 1) | 1));
}
vector<int> ord;
set<int> v0; set<pair<int, int> > ordv; // {vzdialenost od predoslej 0, pozicia}
pair<int, int> give_pair(int x)
{
	if (v0.empty()) return { n, x };
	if (x <= *v0.begin()) return { n + x - *prev(v0.end()), x };
	return { x - *prev(v0.lower_bound(x)), x };
}
void pridaj(int x)
{
	auto nx = v0.upper_bound(x);
	if (!v0.empty() && nx == v0.end()) nx = v0.begin();
	int val = (nx == v0.end() || *nx == x ? -1 : *nx);
	if (val != -1) ordv.erase(give_pair(val));
	v0.insert(x), ordv.insert(give_pair(x));
	if (val != -1) ordv.insert(give_pair(val));
}
void zober(int x)
{
	auto nx = v0.upper_bound(x);
	if (!v0.empty() && nx == v0.end()) nx = v0.begin();
	int val = (nx == v0.end() || *nx == x ? -1 : *nx);
	if (val != -1) ordv.erase(give_pair(val));
	ordv.erase(give_pair(x)), v0.erase(x);
	if (val != -1) ordv.insert(give_pair(val));
}
vector<vector<int> > hl(logn, vector<int>(maxn, 0)), hr(logn, vector<int>(maxn, 0));
void init(int K, vector<int> h)
{
	n = h.size(), k = K;
	ord.resize(n, -1);
	for (int i = 0; i < n; i++)
	{
		if (h[i]) mini[maxn + i] = { h[i], i };
		else pridaj(i);
	}
	for (int i = maxn; i < maxn * 2; i++) l[i] = r[i] = i - maxn;
	for (int i = maxn - 1; i; i--) l[i] = l[i * 2], r[i] = r[i * 2 + 1], mini[i] = min(mini[i * 2], mini[i * 2 + 1]);
	for (int i = n - 1; i >= 0; i--)
	{
		int id = prev(ordv.end())->second;
		ord[id] = i;
		zober(id);
		add(id - (k - 1), id - 1, -1), add(n + id - (k - 1), n - 1, -1);
		while (mini[1].first == 0)
		{
			int x = mini[1].second;
			add(x, x, inf), pridaj(x);
		}
	}
	vector<pair<int, int> > pr;
	for (int i = 0; i < maxn * 2; i++) lz[i] = 0;
	for (int i = 0; i < 2 * n; i++) mini[maxn + i] = { ord[i % n], i }, pr.push_back({ ord[i % n], i });
	for (int i = maxn - 1; i; i--) mini[i] = min(mini[i << 1], mini[(i << 1) | 1]);
	sort(pr.begin(), pr.end());
	for (pair<int, int> pp : pr)
	{
		int i = pp.second;
		hl[0][i] = query(i - k + 1, i - 1).second, hr[0][i] = query(i + 1, i + k - 1).second;
		if (hl[0][i] == -1) hl[0][i] = i;
		if (hr[0][i] == -1) hr[0][i] = i;
		add(i, i, inf);
	}
	for (int j = 1; j < logn; j++) for (int i = 0; i < 2 * n; i++) hr[j][i] = hr[j - 1][hr[j - 1][i]], hl[j][i] = hl[j - 1][hl[j - 1][i]];
}
int higher(int x, int y) // plati x < y
{
	int x2 = x;
	for (int j = logn - 1; j >= 0; j--) if (hr[j][x] <= y) x = hr[j][x];
	if (ord[x % n] <= ord[y % n] && abs(y - x) < k) return 1;
	x = x2;
	for (int j = logn - 1; j >= 0; j--) if (hl[j][y] >= x) y = hl[j][y];
	if (ord[x % n] >= ord[y % n] && abs(y - x) < k) return -1;
	return 0;
}
int compare_plants(int x, int y)
{
	int h1 = higher(y, x + n), h2 = -higher(x, y);
	if (h1 != 0) return h1;
	if (h2 != 0) return h2;
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 41 ms 100808 KB Output is correct
2 Correct 48 ms 100912 KB Output is correct
3 Correct 43 ms 100820 KB Output is correct
4 Correct 42 ms 100836 KB Output is correct
5 Correct 42 ms 100872 KB Output is correct
6 Correct 138 ms 102136 KB Output is correct
7 Correct 278 ms 105108 KB Output is correct
8 Correct 1435 ms 115468 KB Output is correct
9 Correct 1291 ms 115128 KB Output is correct
10 Correct 1248 ms 115144 KB Output is correct
11 Correct 1205 ms 116184 KB Output is correct
12 Correct 1171 ms 115924 KB Output is correct
13 Correct 1408 ms 124736 KB Output is correct
14 Correct 872 ms 113016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 100932 KB Output is correct
2 Correct 43 ms 100924 KB Output is correct
3 Correct 54 ms 100900 KB Output is correct
4 Correct 44 ms 100916 KB Output is correct
5 Correct 43 ms 100848 KB Output is correct
6 Correct 48 ms 100940 KB Output is correct
7 Correct 229 ms 104012 KB Output is correct
8 Correct 45 ms 100868 KB Output is correct
9 Correct 47 ms 100884 KB Output is correct
10 Correct 239 ms 103984 KB Output is correct
11 Correct 289 ms 103972 KB Output is correct
12 Correct 208 ms 104116 KB Output is correct
13 Correct 243 ms 104008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 100932 KB Output is correct
2 Correct 43 ms 100924 KB Output is correct
3 Correct 54 ms 100900 KB Output is correct
4 Correct 44 ms 100916 KB Output is correct
5 Correct 43 ms 100848 KB Output is correct
6 Correct 48 ms 100940 KB Output is correct
7 Correct 229 ms 104012 KB Output is correct
8 Correct 45 ms 100868 KB Output is correct
9 Correct 47 ms 100884 KB Output is correct
10 Correct 239 ms 103984 KB Output is correct
11 Correct 289 ms 103972 KB Output is correct
12 Correct 208 ms 104116 KB Output is correct
13 Correct 243 ms 104008 KB Output is correct
14 Correct 351 ms 105020 KB Output is correct
15 Correct 2084 ms 114816 KB Output is correct
16 Correct 320 ms 105032 KB Output is correct
17 Correct 1898 ms 114868 KB Output is correct
18 Correct 1661 ms 116716 KB Output is correct
19 Correct 1314 ms 114748 KB Output is correct
20 Correct 1959 ms 114868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 48 ms 100924 KB Output is correct
2 Correct 50 ms 100808 KB Output is correct
3 Correct 194 ms 103832 KB Output is correct
4 Correct 1238 ms 115892 KB Output is correct
5 Correct 1535 ms 114332 KB Output is correct
6 Correct 1848 ms 114320 KB Output is correct
7 Correct 2185 ms 114520 KB Output is correct
8 Correct 2127 ms 114712 KB Output is correct
9 Correct 1507 ms 114384 KB Output is correct
10 Correct 1450 ms 114436 KB Output is correct
11 Correct 1353 ms 125724 KB Output is correct
12 Correct 1025 ms 114148 KB Output is correct
13 Correct 1683 ms 120316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 49 ms 100828 KB Output is correct
2 Correct 51 ms 100920 KB Output is correct
3 Correct 50 ms 100880 KB Output is correct
4 Correct 52 ms 100868 KB Output is correct
5 Correct 49 ms 100808 KB Output is correct
6 Correct 54 ms 100944 KB Output is correct
7 Correct 71 ms 101292 KB Output is correct
8 Correct 68 ms 101328 KB Output is correct
9 Correct 72 ms 101308 KB Output is correct
10 Correct 69 ms 101256 KB Output is correct
11 Correct 71 ms 101236 KB Output is correct
12 Correct 70 ms 101340 KB Output is correct
13 Correct 69 ms 101400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 48 ms 100840 KB Output is correct
2 Correct 50 ms 100820 KB Output is correct
3 Correct 50 ms 100888 KB Output is correct
4 Correct 49 ms 100944 KB Output is correct
5 Correct 51 ms 100900 KB Output is correct
6 Correct 1031 ms 113320 KB Output is correct
7 Correct 1222 ms 113388 KB Output is correct
8 Correct 1485 ms 113704 KB Output is correct
9 Correct 1584 ms 113840 KB Output is correct
10 Correct 999 ms 113580 KB Output is correct
11 Correct 1307 ms 114068 KB Output is correct
12 Correct 979 ms 114992 KB Output is correct
13 Correct 1089 ms 113460 KB Output is correct
14 Correct 1207 ms 113452 KB Output is correct
15 Correct 1574 ms 113608 KB Output is correct
16 Correct 802 ms 114196 KB Output is correct
17 Correct 1031 ms 113380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 100808 KB Output is correct
2 Correct 48 ms 100912 KB Output is correct
3 Correct 43 ms 100820 KB Output is correct
4 Correct 42 ms 100836 KB Output is correct
5 Correct 42 ms 100872 KB Output is correct
6 Correct 138 ms 102136 KB Output is correct
7 Correct 278 ms 105108 KB Output is correct
8 Correct 1435 ms 115468 KB Output is correct
9 Correct 1291 ms 115128 KB Output is correct
10 Correct 1248 ms 115144 KB Output is correct
11 Correct 1205 ms 116184 KB Output is correct
12 Correct 1171 ms 115924 KB Output is correct
13 Correct 1408 ms 124736 KB Output is correct
14 Correct 872 ms 113016 KB Output is correct
15 Correct 42 ms 100932 KB Output is correct
16 Correct 43 ms 100924 KB Output is correct
17 Correct 54 ms 100900 KB Output is correct
18 Correct 44 ms 100916 KB Output is correct
19 Correct 43 ms 100848 KB Output is correct
20 Correct 48 ms 100940 KB Output is correct
21 Correct 229 ms 104012 KB Output is correct
22 Correct 45 ms 100868 KB Output is correct
23 Correct 47 ms 100884 KB Output is correct
24 Correct 239 ms 103984 KB Output is correct
25 Correct 289 ms 103972 KB Output is correct
26 Correct 208 ms 104116 KB Output is correct
27 Correct 243 ms 104008 KB Output is correct
28 Correct 351 ms 105020 KB Output is correct
29 Correct 2084 ms 114816 KB Output is correct
30 Correct 320 ms 105032 KB Output is correct
31 Correct 1898 ms 114868 KB Output is correct
32 Correct 1661 ms 116716 KB Output is correct
33 Correct 1314 ms 114748 KB Output is correct
34 Correct 1959 ms 114868 KB Output is correct
35 Correct 48 ms 100924 KB Output is correct
36 Correct 50 ms 100808 KB Output is correct
37 Correct 194 ms 103832 KB Output is correct
38 Correct 1238 ms 115892 KB Output is correct
39 Correct 1535 ms 114332 KB Output is correct
40 Correct 1848 ms 114320 KB Output is correct
41 Correct 2185 ms 114520 KB Output is correct
42 Correct 2127 ms 114712 KB Output is correct
43 Correct 1507 ms 114384 KB Output is correct
44 Correct 1450 ms 114436 KB Output is correct
45 Correct 1353 ms 125724 KB Output is correct
46 Correct 1025 ms 114148 KB Output is correct
47 Correct 1683 ms 120316 KB Output is correct
48 Correct 49 ms 100828 KB Output is correct
49 Correct 51 ms 100920 KB Output is correct
50 Correct 50 ms 100880 KB Output is correct
51 Correct 52 ms 100868 KB Output is correct
52 Correct 49 ms 100808 KB Output is correct
53 Correct 54 ms 100944 KB Output is correct
54 Correct 71 ms 101292 KB Output is correct
55 Correct 68 ms 101328 KB Output is correct
56 Correct 72 ms 101308 KB Output is correct
57 Correct 69 ms 101256 KB Output is correct
58 Correct 71 ms 101236 KB Output is correct
59 Correct 70 ms 101340 KB Output is correct
60 Correct 69 ms 101400 KB Output is correct
61 Correct 214 ms 103836 KB Output is correct
62 Correct 343 ms 105024 KB Output is correct
63 Correct 1831 ms 114936 KB Output is correct
64 Correct 1178 ms 114116 KB Output is correct
65 Correct 2271 ms 114372 KB Output is correct
66 Correct 2025 ms 114512 KB Output is correct
67 Correct 2136 ms 114712 KB Output is correct
68 Correct 1457 ms 114372 KB Output is correct
69 Correct 1800 ms 115204 KB Output is correct
70 Correct 1305 ms 115952 KB Output is correct
71 Correct 1511 ms 114256 KB Output is correct
72 Correct 1900 ms 114316 KB Output is correct
73 Correct 2088 ms 114516 KB Output is correct
74 Correct 1592 ms 114132 KB Output is correct
75 Correct 1220 ms 114280 KB Output is correct