Submission #885424

# Submission time Handle Problem Language Result Execution time Memory
885424 2023-12-09T16:30:56 Z pubin06 Land of the Rainbow Gold (APIO17_rainbow) C++14
100 / 100
747 ms 196992 KB
#include "rainbow.h"
#include <bits/stdc++.h>
#define sz(v) (int)(v).size()
using namespace std;
 
const int MXn = 2e5 + 5;
const int dx[] = {1, 0, 1};
const int dy[] = {0, 1, 1};
 
int n, m;
struct PST {
	vector<int> val, idL, idR;
	void resiz(int s) {
		val.resize(s + 3, 0);
		idL.resize(s + 3, -1);
		idR.resize(s + 3, -1);
	}
	int no = 0;
	int build(int l = 1, int r = n) {
		int cur = ++no;
		assert(cur < sz(val));
		val[cur] = 0;
		if (l == r) return cur;
		int mid = (l + r) >> 1;
		idL[cur] = build(l, mid);
		idR[cur] = build(mid + 1, r);
		return cur;
	}
	int update(int i, int v, int oID, int l = 1, int r = n) {
		int cur = ++no;
		assert(cur < sz(val));
		if (l == r) {
			val[cur] = val[oID] + v;
			return cur;
		}
		int mid = (l + r) >> 1;
		if (i <= mid) {
			idL[cur] = update(i, v, idL[oID], l, mid);
			idR[cur] = idR[oID];
		} else {
			idL[cur] = idL[oID];
			idR[cur] = update(i, v, idR[oID], mid + 1, r);
		}
		val[cur] = val[idL[cur]] + val[idR[cur]];
		return cur;
	}
	int get(int Lf, int Rt, int id, int l = 1, int r = n) {
		if (Lf <= l && r <= Rt) {
			return val[id];
		}
		int mid = (l + r) >> 1;
		int res = 0;
		if (Lf <= mid) res += get(Lf, Rt, idL[id], l, mid);
		if (Rt > mid) res += get(Lf, Rt, idR[id], mid + 1, r);
		return res;
	}
} T[4];
int root[4][MXn];
 
bool onboard(int x, int y) {
	return 1 <= x && x <= n && 1 <= y && y <= m;
}
vector<int> river[MXn], v1[MXn], v2[MXn], v3[MXn];
int xmin = MXn, xmax = -MXn, ymin = MXn, ymax = -MXn;
void add(int sr, int sc) {
	xmin = min(xmin, sr);
	xmax = max(xmax, sr);
	ymin = min(ymin, sc);
	ymax = max(ymax, sc);
    assert(onboard(sr, sc));
	river[sc].emplace_back(sr);
	if (sc > 1) v1[sc - 1].emplace_back(sr);
	v1[sc].emplace_back(sr);
	if (sr > 1) v2[sc].emplace_back(sr - 1);
	v2[sc].emplace_back(sr);
	v3[sc].emplace_back(sr);
	for (int k = 0; k < 3; k++) {
		int xx = dx[k] + sr, yy = dy[k] + sc;
		if (onboard(xx, yy)) v3[yy].emplace_back(xx);
	}
}
void init(int R, int C, int sr, int sc, int M, char *S) {
	n = R, m = C;
	add(sr, sc);
	for (int i = 0; i < M; i++) {
		if (S[i] == 'N') sr--;
		if (S[i] == 'S') sr++;
		if (S[i] == 'W') sc--;
		if (S[i] == 'E') sc++;
		add(sr, sc);
	}
	vector<int> siz(4, 0);
	for (int c = 1; c <= C; c++) {
		sort(begin(river[c]), end(river[c]));
		river[c].resize(unique(begin(river[c]), end(river[c])) - river[c].begin());
		siz[0] += sz(river[c]);
		sort(begin(v1[c]), end(v1[c]));
		v1[c].resize(unique(begin(v1[c]), end(v1[c])) - v1[c].begin());
		siz[1] += sz(v1[c]);
		sort(begin(v2[c]), end(v2[c]));
		v2[c].resize(unique(begin(v2[c]), end(v2[c])) - v2[c].begin());
		siz[2] += sz(v2[c]);
		sort(begin(v3[c]), end(v3[c]));
		v3[c].resize(unique(begin(v3[c]), end(v3[c])) - v3[c].begin());
		siz[3] += sz(v3[c]);
	}
	for (int t = 0; t < 4; t++) {
		T[t].resiz((1 << 19) + siz[t] * 19);
		root[t][0] = T[t].build();
	}
	for (int c = 1; c <= C; c++) {
		root[0][c] = root[0][c - 1];
		root[1][c] = root[1][c - 1];
		root[2][c] = root[2][c - 1];
		root[3][c] = root[3][c - 1];
		
		for (int r: river[c]) {
			root[0][c] = T[0].update(r, 1, root[0][c]);
		}
		for (int r: v1[c]) {
			root[1][c] = T[1].update(r, 1, root[1][c]);
		}
		for (int r: v2[c]) {
			root[2][c] = T[2].update(r, 1, root[2][c]);
		}
		for (int r: v3[c]) {
			root[3][c] = T[3].update(r, 1, root[3][c]);
		}
	}
}
 
