Submission #250813

# Submission time Handle Problem Language Result Execution time Memory
250813 2020-07-19T08:16:31 Z atoiz Land of the Rainbow Gold (APIO17_rainbow) C++14
23 / 100
1142 ms 136232 KB
#include "rainbow.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
#include <set>

using namespace std;

template <typename T>
void normalize(vector<T> &vec)
{
	sort(vec.begin(), vec.end()), vec.erase(unique(vec.begin(), vec.end()), vec.end());
}

struct SegmentTree2D
{
	int nx;
	vector<int> valx;
	vector<vector<int>> valy, it;

	int pos(vector<int> &vec, int x) { return int(upper_bound(vec.begin(), vec.end(), x) - vec.begin() - 1); }

	void build(vector<pair<int, int>> vals)
	{
		valx.clear();
		for (auto p : vals) valx.push_back(p.first);
		normalize(valx);
		
		nx = (int) valx.size();
		valy.assign(nx * 2, vector<int>(0));
		it.resize(nx * 2);
		for (auto p : vals) {
			for (int i = pos(valx, p.first) + nx; i >= 1; i >>= 1) valy[i].push_back(p.second);
		}

		for (int i = nx * 2 - 1; i >= 1; --i) {
			normalize(valy[i]);
			int ny = (int) valy[i].size();
			it[i].assign(ny * 2, 0);
		}

		for (auto p : vals) {
			for (int x = pos(valx, p.first) + nx; x >= 1; x >>= 1) {
				int ny = (int) valy[x].size();
				for (int y = pos(valy[x], p.second) + ny; y >= 1; y >>= 1) {
					++it[x][y];
				}
			}
		}
	}

	int get1d(int x, int ly, int ry)
	{
		int ans = 0, ny = (int) valy[x].size();
		for (ly = pos(valy[x], ly - 1) + ny + 1, ry = pos(valy[x], ry) + ny + 1; ly < ry; ly >>= 1, ry >>= 1) {
			if (ly & 1) ans += it[x][ly++];
			if (ry & 1) ans += it[x][--ry];
		}
		return ans;
	}

	int get(int lx, int rx, int ly, int ry)
	{
		int ans = 0;
		for (lx = pos(valx, lx - 1) + nx + 1, rx = pos(valx, rx) + nx + 1; lx < rx; lx >>= 1, rx >>= 1) {
			if (lx & 1) ans += get1d(lx++, ly, ry);
			if (rx & 1) ans += get1d(--rx, ly, ry);
		}
		return ans;
	}
} hr, vt, sq, vx;
int minr, maxr, minc, maxc, fullans;

void init(int R, int C, int sr, int sc, int M, char *S) 
{
	vector<pair<int, int>> vxvec(M + 1);
	vxvec[0] = make_pair(sr, sc);
	minr = maxr = sr, minc = maxc = sc;
	for (int i = 0; i < M; ++i) {
		// cerr << S[i];
		if (S[i] == 'N') --sr;
		else if (S[i] == 'S') ++sr;
		else if (S[i] == 'W') --sc;
		else ++sc;
		minr = min(minr, sr), maxr = max(maxr, sr);
		minc = min(minc, sc), maxc = max(maxc, sc);
		vxvec[i + 1] = make_pair(sr, sc);
	}
	// cerr << endl;
	normalize(vxvec);

	auto hrvec = vxvec, vtvec = vxvec, sqvec = vxvec;
	for (auto p : vxvec) {
		hrvec.emplace_back(p.first, p.second - 1);
		vtvec.emplace_back(p.first - 1, p.second);
		sqvec.emplace_back(p.first, p.second - 1);
		sqvec.emplace_back(p.first - 1, p.second);
		sqvec.emplace_back(p.first - 1, p.second - 1);
	}
	normalize(hrvec), normalize(vtvec), normalize(sqvec);
	hr.build(hrvec), vt.build(vtvec), sq.build(sqvec), vx.build(vxvec);

	set<pair<int, int>> vxset(vxvec.begin(), vxvec.end());
	// for (auto p : vxvec) cerr << p.first << ' ' << p.second << endl; cout << " - " << endl;
	fullans = 0;
	for (auto p : sqvec) {
		bool b1 = (vxset.find(make_pair(p.first, p.second)) != vxset.end());
		bool b2 = (vxset.find(make_pair(p.first + 1, p.second)) != vxset.end());
		bool b3 = (vxset.find(make_pair(p.first, p.second + 1)) != vxset.end());
		bool b4 = (vxset.find(make_pair(p.first + 1, p.second + 1)) != vxset.end());
		int cur = (int) b1 + b2 + b3 + b4;
		if (cur == 1) ++fullans;
		if (cur == 3) --fullans;
		if (cur == 2 && ((b2 && b3) || (b1 && b4))) fullans += 2;
		// cerr << p.first << ' ' << p.second << ": " << cur << endl;
	}
	// if (fullans > 4 || fullans % 4) while (true);
	assert(fullans % 4 == 0);
	assert(fullans <= 4);
	fullans = 2 - fullans / 4;
	cerr << fullans << endl;
}

