Submission #107389

# Submission time Handle Problem Language Result Execution time Memory
107389 2019-04-23T19:20:05 Z Noam527 Land of the Rainbow Gold (APIO17_rainbow) C++17
100 / 100
1332 ms 118892 KB
#include <bits/stdc++.h>
#define CHECK cout << "ok" << endl
#define finish(x) return cout << x << endl, 0
typedef long long ll;
typedef long double ldb;
const int md = 1e9 + 7;
const ll inf = 1e18;
const int OO = 0;
const int OOO = 1;
using namespace std;

int countless(const vector<int> &a, int x) {
	if (!a.size()) return 0;
	int lo = 0, hi = (int)a.size() - 1, mid;
	while (lo < hi) {
		mid = (lo + hi) / 2;
		if (x <= a[mid]) hi = mid;
		else lo = mid + 1;
	}
	if (x > a[lo]) lo++;
	return lo;
}
int between(const vector<int> &a, int lo, int hi) {
	return countless(a, hi + 1) - countless(a, lo);
}
struct segtree {
	int n;
	vector<vector<int>> t;
	segtree() {}
	segtree(int sz) {
		n = max(2, sz);
		while (n != (n & -n)) n += (n & -n);
		t.resize(2 * n);
	}
	// sorted by .second
	void init(const vector<pair<int, int>> &a) {
		for (const auto &i : a)
			addpoint(i.first, i.second);
	}
	void addpoint(int x, int y) {
		x += n - 1;
		t[x].push_back(y);
		x = (x - 1) / 2;
		while (x) {
			t[x].push_back(y);
			x = (x - 1) / 2;
		}
		t[0].push_back(y);
	}
	void process() {
		for (auto &i : t)
			sort(i.begin(), i.end());
	}
	// x in [l, r], y in [lo, hi]
	int query(int l, int r, int lo, int hi) {
		if (l > r || lo > hi) return 0;
		int rtn = 0;
		for (l += n - 1, r += n - 1; l < r; l = (l - 1) / 2, r = (r - 1) / 2) {
			if (!(l & 1)) rtn += between(t[l], lo, hi), l++;
			if (r & 1) rtn += between(t[r], lo, hi), r--;
		}
		if (l == r) rtn += between(t[l], lo, hi);
		return rtn;
	}
};

const int maxC = 200005;
segtree full(maxC), point(maxC), H(maxC), V(maxC);

bool cmp(const pair<int, int> &aa, const pair<int, int> &bb) {
	if (aa.second != bb.second) return aa.second < bb.second;
	return aa.first < bb.first;
}

int mn[2], mx[2];

void init(int R, int C, int sr, int sc, int M, char *S) {
	vector<pair<int, int>> a, b, c;
	a.emplace_back(sr, sc);
	mn[0] = mx[0] = sr;
	mn[1] = mx[1] = sc;
	for (int i = 0; i < M; i++) {
		if (S[i] == 'N') sr--;
		else if (S[i] == 'S') sr++;
		else if (S[i] == 'W') sc--;
		else sc++;
		mn[0] = min(mn[0], sr);
		mx[0] = max(mx[0], sr);
		mn[1] = min(mn[1], sc);
		mx[1] = max(mx[1], sc);
		a.emplace_back(sr, sc);
	}
	auto clean = [](vector<pair<int, int>> &aa) {
		sort(aa.begin(), aa.end(), cmp);
		aa.resize(unique(aa.begin(), aa.end()) - aa.begin());
	};
	clean(a);
	full.init(a);

	b.clear();
	b.reserve(4 * a.size());
	for (const auto &i : a) {
		b.emplace_back(i.first + 0, i.second + 0);
		b.emplace_back(i.first + 0, i.second + 1);
		b.emplace_back(i.first + 1, i.second + 0);
		b.emplace_back(i.first + 1, i.second + 1);
	}
	clean(b);
	point.init(b);

	b.clear();
	b.reserve(2 * a.size());
	c.reserve(2 * a.size());
	for (const auto &i : a) {
		b.emplace_back(i.first + 0, i.second + 0);
		c.emplace_back(i.first + 0, i.second + 0);
		b.emplace_back(i.first + 1, i.second + 0);
		c.emplace_back(i.first + 0, i.second + 1);
	}
	clean(b);
	clean(c);
	H.init(b);
	V.init(c);
	if (OO) {
		cout << "for V:\n";
		for (const auto &i : c) cout << i.first << " " << i.second << '\n';
		cout << V.query(3, 4, 3, 4) << '\n';
	}
}

