Submission #533648

# Submission time Handle Problem Language Result Execution time Memory
533648 2022-03-06T18:46:10 Z rk42745417 None (JOI16_ho_t4) C++17
100 / 100
77 ms 8360 KB
#include <bits/stdc++.h>
using namespace std;

#define EmiliaMyWife ios::sync_with_stdio(0); cin.tie(0);
using ll = int64_t;
using ull = uint64_t;
using uint = uint32_t;
using ld = long double;
const int INF = 0x3f3f3f3f;
const ll LINF = ll(4e18) + ll(2e15);
const int MOD = 1e9 + 7;
const double EPS = 1e-8;
static int LamyIsCute = []() {
	EmiliaMyWife
	return 48763;
}();

pair<int, int> operator+(pair<int, int> a, pair<int, int> b) {
	return make_pair(a.first + b.first, a.second + b.second);
}

pair<int, int> get_direction(char c) {
	if(c == 'E')
		return {1, 0};
	if(c == 'W')
		return {-1, 0};
	if(c == 'N')
		return {0, 1};
	if(c == 'S')
		return {0, -1};
	return {0, 0};
}
pair<int, int> get_vector(const string &s) {
	pair<int, int> res = {0, 0};
	for(char c : s)
		res = res + get_direction(c);
	return res;
}
pair<int, int> simplify(string &s) {
	auto [a, b] = get_vector(s);
	if(a == 0) {
		for(char &c : s) {
			if(c == 'E')
				c = 'N';
			else if(c == 'W')
				c = 'S';
			else if(c == 'N')
				c = 'E';
			else // c == 'S'
				c = 'W';
		}
		swap(a, b);
	}
	if(a < 0) {
		for(char &c : s) {
			if(c == 'E')
				c = 'W';
			else if(c == 'W')
				c = 'E';
		}
		a = -a;
	}
	if(b < 0) {
		for(char &c : s) {
			if(c == 'S')
				c = 'N';
			else if(c == 'N')
				c = 'S';
		}
		b = -b;
	}
	return {a, b};
}
int get_ans(const string &s) {
	set<pair<int, int>> has;
	pair<int, int> res = {0, 0};
	has.insert(res);
	for(char c : s) {
		res = res + get_direction(c);
		has.insert(res);
	}
	int ans = 0;
	for(const auto &[a, b] : has)
		ans += has.count({a, b + 1}) && has.count({a + 1, b}) && has.count({a + 1, b + 1});
	return ans;
}

struct dat {
	int p, q, l, r;
	bool operator<(dat other) {
		auto a = make_pair(p, q), b = make_pair(other.p, other.q);
		if(a == b)
			return l < other.l;
		return a < b;
	}
	dat() { }
	dat(int a, int b, int c, int d): p(a), q(b), l(c), r(d) { }
};

signed main() {
	int n, k;
	cin >> n >> k;
	string s;
	cin >> s;

	auto [a, b] = simplify(s);
	if(a == 0)
		return cout << get_ans(s) << '\n', 0;

	vector<dat> arr;
	pair<int, int> res = {0, 0};
	set<pair<int, int>> has;
	for(char c : s) {
		res = res + get_direction(c);
		if(has.count(res))
			continue;
		has.insert(res);

		auto [x, y] = res;
		int w = (x > 0) ? x / a : (x - a + 1) / a;
		arr.push_back({x - a * w, y - b * w, w, w + k - 1});
		if(x - a * w == 0) {
			w--;
			arr.push_back({x - a * w, y - b * w, w, w + k - 1});
		}
	}
	if(!has.count({0, 0})) {
		arr.push_back({0, 0, 0, 0});
		arr.push_back({a, b, -1, -1});
	}
	sort(arr.begin(), arr.end());

	ll ans = 0;
	res = {-INF, -INF};
	for(const auto &[x, y, _, __] : arr) {
		if(make_pair(x, y) == res)
			continue;
		res = {x, y};

		pair<int, int> pos[4] = {res, res + make_pair(0, 1), res + make_pair(1, 0), res + make_pair(1, 1)};
		int it[4], mx[4] = {-INF, -INF, -INF, -INF};

		auto check = [&](int p, int id) {
			return p >= 0 && p < arr.size() && make_pair(arr[p].p, arr[p].q) == pos[id];
		};

		for(int i = 0; i < 4; i++)
			it[i] = lower_bound(arr.begin(), arr.end(), dat(pos[i].first, pos[i].second, -INF, -INF)) - arr.begin();

		auto update = [&](int val) {
			for(int i = 0; i < 4; i++) {
				while(check(it[i], i) && arr[it[i]].l <= val) {
					mx[i] = max(mx[i], arr[it[i]].r);
					it[i]++;
				}
			}
		};

		int prv = -INF;
		while(check(it[0], 0) || check(it[1], 1) || check(it[2], 2) || check(it[3], 3)) {
			int l = INF;
			for(int i = 0; i < 4; i++)
				if(check(it[i], i))
					l = min(l, arr[it[i]].l);
			l = max(prv, l);
			update(l);
			int r = INF;
			for(int i = 0; i < 4; i++)
				r = min(r, mx[i]);
			if(l <= r) {
				ans += (r - l + 1);
				prv = r + 1;
			}
		}
	}
	cout << ans << '\n';
}

