Submission #676544

# Submission time Handle Problem Language Result Execution time Memory
676544 2022-12-31T08:25:48 Z ymm Land of the Rainbow Gold (APIO17_rainbow) C++17
100 / 100
799 ms 148932 KB
#include <bits/stdc++.h>
#define Loop(x,l,r) for (ll x = (l); x < (ll)(r); ++x)
#define LoopR(x,l,r) for (ll x = (r)-1; x >= (ll)(l); --x)
typedef long long ll;
typedef std::pair<int, int> pii;
typedef std::pair<ll , ll > pll;
using namespace std;
#include "rainbow.h"

struct seg {
	vector<int> *t;
	
	void init(int sz) { t = new vector<int>[4*sz]; }
	
	void add(int p, int x, int i, int b, int e) {
		if (e-b == 1) {
			t[i].push_back(x);
			return;
		}
		if (p < (b+e)/2)
			add(p, x, 2*i+1, b, (b+e)/2);
		else
			add(p, x, 2*i+2, (b+e)/2, e);
	}
	void build(int i, int b, int e) {
		if (e-b == 1) {
			sort(t[i].begin(), t[i].end());
			auto it = unique(t[i].begin(), t[i].end());
			t[i].resize(it - t[i].begin());
			return;
		}
		build(2*i+1, b, (b+e)/2);
		build(2*i+2, (b+e)/2, e);
		merge(t[2*i+1].begin(), t[2*i+1].end(),
		      t[2*i+2].begin(), t[2*i+2].end(),
		      back_inserter(t[i]));
	}
	int get(int l1, int r1, int l2, int r2, int i, int b, int e) {
		if (l1 <= b && e <= r1) {
			return   lower_bound(t[i].begin(), t[i].end(), r2)
			       - lower_bound(t[i].begin(), t[i].end(), l2);
		}
		if (r1 <= b || e <= l1)
			return 0;
		return   get(l1, r1, l2, r2, 2*i+1, b, (b+e)/2)
		       + get(l1, r1, l2, r2, 2*i+2, (b+e)/2, e);
	}
} seg_edge_h, seg_edge_v, seg_node, seg_river;
int n, m;
int rmn = INT_MAX, rmx = 0;
int cmn = INT_MAX, cmx = 0;

void add_river(int i, int j)
{
	seg_edge_h.add(i, j, 0, 0, n+1);
	seg_edge_h.add(i+1, j, 0, 0, n+1);
	seg_edge_v.add(i, j, 0, 0, n+1);
	seg_edge_v.add(i, j+1, 0, 0, n+1);
	seg_node.add(i, j, 0, 0, n+1);
	seg_node.add(i, j+1, 0, 0, n+1);
	seg_node.add(i+1, j, 0, 0, n+1);
	seg_node.add(i+1, j+1, 0, 0, n+1);
	seg_river.add(i, j, 0, 0, n);
	rmn = min(rmn, i);
	rmx = max(rmx, i);
	cmn = min(cmn, j);
	cmx = max(cmx, j);
}

void init(int R, int C, int sr, int sc, int M, char *S)
{
	n = R; m = C;
	seg_edge_h.init(n+1);
	seg_edge_v.init(n+1);
	seg_node.init(n+1);
	seg_river.init(n);
	int x = sr - 1, y = sc - 1;
	add_river(x, y);
	Loop (i,0,M) {
		char c = S[i];
		x -= c == 'N';
		x += c == 'S';
		y -= c == 'W';
		y += c == 'E';
		add_river(x, y);
	}
	seg_edge_h.build(0, 0, n+1);
	seg_edge_v.build(0, 0, n+1);
	seg_node.build(0, 0, n+1);
	seg_river.build(0, 0, n);
}

int colour(int ar, int ac, int br, int bc)
{
	--ar; --ac;
	int edges = 0, nodes = 0, rivers = 0, comps = 0;
	edges += seg_edge_h.get(ar+1, br, ac, bc, 0, 0, n+1);
	edges += seg_edge_v.get(ar, br, ac+1, bc, 0, 0, n+1);
	edges += 2*(br - ar) + 2*(bc - ac);
	nodes += seg_node.get(ar+1, br, ac+1, bc, 0, 0, n+1);
	nodes += 2*(br - ar) + 2*(bc - ac);
	rivers += seg_river.get(ar, br, ac, bc, 0, 0, n);
	comps = 1 + (ar < rmn && rmx+1 < br && ac < cmn && cmx+1 < bc);
	int ans = edges - nodes + comps - rivers;
	return ans;
}