int colour(int ar, int ac, int br, int bc) 
{
	if (ar < minr && maxr < br && ac < minc && maxc < bc) return fullans;

	int64_t hrcnt = 1ll * (br - ar + 1) * (bc - ac) - hr.get(ar, br, ac, bc - 1);
	int64_t vtcnt = 1ll * (br - ar) * (bc - ac + 1) - vt.get(ar, br - 1, ac, bc);
	int64_t sqcnt = 1ll * (br - ar) * (bc - ac) - sq.get(ar, br - 1, ac, bc - 1);
	int64_t vxcnt = 1ll * (br - ar + 1) * (bc - ac + 1) - vx.get(ar, br, ac, bc);
	// cerr << hrcnt << ' ' << vtcnt << ' ' << sqcnt << ' ' << vxcnt << endl;
    return (int) (vxcnt + sqcnt - hrcnt - vtcnt);
}

# Verdict Execution time Memory Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 7 ms 768 KB Output is correct
3 Correct 4 ms 512 KB Output is correct
4 Correct 4 ms 512 KB Output is correct
5 Correct 8 ms 896 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 4 ms 512 KB Output is correct
12 Correct 7 ms 764 KB Output is correct
13 Correct 9 ms 1024 KB Output is correct
14 Correct 10 ms 1152 KB Output is correct
15 Correct 1 ms 256 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 0 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 336 ms 19032 KB Output is correct
4 Correct 491 ms 31276 KB Output is correct
5 Correct 498 ms 30784 KB Output is correct
6 Correct 375 ms 23960 KB Output is correct
7 Correct 395 ms 22788 KB Output is correct
8 Correct 86 ms 3548 KB Output is correct
9 Correct 519 ms 31276 KB Output is correct
10 Correct 488 ms 30784 KB Output is correct
11 Correct 395 ms 23960 KB Output is correct
12 Correct 407 ms 29508 KB Output is correct
13 Correct 403 ms 31276 KB Output is correct
14 Correct 404 ms 30784 KB Output is correct
15 Correct 336 ms 23960 KB Output is correct
16 Correct 353 ms 22192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 272 ms 26156 KB Output is correct
3 Correct 687 ms 136232 KB Output is correct
4 Correct 1142 ms 121164 KB Output is correct
5 Correct 668 ms 94080 KB Output is correct
6 Correct 137 ms 12772 KB Output is correct
7 Incorrect 271 ms 22356 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 7 ms 768 KB Output is correct
3 Correct 4 ms 512 KB Output is correct
4 Correct 4 ms 512 KB Output is correct
5 Correct 8 ms 896 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 4 ms 512 KB Output is correct
12 Correct 7 ms 764 KB Output is correct
13 Correct 9 ms 1024 KB Output is correct
14 Correct 10 ms 1152 KB Output is correct
15 Correct 1 ms 256 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 0 ms 384 KB Output is correct
18 Correct 1050 ms 29868 KB Output is correct
19 Correct 208 ms 4976 KB Output is correct
20 Correct 177 ms 4064 KB Output is correct
21 Correct 188 ms 4344 KB Output is correct
22 Correct 198 ms 4676 KB Output is correct
23 Correct 188 ms 4996 KB Output is correct
24 Incorrect 248 ms 4344 KB Output isn't correct
25 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 7 ms 768 KB Output is correct
3 Correct 4 ms 512 KB Output is correct
4 Correct 4 ms 512 KB Output is correct
5 Correct 8 ms 896 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 4 ms 512 KB Output is correct
12 Correct 7 ms 764 KB Output is correct
13 Correct 9 ms 1024 KB Output is correct
14 Correct 10 ms 1152 KB Output is correct
15 Correct 1 ms 256 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 0 ms 384 KB Output is correct
18 Correct 1050 ms 29868 KB Output is correct
19 Correct 208 ms 4976 KB Output is correct
20 Correct 177 ms 4064 KB Output is correct
21 Correct 188 ms 4344 KB Output is correct
22 Correct 198 ms 4676 KB Output is correct
23 Correct 188 ms 4996 KB Output is correct
24 Incorrect 248 ms 4344 KB Output isn't correct
25 Halted 0 ms 0 KB -