int colour(int ar, int ac, int br, int bc) {
	if (ar < mn[0] && mx[0] < br && ac < mn[1] && mx[1] < bc)
		ar = mn[0];

	ll v = 0, e = 0;
	v += 2 * (br - ar + 1 + bc - ac + 1);
	v += point.query(ar + 1, br, ac + 1, bc);

	e += 2 * (br - ar + 1 + bc - ac + 1);
	e += H.query(ar + 1, br, ac, bc);
	e += V.query(ar, br, ac + 1, bc);

	if (OO) cout << "v: " << v << '\n';
	if (OO) cout << "e: " << e << '\n';
	if (OO) cout << "full: " << full.query(ar, br, ac, bc) << '\n';
	if (OO) cout << "H: " << H.query(ar + 1, br, ac, bc) << '\n';
	if (OO) cout << "V: " << V.query(ar, br, ac + 1, bc) << '\n';

	return e - v + 1 - full.query(ar, br, ac, bc);
}
/*
int main() {
	int R, C, sr, sc, n;
	cin >> R >> C >> sr >> sc >> n;
	char *S = new char[n];
	cin >> S;
	init(R, C, sr, sc, n, S);
	while (1) {
		int ar, ac, br, bc;
		cout << "top left: ";
		cin >> ar >> ac;
		cout << "bottom right: ";
		cin >> br >> bc;
		cout << colour(ar, ac, br, bc) << '\n';
	}
}
*/
# Verdict Execution time Memory Grader output
1 Correct 50 ms 49656 KB Output is correct
2 Correct 51 ms 50168 KB Output is correct
3 Correct 52 ms 49656 KB Output is correct
4 Correct 45 ms 49784 KB Output is correct
5 Correct 59 ms 50236 KB Output is correct
6 Correct 42 ms 49532 KB Output is correct
7 Correct 43 ms 49524 KB Output is correct
8 Correct 52 ms 49656 KB Output is correct
9 Correct 44 ms 49684 KB Output is correct
10 Correct 47 ms 49640 KB Output is correct
11 Correct 47 ms 49792 KB Output is correct
12 Correct 47 ms 49912 KB Output is correct
13 Correct 49 ms 50424 KB Output is correct
14 Correct 52 ms 50680 KB Output is correct
15 Correct 45 ms 49628 KB Output is correct
16 Correct 43 ms 49636 KB Output is correct
17 Correct 47 ms 49528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 43 ms 49636 KB Output is correct
2 Correct 47 ms 49528 KB Output is correct
3 Correct 285 ms 82288 KB Output is correct
4 Correct 350 ms 108524 KB Output is correct
5 Correct 388 ms 107500 KB Output is correct
6 Correct 316 ms 99948 KB Output is correct
7 Correct 343 ms 95680 KB Output is correct
8 Correct 121 ms 51160 KB Output is correct
9 Correct 402 ms 107884 KB Output is correct
10 Correct 406 ms 107500 KB Output is correct
11 Correct 350 ms 100152 KB Output is correct
12 Correct 308 ms 105420 KB Output is correct
13 Correct 316 ms 107188 KB Output is correct
14 Correct 328 ms 108120 KB Output is correct
15 Correct 315 ms 99916 KB Output is correct
16 Correct 304 ms 99032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 49628 KB Output is correct
2 Correct 216 ms 108680 KB Output is correct
3 Correct 417 ms 118552 KB Output is correct
4 Correct 319 ms 112720 KB Output is correct
5 Correct 240 ms 98800 KB Output is correct
6 Correct 97 ms 62192 KB Output is correct
7 Correct 123 ms 72704 KB Output is correct
8 Correct 214 ms 105240 KB Output is correct
9 Correct 227 ms 100844 KB Output is correct
10 Correct 97 ms 62704 KB Output is correct
11 Correct 159 ms 80800 KB Output is correct
12 Correct 251 ms 108764 KB Output is correct
13 Correct 344 ms 118508 KB Output is correct
14 Correct 282 ms 112752 KB Output is correct
15 Correct 244 ms 98796 KB Output is correct
16 Correct 97 ms 59884 KB Output is correct
17 Correct 126 ms 73856 KB Output is correct
18 Correct 233 ms 110600 KB Output is correct
19 Correct 256 ms 113644 KB Output is correct
20 Correct 285 ms 113400 KB Output is correct
21 Correct 206 ms 105324 KB Output is correct
22 Correct 200 ms 100716 KB Output is correct
23 Correct 92 ms 62704 KB Output is correct
24 Correct 155 ms 80824 KB Output is correct
25 Correct 219 ms 108768 KB Output is correct
26 Correct 397 ms 118764 KB Output is correct
27 Correct 332 ms 112776 KB Output is correct
28 Correct 220 ms 98768 KB Output is correct
29 Correct 96 ms 59884 KB Output is correct
30 Correct 146 ms 73780 KB Output is correct
31 Correct 264 ms 110444 KB Output is correct
32 Correct 301 ms 113644 KB Output is correct
33 Correct 309 ms 113388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 50 ms 49656 KB Output is correct
2 Correct 51 ms 50168 KB Output is correct
3 Correct 52 ms 49656 KB Output is correct
4 Correct 45 ms 49784 KB Output is correct
5 Correct 59 ms 50236 KB Output is correct
6 Correct 42 ms 49532 KB Output is correct
7 Correct 43 ms 49524 KB Output is correct
8 Correct 52 ms 49656 KB Output is correct
9 Correct 44 ms 49684 KB Output is correct
10 Correct 47 ms 49640 KB Output is correct
11 Correct 47 ms 49792 KB Output is correct
12 Correct 47 ms 49912 KB Output is correct
13 Correct 49 ms 50424 KB Output is correct
14 Correct 52 ms 50680 KB Output is correct
15 Correct 45 ms 49628 KB Output is correct
16 Correct 43 ms 49636 KB Output is correct
17 Correct 47 ms 49528 KB Output is correct
18 Correct 1332 ms 80084 KB Output is correct
19 Correct 208 ms 52600 KB Output is correct
20 Correct 192 ms 51320 KB Output is correct
21 Correct 220 ms 51448 KB Output is correct
22 Correct 262 ms 51832 KB Output is correct
23 Correct 211 ms 52476 KB Output is correct
24 Correct 252 ms 51448 KB Output is correct
25 Correct 241 ms 51964 KB Output is correct
26 Correct 257 ms 52088 KB Output is correct
27 Correct 448 ms 76524 KB Output is correct
28 Correct 423 ms 62572 KB Output is correct
29 Correct 494 ms 72812 KB Output is correct
30 Correct 759 ms 110828 KB Output is correct
31 Correct 54 ms 49784 KB Output is correct
32 Correct 729 ms 77932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 50 ms 49656 KB Output is correct
2 Correct 51 ms 50168 KB Output is correct
3 Correct 52 ms 49656 KB Output is correct
4 Correct 45 ms 49784 KB Output is correct
5 Correct 59 ms 50236 KB Output is correct
6 Correct 42 ms 49532 KB Output is correct
7 Correct 43 ms 49524 KB Output is correct
8 Correct 52 ms 49656 KB Output is correct
9 Correct 44 ms 49684 KB Output is correct
10 Correct 47 ms 49640 KB Output is correct
11 Correct 47 ms 49792 KB Output is correct
12 Correct 47 ms 49912 KB Output is correct
13 Correct 49 ms 50424 KB Output is correct
14 Correct 52 ms 50680 KB Output is correct
15 Correct 45 ms 49628 KB Output is correct
16 Correct 43 ms 49636 KB Output is correct
17 Correct 47 ms 49528 KB Output is correct
18 Correct 1332 ms 80084 KB Output is correct
19 Correct 208 ms 52600 KB Output is correct
20 Correct 192 ms 51320 KB Output is correct
21 Correct 220 ms 51448 KB Output is correct
22 Correct 262 ms 51832 KB Output is correct
23 Correct 211 ms 52476 KB Output is correct
24 Correct 252 ms 51448 KB Output is correct
25 Correct 241 ms 51964 KB Output is correct
26 Correct 257 ms 52088 KB Output is correct
27 Correct 448 ms 76524 KB Output is correct
28 Correct 423 ms 62572 KB Output is correct
29 Correct 494 ms 72812 KB Output is correct
30 Correct 759 ms 110828 KB Output is correct
31 Correct 54 ms 49784 KB Output is correct
32 Correct 729 ms 77932 KB Output is correct
33 Correct 216 ms 108680 KB Output is correct
34 Correct 417 ms 118552 KB Output is correct
35 Correct 319 ms 112720 KB Output is correct
36 Correct 240 ms 98800 KB Output is correct
37 Correct 97 ms 62192 KB Output is correct
38 Correct 123 ms 72704 KB Output is correct
39 Correct 214 ms 105240 KB Output is correct
40 Correct 227 ms 100844 KB Output is correct
41 Correct 97 ms 62704 KB Output is correct
42 Correct 159 ms 80800 KB Output is correct
43 Correct 251 ms 108764 KB Output is correct
44 Correct 344 ms 118508 KB Output is correct
45 Correct 282 ms 112752 KB Output is correct
46 Correct 244 ms 98796 KB Output is correct
47 Correct 97 ms 59884 KB Output is correct
48 Correct 126 ms 73856 KB Output is correct
49 Correct 233 ms 110600 KB Output is correct
50 Correct 256 ms 113644 KB Output is correct
51 Correct 285 ms 113400 KB Output is correct
52 Correct 206 ms 105324 KB Output is correct
53 Correct 200 ms 100716 KB Output is correct
54 Correct 92 ms 62704 KB Output is correct
55 Correct 155 ms 80824 KB Output is correct
56 Correct 219 ms 108768 KB Output is correct
57 Correct 397 ms 118764 KB Output is correct
58 Correct 332 ms 112776 KB Output is correct
59 Correct 220 ms 98768 KB Output is correct
60 Correct 96 ms 59884 KB Output is correct
61 Correct 146 ms 73780 KB Output is correct
62 Correct 264 ms 110444 KB Output is correct
63 Correct 301 ms 113644 KB Output is correct
64 Correct 309 ms 113388 KB Output is correct
65 Correct 499 ms 105452 KB Output is correct
66 Correct 556 ms 100972 KB Output is correct
67 Correct 347 ms 62904 KB Output is correct
68 Correct 491 ms 81028 KB Output is correct
69 Correct 497 ms 108856 KB Output is correct
70 Correct 1274 ms 118892 KB Output is correct
71 Correct 715 ms 112748 KB Output is correct
72 Correct 677 ms 98908 KB Output is correct
73 Correct 394 ms 60112 KB Output is correct
74 Correct 447 ms 73968 KB Output is correct
75 Correct 598 ms 110700 KB Output is correct
76 Correct 959 ms 113744 KB Output is correct
77 Correct 1080 ms 113516 KB Output is correct
78 Correct 285 ms 82288 KB Output is correct
79 Correct 350 ms 108524 KB Output is correct
80 Correct 388 ms 107500 KB Output is correct
81 Correct 316 ms 99948 KB Output is correct
82 Correct 343 ms 95680 KB Output is correct
83 Correct 121 ms 51160 KB Output is correct
84 Correct 402 ms 107884 KB Output is correct
85 Correct 406 ms 107500 KB Output is correct
86 Correct 350 ms 100152 KB Output is correct
87 Correct 308 ms 105420 KB Output is correct
88 Correct 316 ms 107188 KB Output is correct
89 Correct 328 ms 108120 KB Output is correct
90 Correct 315 ms 99916 KB Output is correct
91 Correct 304 ms 99032 KB Output is correct