# Verdict Execution time Memory Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 4 ms 596 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 2 ms 340 KB Output is correct
5 Correct 4 ms 596 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 3 ms 468 KB Output is correct
12 Correct 3 ms 468 KB Output is correct
13 Correct 4 ms 596 KB Output is correct
14 Correct 5 ms 852 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 110 ms 7464 KB Output is correct
4 Correct 149 ms 10628 KB Output is correct
5 Correct 122 ms 10708 KB Output is correct
6 Correct 127 ms 9896 KB Output is correct
7 Correct 151 ms 9848 KB Output is correct
8 Correct 75 ms 6880 KB Output is correct
9 Correct 131 ms 10852 KB Output is correct
10 Correct 142 ms 10864 KB Output is correct
11 Correct 143 ms 9800 KB Output is correct
12 Correct 76 ms 10520 KB Output is correct
13 Correct 78 ms 10728 KB Output is correct
14 Correct 81 ms 10796 KB Output is correct
15 Correct 87 ms 9928 KB Output is correct
16 Correct 99 ms 8472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 134 ms 121676 KB Output is correct
3 Correct 208 ms 145384 KB Output is correct
4 Correct 196 ms 140392 KB Output is correct
5 Correct 173 ms 123092 KB Output is correct
6 Correct 119 ms 89904 KB Output is correct
7 Correct 116 ms 98424 KB Output is correct
8 Correct 53 ms 11368 KB Output is correct
9 Correct 49 ms 12556 KB Output is correct
10 Correct 66 ms 48672 KB Output is correct
11 Correct 107 ms 63696 KB Output is correct
12 Correct 128 ms 121804 KB Output is correct
13 Correct 215 ms 145320 KB Output is correct
14 Correct 198 ms 140432 KB Output is correct
15 Correct 170 ms 123112 KB Output is correct
16 Correct 108 ms 87744 KB Output is correct
17 Correct 117 ms 99220 KB Output is correct
18 Correct 149 ms 127820 KB Output is correct
19 Correct 170 ms 132976 KB Output is correct
20 Correct 170 ms 133616 KB Output is correct
21 Correct 47 ms 11280 KB Output is correct
22 Correct 64 ms 12548 KB Output is correct
23 Correct 59 ms 48704 KB Output is correct
24 Correct 105 ms 63680 KB Output is correct
25 Correct 128 ms 121768 KB Output is correct
26 Correct 221 ms 145364 KB Output is correct
27 Correct 192 ms 140312 KB Output is correct
28 Correct 170 ms 123036 KB Output is correct
29 Correct 98 ms 87772 KB Output is correct
30 Correct 126 ms 99224 KB Output is correct
31 Correct 143 ms 127864 KB Output is correct
32 Correct 176 ms 133024 KB Output is correct
33 Correct 174 ms 133716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 4 ms 596 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 2 ms 340 KB Output is correct
5 Correct 4 ms 596 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 3 ms 468 KB Output is correct
12 Correct 3 ms 468 KB Output is correct
13 Correct 4 ms 596 KB Output is correct
14 Correct 5 ms 852 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 779 ms 24024 KB Output is correct
19 Correct 151 ms 4412 KB Output is correct
20 Correct 145 ms 3844 KB Output is correct
21 Correct 154 ms 4040 KB Output is correct
22 Correct 179 ms 4220 KB Output is correct
23 Correct 138 ms 4276 KB Output is correct
24 Correct 181 ms 4012 KB Output is correct
25 Correct 183 ms 4224 KB Output is correct
26 Correct 176 ms 4364 KB Output is correct
27 Correct 364 ms 21188 KB Output is correct
28 Correct 319 ms 15064 KB Output is correct
29 Correct 352 ms 20040 KB Output is correct
30 Correct 622 ms 36852 KB Output is correct
31 Correct 3 ms 340 KB Output is correct
32 Correct 582 ms 20860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 4 ms 596 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 2 ms 340 KB Output is correct
5 Correct 4 ms 596 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 3 ms 468 KB Output is correct
12 Correct 3 ms 468 KB Output is correct
13 Correct 4 ms 596 KB Output is correct
14 Correct 5 ms 852 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 779 ms 24024 KB Output is correct
19 Correct 151 ms 4412 KB Output is correct
20 Correct 145 ms 3844 KB Output is correct
21 Correct 154 ms 4040 KB Output is correct
22 Correct 179 ms 4220 KB Output is correct
23 Correct 138 ms 4276 KB Output is correct
24 Correct 181 ms 4012 KB Output is correct
25 Correct 183 ms 4224 KB Output is correct
26 Correct 176 ms 4364 KB Output is correct
27 Correct 364 ms 21188 KB Output is correct
28 Correct 319 ms 15064 KB Output is correct
29 Correct 352 ms 20040 KB Output is correct
30 Correct 622 ms 36852 KB Output is correct
31 Correct 3 ms 340 KB Output is correct
32 Correct 582 ms 20860 KB Output is correct
33 Correct 134 ms 121676 KB Output is correct
34 Correct 208 ms 145384 KB Output is correct
35 Correct 196 ms 140392 KB Output is correct
36 Correct 173 ms 123092 KB Output is correct
37 Correct 119 ms 89904 KB Output is correct
38 Correct 116 ms 98424 KB Output is correct
39 Correct 53 ms 11368 KB Output is correct
40 Correct 49 ms 12556 KB Output is correct
41 Correct 66 ms 48672 KB Output is correct
42 Correct 107 ms 63696 KB Output is correct
43 Correct 128 ms 121804 KB Output is correct
44 Correct 215 ms 145320 KB Output is correct
45 Correct 198 ms 140432 KB Output is correct
46 Correct 170 ms 123112 KB Output is correct
47 Correct 108 ms 87744 KB Output is correct
48 Correct 117 ms 99220 KB Output is correct
49 Correct 149 ms 127820 KB Output is correct
50 Correct 170 ms 132976 KB Output is correct
51 Correct 170 ms 133616 KB Output is correct
52 Correct 47 ms 11280 KB Output is correct
53 Correct 64 ms 12548 KB Output is correct
54 Correct 59 ms 48704 KB Output is correct
55 Correct 105 ms 63680 KB Output is correct
56 Correct 128 ms 121768 KB Output is correct
57 Correct 221 ms 145364 KB Output is correct
58 Correct 192 ms 140312 KB Output is correct
59 Correct 170 ms 123036 KB Output is correct
60 Correct 98 ms 87772 KB Output is correct
61 Correct 126 ms 99224 KB Output is correct
62 Correct 143 ms 127864 KB Output is correct
63 Correct 176 ms 133024 KB Output is correct
64 Correct 174 ms 133716 KB Output is correct
65 Correct 110 ms 7464 KB Output is correct
66 Correct 149 ms 10628 KB Output is correct
67 Correct 122 ms 10708 KB Output is correct
68 Correct 127 ms 9896 KB Output is correct
69 Correct 151 ms 9848 KB Output is correct
70 Correct 75 ms 6880 KB Output is correct
71 Correct 131 ms 10852 KB Output is correct
72 Correct 142 ms 10864 KB Output is correct
73 Correct 143 ms 9800 KB Output is correct
74 Correct 76 ms 10520 KB Output is correct
75 Correct 78 ms 10728 KB Output is correct
76 Correct 81 ms 10796 KB Output is correct
77 Correct 87 ms 9928 KB Output is correct
78 Correct 99 ms 8472 KB Output is correct
79 Correct 237 ms 14792 KB Output is correct
80 Correct 293 ms 16048 KB Output is correct
81 Correct 329 ms 52096 KB Output is correct
82 Correct 438 ms 67264 KB Output is correct
83 Correct 519 ms 125204 KB Output is correct
84 Correct 799 ms 148932 KB Output is correct
85 Correct 578 ms 143832 KB Output is correct
86 Correct 619 ms 126608 KB Output is correct
87 Correct 469 ms 91388 KB Output is correct
88 Correct 433 ms 102712 KB Output is correct
89 Correct 492 ms 131400 KB Output is correct
90 Correct 660 ms 136624 KB Output is correct
91 Correct 664 ms 137156 KB Output is correct