Submission #544992

# Submission time Handle Problem Language Result Execution time Memory
544992 2022-04-03T09:48:49 Z valerikk Land of the Rainbow Gold (APIO17_rainbow) C++17
100 / 100
962 ms 122504 KB
#include "rainbow.h"
#include <bits/stdc++.h>

using namespace std;

struct Fenwick {
	int n;
	vector<vector<int> > ys;
	vector<vector<long long> > f;
	vector<int> nn;
	bool built;

	void fake_add(int x, int y) {
		assert(!built);
		for (int i = x + 1; i <= n; i += i & -i) {
			ys[i].emplace_back(y);
		}
	}

	void build() {
		assert(!built);
		for (int i = 0; i <= n; ++i) {
			sort(ys[i].begin(), ys[i].end());
			ys[i].resize(unique(ys[i].begin(), ys[i].end()) - ys[i].begin());
			nn[i] = ys[i].size();
			f[i].resize(nn[i] + 1);
		}
		built = true;
	}

	void add(int x, int y) {
		assert(built);
		for (int i = x + 1; i <= n; i += i & -i) {
			for (int j = lower_bound(ys[i].begin(), ys[i].end(), y) - ys[i].begin() + 1; j <= nn[i]; j += j & -j) {
				f[i][j] += 1;
			}
		}
	}

	long long get(int x, int y) {
		assert(built);

		long long res = 0;		
		for (int i = x; i > 0; i -= i & -i) {
			for (int j = lower_bound(ys[i].begin(), ys[i].end(), y) - ys[i].begin(); j > 0; j -= j & -j) {
				res += f[i][j];
			}
		}
		return res;
	}

	long long get(int x1, int y1, int x2, int y2) {
		return get(x2, y2) - get(x1, y2) - get(x2, y1) + get(x1, y1);
	}

	Fenwick(int n = 0) : n(n), ys(n + 1), f(n + 1), nn(n + 1), built(false) {}
};

int n, m;
int k;
vector<pair<int, int> > a;
Fenwick fv, fex, fey, ff;
int mnx, mny, mxx, mxy;

void init(int R, int C, int sr, int sc, int M, char *S) {
	n = R;
	m = C;
	
	int x = sr - 1, y = sc - 1;
	a.emplace_back(x, y);
	for (int i = 0; i < M; ++i) {
		if (S[i] == 'N') {
			--x;
		}
		if (S[i] == 'S') {
			++x;
		}
		if (S[i] == 'E') {
			++y;
		}
		if (S[i] == 'W') {
			--y;
		}

		a.emplace_back(x, y);
	}
	
	sort(a.begin(), a.end());
	a.resize(unique(a.begin(), a.end()) - a.begin());

	mnx = numeric_limits<int>::max();
	mxx = numeric_limits<int>::min();
	mny = numeric_limits<int>::max();
	mxy = numeric_limits<int>::min();

	vector<pair<int, int>> v, ex, ey, f;
	for (const auto &[x, y] : a) {
		mnx = min(mnx, x);
		mxx = max(mxx, x);
		mny = min(mny, y);
		mxy = max(mxy, y);

		v.emplace_back(x, y);
		ex.emplace_back(x, y);
		ey.emplace_back(x, y);
		f.emplace_back(x, y);
		if (x > 0) {
			ex.emplace_back(x - 1, y);
			f.emplace_back(x - 1, y);
		}
		if (y > 0) {
			ey.emplace_back(x, y - 1);
			f.emplace_back(x, y - 1);
		}
		if (x > 0 && y > 0) {
			f.emplace_back(x - 1, y - 1);
		}
	}

	sort(v.begin(), v.end());
	v.resize(unique(v.begin(), v.end()) - v.begin());
	sort(ex.begin(), ex.end());
	ex.resize(unique(ex.begin(), ex.end()) - ex.begin());
	sort(ey.begin(), ey.end());
	ey.resize(unique(ey.begin(), ey.end()) - ey.begin());
	sort(f.begin(), f.end());
	f.resize(unique(f.begin(), f.end()) - f.begin());

	fv = {n};
	fex = {n};
	fey = {n};
	ff = {n};

	for (const auto &[x, y] : v) {
		fv.fake_add(x, y);
	}
	for (const auto &[x, y] : ex) {
		fex.fake_add(x, y);
	}
	for (const auto &[x, y] : ey) {
		fey.fake_add(x, y);
	}
	for (const auto &[x, y] : f) {
		ff.fake_add(x, y);
	}

	fv.build();
	fex.build();
	fey.build();
	ff.build();

	for (const auto &[x, y] : v) {
		fv.add(x, y);
	}
	for (const auto &[x, y] : ex) {
		fex.add(x, y);
	}
	for (const auto &[x, y] : ey) {
		fey.add(x, y);
	}
	for (const auto &[x, y] : f) {
		ff.add(x, y);
	}
}	