int colour(int ar, int ac, int br, int bc) {
	long long V = (br - ar + 1) * (bc - ac + 1) - (T[0].get(ar, br, root[0][bc]) - T[0].get(ar, br, root[0][ac - 1]));
	long long E = (bc - ac) * (br - ar + 1) - (T[1].get(ar, br, root[1][bc - 1]) - T[1].get(ar, br, root[1][ac - 1])) + (bc - ac + 1) * (br - ar);
 	long long F = 0;
 	if (ar < br) {
 	    E -= (T[2].get(ar, br - 1, root[2][bc]) - T[2].get(ar, br - 1, root[2][ac - 1]));
 	    F = (br - ar) * (bc - ac) - (T[3].get(ar + 1, br, root[3][bc]) - T[3].get(ar + 1, br, root[3][ac]));
 	}
 	if (ar < xmin && xmax < br && ac < ymin && ymax < bc) F++;
 	// cerr << x1 << ' ' << ymin << ' ' << xmax << ' ' << ymax << '\n';
 	// cerr << V << ' ' << E << ' ' << F << '\n';
 	return V - E + F;
}
# Verdict Execution time Memory Grader output
1 Correct 17 ms 45916 KB Output is correct
2 Correct 16 ms 46684 KB Output is correct
3 Correct 16 ms 45916 KB Output is correct
4 Correct 16 ms 46088 KB Output is correct
5 Correct 17 ms 47168 KB Output is correct
6 Correct 14 ms 45660 KB Output is correct
7 Correct 14 ms 45660 KB Output is correct
8 Correct 14 ms 45668 KB Output is correct
9 Correct 15 ms 45800 KB Output is correct
10 Correct 15 ms 45660 KB Output is correct
11 Correct 16 ms 46168 KB Output is correct
12 Correct 16 ms 46680 KB Output is correct
13 Correct 18 ms 47452 KB Output is correct
14 Correct 17 ms 47964 KB Output is correct
15 Correct 14 ms 45660 KB Output is correct
16 Correct 17 ms 45656 KB Output is correct
17 Correct 14 ms 45656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 45656 KB Output is correct
2 Correct 14 ms 45656 KB Output is correct
3 Correct 102 ms 121504 KB Output is correct
4 Correct 147 ms 171092 KB Output is correct
5 Correct 140 ms 168788 KB Output is correct
6 Correct 112 ms 137296 KB Output is correct
7 Correct 111 ms 132728 KB Output is correct
8 Correct 77 ms 51556 KB Output is correct
9 Correct 131 ms 171252 KB Output is correct
10 Correct 155 ms 168784 KB Output is correct
11 Correct 119 ms 137296 KB Output is correct
12 Correct 114 ms 162900 KB Output is correct
13 Correct 114 ms 171188 KB Output is correct
14 Correct 115 ms 168600 KB Output is correct
15 Correct 107 ms 137344 KB Output is correct
16 Correct 124 ms 135252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 45660 KB Output is correct
2 Correct 162 ms 193616 KB Output is correct
3 Correct 166 ms 184736 KB Output is correct
4 Correct 175 ms 187792 KB Output is correct
5 Correct 132 ms 153220 KB Output is correct
6 Correct 68 ms 76884 KB Output is correct
7 Correct 90 ms 102240 KB Output is correct
8 Correct 79 ms 155476 KB Output is correct
9 Correct 70 ms 145872 KB Output is correct
10 Correct 46 ms 74480 KB Output is correct
11 Correct 82 ms 112724 KB Output is correct
12 Correct 150 ms 193404 KB Output is correct
13 Correct 167 ms 185252 KB Output is correct
14 Correct 165 ms 187984 KB Output is correct
15 Correct 142 ms 153220 KB Output is correct
16 Correct 63 ms 72276 KB Output is correct
17 Correct 96 ms 101940 KB Output is correct
18 Correct 165 ms 185408 KB Output is correct
19 Correct 146 ms 177764 KB Output is correct
20 Correct 147 ms 177756 KB Output is correct
21 Correct 71 ms 155396 KB Output is correct
22 Correct 75 ms 145744 KB Output is correct
23 Correct 47 ms 74324 KB Output is correct
24 Correct 83 ms 112816 KB Output is correct
25 Correct 151 ms 193488 KB Output is correct
26 Correct 166 ms 184992 KB Output is correct
27 Correct 208 ms 187984 KB Output is correct
28 Correct 126 ms 153220 KB Output is correct
29 Correct 64 ms 72276 KB Output is correct
30 Correct 107 ms 102000 KB Output is correct
31 Correct 170 ms 185424 KB Output is correct
32 Correct 143 ms 177864 KB Output is correct
33 Correct 147 ms 177796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 45916 KB Output is correct
2 Correct 16 ms 46684 KB Output is correct
3 Correct 16 ms 45916 KB Output is correct
4 Correct 16 ms 46088 KB Output is correct
5 Correct 17 ms 47168 KB Output is correct
6 Correct 14 ms 45660 KB Output is correct
7 Correct 14 ms 45660 KB Output is correct
8 Correct 14 ms 45668 KB Output is correct
9 Correct 15 ms 45800 KB Output is correct
10 Correct 15 ms 45660 KB Output is correct
11 Correct 16 ms 46168 KB Output is correct
12 Correct 16 ms 46680 KB Output is correct
13 Correct 18 ms 47452 KB Output is correct
14 Correct 17 ms 47964 KB Output is correct
15 Correct 14 ms 45660 KB Output is correct
16 Correct 17 ms 45656 KB Output is correct
17 Correct 14 ms 45656 KB Output is correct
18 Correct 417 ms 121008 KB Output is correct
19 Correct 123 ms 52308 KB Output is correct
20 Correct 118 ms 49744 KB Output is correct
21 Correct 156 ms 50256 KB Output is correct
22 Correct 151 ms 50936 KB Output is correct
23 Correct 126 ms 52092 KB Output is correct
24 Correct 122 ms 50240 KB Output is correct
25 Correct 150 ms 51284 KB Output is correct
26 Correct 159 ms 51360 KB Output is correct
27 Correct 254 ms 108880 KB Output is correct
28 Correct 223 ms 80776 KB Output is correct
29 Correct 266 ms 104508 KB Output is correct
30 Correct 425 ms 187988 KB Output is correct
31 Correct 16 ms 45912 KB Output is correct
32 Correct 373 ms 110164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 45916 KB Output is correct
2 Correct 16 ms 46684 KB Output is correct
3 Correct 16 ms 45916 KB Output is correct
4 Correct 16 ms 46088 KB Output is correct
5 Correct 17 ms 47168 KB Output is correct
6 Correct 14 ms 45660 KB Output is correct
7 Correct 14 ms 45660 KB Output is correct
8 Correct 14 ms 45668 KB Output is correct
9 Correct 15 ms 45800 KB Output is correct
10 Correct 15 ms 45660 KB Output is correct
11 Correct 16 ms 46168 KB Output is correct
12 Correct 16 ms 46680 KB Output is correct
13 Correct 18 ms 47452 KB Output is correct
14 Correct 17 ms 47964 KB Output is correct
15 Correct 14 ms 45660 KB Output is correct
16 Correct 17 ms 45656 KB Output is correct
17 Correct 14 ms 45656 KB Output is correct
18 Correct 417 ms 121008 KB Output is correct
19 Correct 123 ms 52308 KB Output is correct
20 Correct 118 ms 49744 KB Output is correct
21 Correct 156 ms 50256 KB Output is correct
22 Correct 151 ms 50936 KB Output is correct
23 Correct 126 ms 52092 KB Output is correct
24 Correct 122 ms 50240 KB Output is correct
25 Correct 150 ms 51284 KB Output is correct
26 Correct 159 ms 51360 KB Output is correct
27 Correct 254 ms 108880 KB Output is correct
28 Correct 223 ms 80776 KB Output is correct
29 Correct 266 ms 104508 KB Output is correct
30 Correct 425 ms 187988 KB Output is correct
31 Correct 16 ms 45912 KB Output is correct
32 Correct 373 ms 110164 KB Output is correct
33 Correct 162 ms 193616 KB Output is correct
34 Correct 166 ms 184736 KB Output is correct
35 Correct 175 ms 187792 KB Output is correct
36 Correct 132 ms 153220 KB Output is correct
37 Correct 68 ms 76884 KB Output is correct
38 Correct 90 ms 102240 KB Output is correct
39 Correct 79 ms 155476 KB Output is correct
40 Correct 70 ms 145872 KB Output is correct
41 Correct 46 ms 74480 KB Output is correct
42 Correct 82 ms 112724 KB Output is correct
43 Correct 150 ms 193404 KB Output is correct
44 Correct 167 ms 185252 KB Output is correct
45 Correct 165 ms 187984 KB Output is correct
46 Correct 142 ms 153220 KB Output is correct
47 Correct 63 ms 72276 KB Output is correct
48 Correct 96 ms 101940 KB Output is correct
49 Correct 165 ms 185408 KB Output is correct
50 Correct 146 ms 177764 KB Output is correct
51 Correct 147 ms 177756 KB Output is correct
52 Correct 71 ms 155396 KB Output is correct
53 Correct 75 ms 145744 KB Output is correct
54 Correct 47 ms 74324 KB Output is correct
55 Correct 83 ms 112816 KB Output is correct
56 Correct 151 ms 193488 KB Output is correct
57 Correct 166 ms 184992 KB Output is correct
58 Correct 208 ms 187984 KB Output is correct
59 Correct 126 ms 153220 KB Output is correct
60 Correct 64 ms 72276 KB Output is correct
61 Correct 107 ms 102000 KB Output is correct
62 Correct 170 ms 185424 KB Output is correct
63 Correct 143 ms 177864 KB Output is correct
64 Correct 147 ms 177796 KB Output is correct
65 Correct 102 ms 121504 KB Output is correct
66 Correct 147 ms 171092 KB Output is correct
67 Correct 140 ms 168788 KB Output is correct
68 Correct 112 ms 137296 KB Output is correct
69 Correct 111 ms 132728 KB Output is correct
70 Correct 77 ms 51556 KB Output is correct
71 Correct 131 ms 171252 KB Output is correct
72 Correct 155 ms 168784 KB Output is correct
73 Correct 119 ms 137296 KB Output is correct
74 Correct 114 ms 162900 KB Output is correct
75 Correct 114 ms 171188 KB Output is correct
76 Correct 115 ms 168600 KB Output is correct
77 Correct 107 ms 137344 KB Output is correct
78 Correct 124 ms 135252 KB Output is correct
79 Correct 185 ms 159060 KB Output is correct
80 Correct 205 ms 149304 KB Output is correct
81 Correct 325 ms 77904 KB Output is correct
82 Correct 350 ms 116312 KB Output is correct
83 Correct 624 ms 196992 KB Output is correct
84 Correct 747 ms 188472 KB Output is correct
85 Correct 667 ms 191572 KB Output is correct
86 Correct 620 ms 156808 KB Output is correct
87 Correct 449 ms 76000 KB Output is correct
88 Correct 483 ms 105416 KB Output is correct
89 Correct 595 ms 189112 KB Output is correct
90 Correct 692 ms 181172 KB Output is correct
91 Correct 587 ms 181428 KB Output is correct