Submission #1072684

# Submission time Handle Problem Language Result Execution time Memory
1072684 2024-08-24T02:49:20 Z pavement Land of the Rainbow Gold (APIO17_rainbow) C++17
100 / 100
634 ms 194748 KB
#include "rainbow.h"
#include <bits/stdc++.h>
using namespace std;

using ii = pair<int, int>;

#define pb push_back
#define eb emplace_back

vector<int> row[200005], col[200005];
vector<ii> nodes, cells, horiz, vert;
int ptr;

struct node {
	node *left, *right;
	int S, E;
	vector<int> val;
	node(int _s, int _e, vector<ii> &vec) : S(_s), E(_e) {
		if (S == E) {
			while (ptr < (int)vec.size() && vec[ptr].first == S) {
				val.pb(vec[ptr].second);
				ptr++;
			}
			return;
		}
		int M = (S + E) / 2;
		left = new node(S, M, vec);
		right = new node(M + 1, E, vec);
		merge(left->val.begin(), left->val.end(), right->val.begin(), right->val.end(), back_inserter(val));
	}
	int qry(int l, int r, int x, int y) {
		if (l > E || r < S) {
			return 0;
		}
		if (l <= S && E <= r) {
			return upper_bound(val.begin(), val.end(), y) - lower_bound(val.begin(), val.end(), x);
		}
		return left->qry(l, r, x, y) + right->qry(l, r, x, y);
	}
} *root_nodes, *root_cells, *root_horiz, *root_vert;

void init(int R, int C, int sr, int sc, int M, char *S) {
	cells.eb(sr, sc);
	for (int i = 0; i < M; i++) {
		if (S[i] == 'N') {
			sr--;
		} else if (S[i] == 'S') {
			sr++;
		} else if (S[i] == 'E') {
			sc++;
		} else {
			sc--;
		}
		cells.eb(sr, sc);
	}
	sort(cells.begin(), cells.end());
	cells.erase(unique(cells.begin(), cells.end()), cells.end());
	for (const auto &[r, c] : cells) {
		//~ cout << "! " << r << ' ' << c << '\n';
		nodes.eb(r, c);
		nodes.eb(r + 1, c);
		nodes.eb(r, c + 1);
		nodes.eb(r + 1, c + 1);
		horiz.eb(r, c);
		horiz.eb(r + 1, c);
		vert.eb(r, c);
		vert.eb(r, c + 1);
	}
	for (auto *vec : {&nodes, &horiz, &vert}) {
		sort(vec->begin(), vec->end());
		vec->erase(unique(vec->begin(), vec->end()), vec->end());
	}
	//~ cout << "HERE" << endl;
	for (const auto &[r, c] : nodes) {
		//~ cout << r << ' ' << c << '\n';
		row[r].pb(c);
		col[c].pb(r);
	}
	for (int i = 1; i <= R; i++) {
		sort(row[i].begin(), row[i].end());
	}
	for (int i = 1; i <= C; i++) {
		sort(col[i].begin(), col[i].end());
	}
	root_nodes = new node(1, R, nodes);
	ptr = 0;
	root_cells = new node(1, R, cells);
	ptr = 0;
	root_horiz = new node(1, R, horiz);
	ptr = 0;
	root_vert = new node(1, R, vert);
	//~ cout << "HERE" << endl;
}