int colour(int ar, int ac, int br, int bc) {
	int x1 = ar - 1, y1 = ac - 1, x2 = br, y2 = bc;
	
	long long v = (long long) (x2 - x1) * (y2 - y1) - fv.get(x1, y1, x2, y2);
	long long e = (long long) (x2 - x1 - 1) * (y2 - y1) - fex.get(x1, y1, x2 - 1, y2) + (long long) (x2 - x1) * (y2 - y1 - 1) - fey.get(x1, y1, x2, y2 - 1);
	long long f = (long long) (x2 - x1 - 1) * (y2 - y1 - 1) - ff.get(x1, y1, x2 - 1, y2 - 1) + (mnx > x1 && mny > y1 && mxx < x2 - 1 && mxy < y2 - 1);
	
	if (v == 0) {
		return 0;
	}

	long long c = f - e + v;
	
	return (int) c;
}

# Verdict Execution time Memory Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 4 ms 468 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 3 ms 340 KB Output is correct
5 Correct 4 ms 596 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 3 ms 340 KB Output is correct
12 Correct 3 ms 468 KB Output is correct
13 Correct 5 ms 596 KB Output is correct
14 Correct 5 ms 724 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 203 ms 9932 KB Output is correct
4 Correct 287 ms 16748 KB Output is correct
5 Correct 289 ms 16240 KB Output is correct
6 Correct 221 ms 12020 KB Output is correct
7 Correct 227 ms 11636 KB Output is correct
8 Correct 57 ms 1980 KB Output is correct
9 Correct 268 ms 16640 KB Output is correct
10 Correct 283 ms 16228 KB Output is correct
11 Correct 225 ms 12128 KB Output is correct
12 Correct 202 ms 16088 KB Output is correct
13 Correct 194 ms 16740 KB Output is correct
14 Correct 211 ms 16136 KB Output is correct
15 Correct 172 ms 12024 KB Output is correct
16 Correct 236 ms 12228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 733 ms 122400 KB Output is correct
3 Correct 307 ms 104304 KB Output is correct
4 Correct 650 ms 121112 KB Output is correct
5 Correct 374 ms 100820 KB Output is correct
6 Correct 191 ms 75056 KB Output is correct
7 Correct 260 ms 80768 KB Output is correct
8 Correct 154 ms 17280 KB Output is correct
9 Correct 162 ms 16200 KB Output is correct
10 Correct 100 ms 37932 KB Output is correct
11 Correct 163 ms 44424 KB Output is correct
12 Correct 807 ms 122444 KB Output is correct
13 Correct 335 ms 104304 KB Output is correct
14 Correct 656 ms 121116 KB Output is correct
15 Correct 385 ms 100728 KB Output is correct
16 Correct 174 ms 73540 KB Output is correct
17 Correct 316 ms 84252 KB Output is correct
18 Correct 776 ms 117588 KB Output is correct
19 Correct 568 ms 113000 KB Output is correct
20 Correct 637 ms 119684 KB Output is correct
21 Correct 185 ms 17312 KB Output is correct
22 Correct 160 ms 16208 KB Output is correct
23 Correct 99 ms 37956 KB Output is correct
24 Correct 157 ms 44416 KB Output is correct
25 Correct 778 ms 122496 KB Output is correct
26 Correct 321 ms 104352 KB Output is correct
27 Correct 686 ms 121120 KB Output is correct
28 Correct 368 ms 100724 KB Output is correct
29 Correct 176 ms 73516 KB Output is correct
30 Correct 316 ms 84344 KB Output is correct
31 Correct 727 ms 117740 KB Output is correct
32 Correct 523 ms 112972 KB Output is correct
33 Correct 562 ms 119680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 4 ms 468 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 3 ms 340 KB Output is correct
5 Correct 4 ms 596 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 3 ms 340 KB Output is correct
12 Correct 3 ms 468 KB Output is correct
13 Correct 5 ms 596 KB Output is correct
14 Correct 5 ms 724 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 667 ms 15404 KB Output is correct
19 Correct 247 ms 1832 KB Output is correct
20 Correct 166 ms 1052 KB Output is correct
21 Correct 185 ms 1212 KB Output is correct
22 Correct 195 ms 1460 KB Output is correct
23 Correct 228 ms 1808 KB Output is correct
24 Correct 210 ms 1248 KB Output is correct
25 Correct 216 ms 1484 KB Output is correct
26 Correct 215 ms 1584 KB Output is correct
27 Correct 301 ms 12316 KB Output is correct
28 Correct 258 ms 7472 KB Output is correct
29 Correct 300 ms 11860 KB Output is correct
30 Correct 592 ms 29228 KB Output is correct
31 Correct 3 ms 340 KB Output is correct
32 Correct 408 ms 13548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 4 ms 468 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 3 ms 340 KB Output is correct
5 Correct 4 ms 596 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 3 ms 340 KB Output is correct
12 Correct 3 ms 468 KB Output is correct
13 Correct 5 ms 596 KB Output is correct
14 Correct 5 ms 724 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 667 ms 15404 KB Output is correct
19 Correct 247 ms 1832 KB Output is correct
20 Correct 166 ms 1052 KB Output is correct
21 Correct 185 ms 1212 KB Output is correct
22 Correct 195 ms 1460 KB Output is correct
23 Correct 228 ms 1808 KB Output is correct
24 Correct 210 ms 1248 KB Output is correct
25 Correct 216 ms 1484 KB Output is correct
26 Correct 215 ms 1584 KB Output is correct
27 Correct 301 ms 12316 KB Output is correct
28 Correct 258 ms 7472 KB Output is correct
29 Correct 300 ms 11860 KB Output is correct
30 Correct 592 ms 29228 KB Output is correct
31 Correct 3 ms 340 KB Output is correct
32 Correct 408 ms 13548 KB Output is correct
33 Correct 733 ms 122400 KB Output is correct
34 Correct 307 ms 104304 KB Output is correct
35 Correct 650 ms 121112 KB Output is correct
36 Correct 374 ms 100820 KB Output is correct
37 Correct 191 ms 75056 KB Output is correct
38 Correct 260 ms 80768 KB Output is correct
39 Correct 154 ms 17280 KB Output is correct
40 Correct 162 ms 16200 KB Output is correct
41 Correct 100 ms 37932 KB Output is correct
42 Correct 163 ms 44424 KB Output is correct
43 Correct 807 ms 122444 KB Output is correct
44 Correct 335 ms 104304 KB Output is correct
45 Correct 656 ms 121116 KB Output is correct
46 Correct 385 ms 100728 KB Output is correct
47 Correct 174 ms 73540 KB Output is correct
48 Correct 316 ms 84252 KB Output is correct
49 Correct 776 ms 117588 KB Output is correct
50 Correct 568 ms 113000 KB Output is correct
51 Correct 637 ms 119684 KB Output is correct
52 Correct 185 ms 17312 KB Output is correct
53 Correct 160 ms 16208 KB Output is correct
54 Correct 99 ms 37956 KB Output is correct
55 Correct 157 ms 44416 KB Output is correct
56 Correct 778 ms 122496 KB Output is correct
57 Correct 321 ms 104352 KB Output is correct
58 Correct 686 ms 121120 KB Output is correct
59 Correct 368 ms 100724 KB Output is correct
60 Correct 176 ms 73516 KB Output is correct
61 Correct 316 ms 84344 KB Output is correct
62 Correct 727 ms 117740 KB Output is correct
63 Correct 523 ms 112972 KB Output is correct
64 Correct 562 ms 119680 KB Output is correct
65 Correct 203 ms 9932 KB Output is correct
66 Correct 287 ms 16748 KB Output is correct
67 Correct 289 ms 16240 KB Output is correct
68 Correct 221 ms 12020 KB Output is correct
69 Correct 227 ms 11636 KB Output is correct
70 Correct 57 ms 1980 KB Output is correct
71 Correct 268 ms 16640 KB Output is correct
72 Correct 283 ms 16228 KB Output is correct
73 Correct 225 ms 12128 KB Output is correct
74 Correct 202 ms 16088 KB Output is correct
75 Correct 194 ms 16740 KB Output is correct
76 Correct 211 ms 16136 KB Output is correct
77 Correct 172 ms 12024 KB Output is correct
78 Correct 236 ms 12228 KB Output is correct
79 Correct 462 ms 17344 KB Output is correct
80 Correct 514 ms 16184 KB Output is correct
81 Correct 347 ms 37952 KB Output is correct
82 Correct 378 ms 44444 KB Output is correct
83 Correct 962 ms 122504 KB Output is correct
84 Correct 563 ms 104260 KB Output is correct
85 Correct 932 ms 121100 KB Output is correct
86 Correct 624 ms 100772 KB Output is correct
87 Correct 341 ms 73608 KB Output is correct
88 Correct 487 ms 84352 KB Output is correct
89 Correct 859 ms 117900 KB Output is correct
90 Correct 854 ms 113076 KB Output is correct
91 Correct 760 ms 119860 KB Output is correct