Compilation message

2016_ho_t4.cpp: In lambda function:
2016_ho_t4.cpp:144:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<dat>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  144 |    return p >= 0 && p < arr.size() && make_pair(arr[p].p, arr[p].q) == pos[id];
      |                     ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 312 KB Output is correct
7 Correct 1 ms 312 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 312 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 1 ms 316 KB Output is correct
19 Correct 1 ms 312 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 312 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 0 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 312 KB Output is correct
7 Correct 1 ms 312 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 312 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 1 ms 316 KB Output is correct
19 Correct 1 ms 312 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 312 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 0 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 1 ms 320 KB Output is correct
28 Correct 3 ms 460 KB Output is correct
29 Correct 13 ms 1212 KB Output is correct
30 Correct 16 ms 1376 KB Output is correct
31 Correct 56 ms 6204 KB Output is correct
32 Correct 15 ms 1336 KB Output is correct
33 Correct 24 ms 2048 KB Output is correct
34 Correct 24 ms 2184 KB Output is correct
35 Correct 24 ms 1972 KB Output is correct
36 Correct 19 ms 1784 KB Output is correct
37 Correct 16 ms 1484 KB Output is correct
38 Correct 22 ms 1844 KB Output is correct
39 Correct 22 ms 1968 KB Output is correct
40 Correct 15 ms 1444 KB Output is correct
41 Correct 15 ms 1716 KB Output is correct
42 Correct 16 ms 1464 KB Output is correct
43 Correct 22 ms 2068 KB Output is correct
44 Correct 21 ms 1824 KB Output is correct
45 Correct 18 ms 1724 KB Output is correct
46 Correct 38 ms 4388 KB Output is correct
47 Correct 44 ms 4412 KB Output is correct
48 Correct 40 ms 8360 KB Output is correct
49 Correct 54 ms 6732 KB Output is correct
50 Correct 55 ms 6680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 312 KB Output is correct
7 Correct 1 ms 312 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 312 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 1 ms 316 KB Output is correct
19 Correct 1 ms 312 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 312 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 0 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 1 ms 204 KB Output is correct
28 Correct 1 ms 308 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 1 ms 204 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 1 ms 316 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 1 ms 204 KB Output is correct
35 Correct 1 ms 204 KB Output is correct
36 Correct 0 ms 204 KB Output is correct
37 Correct 1 ms 316 KB Output is correct
38 Correct 0 ms 316 KB Output is correct
39 Correct 1 ms 204 KB Output is correct
40 Correct 1 ms 204 KB Output is correct
41 Correct 1 ms 204 KB Output is correct
42 Correct 1 ms 204 KB Output is correct
43 Correct 1 ms 204 KB Output is correct
44 Correct 0 ms 204 KB Output is correct
45 Correct 1 ms 204 KB Output is correct
46 Correct 1 ms 204 KB Output is correct
47 Correct 1 ms 204 KB Output is correct
48 Correct 1 ms 204 KB Output is correct
49 Correct 1 ms 204 KB Output is correct
50 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 312 KB Output is correct
7 Correct 1 ms 312 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 312 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 1 ms 316 KB Output is correct
19 Correct 1 ms 312 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 312 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 0 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 1 ms 320 KB Output is correct
28 Correct 3 ms 460 KB Output is correct
29 Correct 13 ms 1212 KB Output is correct
30 Correct 16 ms 1376 KB Output is correct
31 Correct 56 ms 6204 KB Output is correct
32 Correct 15 ms 1336 KB Output is correct
33 Correct 24 ms 2048 KB Output is correct
34 Correct 24 ms 2184 KB Output is correct
35 Correct 24 ms 1972 KB Output is correct
36 Correct 19 ms 1784 KB Output is correct
37 Correct 16 ms 1484 KB Output is correct
38 Correct 22 ms 1844 KB Output is correct
39 Correct 22 ms 1968 KB Output is correct
40 Correct 15 ms 1444 KB Output is correct
41 Correct 15 ms 1716 KB Output is correct
42 Correct 16 ms 1464 KB Output is correct
43 Correct 22 ms 2068 KB Output is correct
44 Correct 21 ms 1824 KB Output is correct
45 Correct 18 ms 1724 KB Output is correct
46 Correct 38 ms 4388 KB Output is correct
47 Correct 44 ms 4412 KB Output is correct
48 Correct 40 ms 8360 KB Output is correct
49 Correct 54 ms 6732 KB Output is correct
50 Correct 55 ms 6680 KB Output is correct
51 Correct 1 ms 204 KB Output is correct
52 Correct 1 ms 204 KB Output is correct
53 Correct 1 ms 308 KB Output is correct
54 Correct 1 ms 204 KB Output is correct
55 Correct 1 ms 204 KB Output is correct
56 Correct 1 ms 204 KB Output is correct
57 Correct 1 ms 316 KB Output is correct
58 Correct 1 ms 332 KB Output is correct
59 Correct 1 ms 204 KB Output is correct
60 Correct 1 ms 204 KB Output is correct
61 Correct 0 ms 204 KB Output is correct
62 Correct 1 ms 316 KB Output is correct
63 Correct 0 ms 316 KB Output is correct
64 Correct 1 ms 204 KB Output is correct
65 Correct 1 ms 204 KB Output is correct
66 Correct 1 ms 204 KB Output is correct
67 Correct 1 ms 204 KB Output is correct
68 Correct 1 ms 204 KB Output is correct
69 Correct 0 ms 204 KB Output is correct
70 Correct 1 ms 204 KB Output is correct
71 Correct 1 ms 204 KB Output is correct
72 Correct 1 ms 204 KB Output is correct
73 Correct 1 ms 204 KB Output is correct
74 Correct 1 ms 204 KB Output is correct
75 Correct 1 ms 204 KB Output is correct
76 Correct 2 ms 332 KB Output is correct
77 Correct 2 ms 332 KB Output is correct
78 Correct 10 ms 1732 KB Output is correct
79 Correct 30 ms 3524 KB Output is correct
80 Correct 16 ms 1724 KB Output is correct
81 Correct 16 ms 1612 KB Output is correct
82 Correct 21 ms 1824 KB Output is correct
83 Correct 14 ms 1412 KB Output is correct
84 Correct 21 ms 1992 KB Output is correct
85 Correct 22 ms 1912 KB Output is correct
86 Correct 55 ms 8268 KB Output is correct
87 Correct 24 ms 2200 KB Output is correct
88 Correct 21 ms 1740 KB Output is correct
89 Correct 22 ms 1864 KB Output is correct
90 Correct 15 ms 1608 KB Output is correct
91 Correct 22 ms 1992 KB Output is correct
92 Correct 23 ms 1992 KB Output is correct
93 Correct 24 ms 1992 KB Output is correct
94 Correct 20 ms 1772 KB Output is correct
95 Correct 18 ms 1740 KB Output is correct
96 Correct 36 ms 4324 KB Output is correct
97 Correct 44 ms 4416 KB Output is correct
98 Correct 41 ms 8236 KB Output is correct
99 Correct 55 ms 6672 KB Output is correct
100 Correct 77 ms 8340 KB Output is correct