int colour(int ar, int ac, int br, int bc) {
	int V = (br - ar + 1) * 2 + (bc - ac + 1) * 2 - 4, E = V;
	bool border = 0;
	for (int tmp_r : {ar, br + 1}) {
		auto it = lower_bound(row[tmp_r].begin(), row[tmp_r].end(), ac);
		if (it != row[tmp_r].end() && *it <= bc + 1) {
			border = 1;
		}
	}
	for (int tmp_c : {ac, bc + 1}) {
		auto it = lower_bound(col[tmp_c].begin(), col[tmp_c].end(), ar);
		if (it != col[tmp_c].end() && *it <= br + 1) {
			border = 1;
		}
	}
	V += root_nodes->qry(ar + 1, br, ac + 1, bc);
	//~ for (const auto &[r, c] : nodes) {
		//~ if (ar + 1 <= r && r <= br && ac + 1 <= c && c <= bc) {
			//~ V++;
		//~ }
	//~ }
	if (!border) {
		bool wraps = root_cells->qry(ar, br, ac, bc);
		//~ for (const auto &[r, c] : cells) {
			//~ if (ar <= r && r <= br && ac <= c && c <= bc) {
				//~ wraps = 1;
			//~ }
		//~ }
		if (!wraps) {
			return 1;
		}
		V = nodes.size();
		E = horiz.size() + vert.size();
		int F = 2 - (V - E), water_F = cells.size();
		return F - water_F;
	}
	E += root_horiz->qry(ar + 1, br, ac, bc);
	//~ for (const auto &[r, c] : horiz) {
		//~ if (ar + 1 <= r && r <= br && ac <= c && c <= bc) {
			//~ E++;
		//~ }
	//~ }
	E += root_vert->qry(ar, br, ac + 1, bc);
	//~ for (const auto &[r, c] : vert) {
		//~ if (ar <= r && r <= br && ac + 1 <= c && c <= bc) {
			//~ E++;
		//~ }
	//~ }
	int F = 2 - (V - E), water_F = root_cells->qry(ar, br, ac, bc);
	//~ for (const auto &[r, c] : cells) {
		//~ if (ar <= r && r <= br && ac <= c && c <= bc) {
			//~ water_F++;
		//~ }
	//~ }
	return F - water_F - 1;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 9820 KB Output is correct
2 Correct 4 ms 10076 KB Output is correct
3 Correct 3 ms 9816 KB Output is correct
4 Correct 3 ms 9908 KB Output is correct
5 Correct 4 ms 10076 KB Output is correct
6 Correct 2 ms 9840 KB Output is correct
7 Correct 2 ms 9820 KB Output is correct
8 Correct 2 ms 9828 KB Output is correct
9 Correct 2 ms 9820 KB Output is correct
10 Correct 2 ms 9820 KB Output is correct
11 Correct 3 ms 9992 KB Output is correct
12 Correct 4 ms 10072 KB Output is correct
13 Correct 5 ms 10076 KB Output is correct
14 Correct 5 ms 10428 KB Output is correct
15 Correct 2 ms 9820 KB Output is correct
16 Correct 2 ms 9612 KB Output is correct
17 Correct 2 ms 9820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9612 KB Output is correct
2 Correct 2 ms 9820 KB Output is correct
3 Correct 141 ms 22240 KB Output is correct
4 Correct 161 ms 30400 KB Output is correct
5 Correct 160 ms 29888 KB Output is correct
6 Correct 119 ms 25756 KB Output is correct
7 Correct 133 ms 26208 KB Output is correct
8 Correct 41 ms 14280 KB Output is correct
9 Correct 190 ms 29620 KB Output is correct
10 Correct 160 ms 29364 KB Output is correct
11 Correct 135 ms 26300 KB Output is correct
12 Correct 92 ms 29276 KB Output is correct
13 Correct 100 ms 29888 KB Output is correct
14 Correct 100 ms 29584 KB Output is correct
15 Correct 89 ms 25784 KB Output is correct
16 Correct 127 ms 26888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9820 KB Output is correct
2 Correct 162 ms 165300 KB Output is correct
3 Correct 176 ms 191080 KB Output is correct
4 Correct 181 ms 185280 KB Output is correct
5 Correct 155 ms 165780 KB Output is correct
6 Correct 95 ms 122720 KB Output is correct
7 Correct 114 ms 134072 KB Output is correct
8 Correct 55 ms 29088 KB Output is correct
9 Correct 61 ms 26812 KB Output is correct
10 Correct 59 ms 71104 KB Output is correct
11 Correct 78 ms 85456 KB Output is correct
12 Correct 149 ms 165268 KB Output is correct
13 Correct 212 ms 191180 KB Output is correct
14 Correct 184 ms 185180 KB Output is correct
15 Correct 145 ms 165588 KB Output is correct
16 Correct 89 ms 120444 KB Output is correct
17 Correct 112 ms 134336 KB Output is correct
18 Correct 159 ms 169664 KB Output is correct
19 Correct 171 ms 178280 KB Output is correct
20 Correct 166 ms 178624 KB Output is correct
21 Correct 53 ms 26852 KB Output is correct
22 Correct 51 ms 27936 KB Output is correct
23 Correct 51 ms 71052 KB Output is correct
24 Correct 82 ms 85584 KB Output is correct
25 Correct 143 ms 165320 KB Output is correct
26 Correct 177 ms 191680 KB Output is correct
27 Correct 163 ms 185276 KB Output is correct
28 Correct 146 ms 164752 KB Output is correct
29 Correct 85 ms 120512 KB Output is correct
30 Correct 107 ms 134464 KB Output is correct
31 Correct 149 ms 169140 KB Output is correct
32 Correct 159 ms 178924 KB Output is correct
33 Correct 158 ms 179388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 9820 KB Output is correct
2 Correct 4 ms 10076 KB Output is correct
3 Correct 3 ms 9816 KB Output is correct
4 Correct 3 ms 9908 KB Output is correct
5 Correct 4 ms 10076 KB Output is correct
6 Correct 2 ms 9840 KB Output is correct
7 Correct 2 ms 9820 KB Output is correct
8 Correct 2 ms 9828 KB Output is correct
9 Correct 2 ms 9820 KB Output is correct
10 Correct 2 ms 9820 KB Output is correct
11 Correct 3 ms 9992 KB Output is correct
12 Correct 4 ms 10072 KB Output is correct
13 Correct 5 ms 10076 KB Output is correct
14 Correct 5 ms 10428 KB Output is correct
15 Correct 2 ms 9820 KB Output is correct
16 Correct 2 ms 9612 KB Output is correct
17 Correct 2 ms 9820 KB Output is correct
18 Correct 574 ms 34920 KB Output is correct
19 Correct 91 ms 13904 KB Output is correct
20 Correct 92 ms 13232 KB Output is correct
21 Correct 91 ms 13652 KB Output is correct
22 Correct 117 ms 13992 KB Output is correct
23 Correct 88 ms 13928 KB Output is correct
24 Correct 140 ms 13436 KB Output is correct
25 Correct 116 ms 13824 KB Output is correct
26 Correct 110 ms 13928 KB Output is correct
27 Correct 235 ms 32196 KB Output is correct
28 Correct 240 ms 23108 KB Output is correct
29 Correct 252 ms 30400 KB Output is correct
30 Correct 462 ms 53776 KB Output is correct
31 Correct 4 ms 9820 KB Output is correct
32 Correct 463 ms 32620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 9820 KB Output is correct
2 Correct 4 ms 10076 KB Output is correct
3 Correct 3 ms 9816 KB Output is correct
4 Correct 3 ms 9908 KB Output is correct
5 Correct 4 ms 10076 KB Output is correct
6 Correct 2 ms 9840 KB Output is correct
7 Correct 2 ms 9820 KB Output is correct
8 Correct 2 ms 9828 KB Output is correct
9 Correct 2 ms 9820 KB Output is correct
10 Correct 2 ms 9820 KB Output is correct
11 Correct 3 ms 9992 KB Output is correct
12 Correct 4 ms 10072 KB Output is correct
13 Correct 5 ms 10076 KB Output is correct
14 Correct 5 ms 10428 KB Output is correct
15 Correct 2 ms 9820 KB Output is correct
16 Correct 2 ms 9612 KB Output is correct
17 Correct 2 ms 9820 KB Output is correct
18 Correct 574 ms 34920 KB Output is correct
19 Correct 91 ms 13904 KB Output is correct
20 Correct 92 ms 13232 KB Output is correct
21 Correct 91 ms 13652 KB Output is correct
22 Correct 117 ms 13992 KB Output is correct
23 Correct 88 ms 13928 KB Output is correct
24 Correct 140 ms 13436 KB Output is correct
25 Correct 116 ms 13824 KB Output is correct
26 Correct 110 ms 13928 KB Output is correct
27 Correct 235 ms 32196 KB Output is correct
28 Correct 240 ms 23108 KB Output is correct
29 Correct 252 ms 30400 KB Output is correct
30 Correct 462 ms 53776 KB Output is correct
31 Correct 4 ms 9820 KB Output is correct
32 Correct 463 ms 32620 KB Output is correct
33 Correct 162 ms 165300 KB Output is correct
34 Correct 176 ms 191080 KB Output is correct
35 Correct 181 ms 185280 KB Output is correct
36 Correct 155 ms 165780 KB Output is correct
37 Correct 95 ms 122720 KB Output is correct
38 Correct 114 ms 134072 KB Output is correct
39 Correct 55 ms 29088 KB Output is correct
40 Correct 61 ms 26812 KB Output is correct
41 Correct 59 ms 71104 KB Output is correct
42 Correct 78 ms 85456 KB Output is correct
43 Correct 149 ms 165268 KB Output is correct
44 Correct 212 ms 191180 KB Output is correct
45 Correct 184 ms 185180 KB Output is correct
46 Correct 145 ms 165588 KB Output is correct
47 Correct 89 ms 120444 KB Output is correct
48 Correct 112 ms 134336 KB Output is correct
49 Correct 159 ms 169664 KB Output is correct
50 Correct 171 ms 178280 KB Output is correct
51 Correct 166 ms 178624 KB Output is correct
52 Correct 53 ms 26852 KB Output is correct
53 Correct 51 ms 27936 KB Output is correct
54 Correct 51 ms 71052 KB Output is correct
55 Correct 82 ms 85584 KB Output is correct
56 Correct 143 ms 165320 KB Output is correct
57 Correct 177 ms 191680 KB Output is correct
58 Correct 163 ms 185276 KB Output is correct
59 Correct 146 ms 164752 KB Output is correct
60 Correct 85 ms 120512 KB Output is correct
61 Correct 107 ms 134464 KB Output is correct
62 Correct 149 ms 169140 KB Output is correct
63 Correct 159 ms 178924 KB Output is correct
64 Correct 158 ms 179388 KB Output is correct
65 Correct 141 ms 22240 KB Output is correct
66 Correct 161 ms 30400 KB Output is correct
67 Correct 160 ms 29888 KB Output is correct
68 Correct 119 ms 25756 KB Output is correct
69 Correct 133 ms 26208 KB Output is correct
70 Correct 41 ms 14280 KB Output is correct
71 Correct 190 ms 29620 KB Output is correct
72 Correct 160 ms 29364 KB Output is correct
73 Correct 135 ms 26300 KB Output is correct
74 Correct 92 ms 29276 KB Output is correct
75 Correct 100 ms 29888 KB Output is correct
76 Correct 100 ms 29584 KB Output is correct
77 Correct 89 ms 25784 KB Output is correct
78 Correct 127 ms 26888 KB Output is correct
79 Correct 239 ms 31448 KB Output is correct
80 Correct 281 ms 31084 KB Output is correct
81 Correct 249 ms 74696 KB Output is correct
82 Correct 295 ms 89020 KB Output is correct
83 Correct 498 ms 168848 KB Output is correct
84 Correct 611 ms 194748 KB Output is correct
85 Correct 496 ms 188864 KB Output is correct
86 Correct 491 ms 169720 KB Output is correct
87 Correct 324 ms 124104 KB Output is correct
88 Correct 346 ms 137868 KB Output is correct
89 Correct 391 ms 173760 KB Output is correct
90 Correct 586 ms 181944 KB Output is correct
91 Correct 634 ms 181944 KB Output is correct