Submission #533715

# Submission time Handle Problem Language Result Execution time Memory
533715 2022-03-07T03:42:48 Z rk42745417 None (JOI16_ho_t4) C++17
100 / 100
49 ms 4796 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};
	arr.push_back({0, 0, 0, 0});
	arr.push_back({a, b, -1, -1});

	for(char c : s) {
		res = res + get_direction(c);

		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});
		}
	}
	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:139:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<dat>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  139 |    return p >= 0 && p < arr.size() && make_pair(arr[p].p, arr[p].q) == pos[id];
      |                     ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 316 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 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 1 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 1 ms 248 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 312 KB Output is correct
20 Correct 0 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 1 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 316 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 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 1 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 1 ms 248 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 312 KB Output is correct
20 Correct 0 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 1 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 1 ms 312 KB Output is correct
27 Correct 2 ms 460 KB Output is correct
28 Correct 3 ms 428 KB Output is correct
29 Correct 12 ms 1604 KB Output is correct
30 Correct 15 ms 2624 KB Output is correct
31 Correct 39 ms 2752 KB Output is correct
32 Correct 15 ms 1436 KB Output is correct
33 Correct 22 ms 2752 KB Output is correct
34 Correct 21 ms 2752 KB Output is correct
35 Correct 21 ms 2740 KB Output is correct
36 Correct 19 ms 2752 KB Output is correct
37 Correct 16 ms 1484 KB Output is correct
38 Correct 22 ms 2752 KB Output is correct
39 Correct 22 ms 2712 KB Output is correct
40 Correct 16 ms 1484 KB Output is correct
41 Correct 17 ms 2752 KB Output is correct
42 Correct 17 ms 2752 KB Output is correct
43 Correct 20 ms 2752 KB Output is correct
44 Correct 20 ms 2752 KB Output is correct
45 Correct 18 ms 2744 KB Output is correct
46 Correct 42 ms 4748 KB Output is correct
47 Correct 37 ms 4708 KB Output is correct
48 Correct 21 ms 4796 KB Output is correct
49 Correct 36 ms 2752 KB Output is correct
50 Correct 30 ms 2752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 316 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 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 1 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 1 ms 248 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 312 KB Output is correct
20 Correct 0 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 1 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 204 KB Output is correct
29 Correct 1 ms 308 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 312 KB Output is correct
33 Correct 1 ms 204 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 204 KB Output is correct
38 Correct 1 ms 204 KB Output is correct
39 Correct 1 ms 312 KB Output is correct
40 Correct 1 ms 204 KB Output is correct
41 Correct 1 ms 312 KB Output is correct
42 Correct 1 ms 204 KB Output is correct
43 Correct 0 ms 204 KB Output is correct
44 Correct 1 ms 312 KB Output is correct
45 Correct 0 ms 204 KB Output is correct
46 Correct 1 ms 316 KB Output is correct
47 Correct 1 ms 204 KB Output is correct
48 Correct 0 ms 204 KB Output is correct
49 Correct 1 ms 204 KB Output is correct
50 Correct 1 ms 316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 316 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 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 1 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 1 ms 248 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 312 KB Output is correct
20 Correct 0 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 1 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 1 ms 312 KB Output is correct
27 Correct 2 ms 460 KB Output is correct
28 Correct 3 ms 428 KB Output is correct
29 Correct 12 ms 1604 KB Output is correct
30 Correct 15 ms 2624 KB Output is correct
31 Correct 39 ms 2752 KB Output is correct
32 Correct 15 ms 1436 KB Output is correct
33 Correct 22 ms 2752 KB Output is correct
34 Correct 21 ms 2752 KB Output is correct
35 Correct 21 ms 2740 KB Output is correct
36 Correct 19 ms 2752 KB Output is correct
37 Correct 16 ms 1484 KB Output is correct
38 Correct 22 ms 2752 KB Output is correct
39 Correct 22 ms 2712 KB Output is correct
40 Correct 16 ms 1484 KB Output is correct
41 Correct 17 ms 2752 KB Output is correct
42 Correct 17 ms 2752 KB Output is correct
43 Correct 20 ms 2752 KB Output is correct
44 Correct 20 ms 2752 KB Output is correct
45 Correct 18 ms 2744 KB Output is correct
46 Correct 42 ms 4748 KB Output is correct
47 Correct 37 ms 4708 KB Output is correct
48 Correct 21 ms 4796 KB Output is correct
49 Correct 36 ms 2752 KB Output is correct
50 Correct 30 ms 2752 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 204 KB Output is correct
54 Correct 1 ms 308 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 312 KB Output is correct
58 Correct 1 ms 204 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 204 KB Output is correct
63 Correct 1 ms 204 KB Output is correct
64 Correct 1 ms 312 KB Output is correct
65 Correct 1 ms 204 KB Output is correct
66 Correct 1 ms 312 KB Output is correct
67 Correct 1 ms 204 KB Output is correct
68 Correct 0 ms 204 KB Output is correct
69 Correct 1 ms 312 KB Output is correct
70 Correct 0 ms 204 KB Output is correct
71 Correct 1 ms 316 KB Output is correct
72 Correct 1 ms 204 KB Output is correct
73 Correct 0 ms 204 KB Output is correct
74 Correct 1 ms 204 KB Output is correct
75 Correct 1 ms 316 KB Output is correct
76 Correct 2 ms 692 KB Output is correct
77 Correct 2 ms 332 KB Output is correct
78 Correct 7 ms 980 KB Output is correct
79 Correct 21 ms 2624 KB Output is correct
80 Correct 16 ms 2688 KB Output is correct
81 Correct 17 ms 1572 KB Output is correct
82 Correct 21 ms 2752 KB Output is correct
83 Correct 16 ms 2740 KB Output is correct
84 Correct 21 ms 2736 KB Output is correct
85 Correct 22 ms 2752 KB Output is correct
86 Correct 35 ms 4788 KB Output is correct
87 Correct 22 ms 2740 KB Output is correct
88 Correct 20 ms 2656 KB Output is correct
89 Correct 21 ms 2748 KB Output is correct
90 Correct 16 ms 1592 KB Output is correct
91 Correct 21 ms 2752 KB Output is correct
92 Correct 21 ms 2752 KB Output is correct
93 Correct 21 ms 2752 KB Output is correct
94 Correct 20 ms 2740 KB Output is correct
95 Correct 18 ms 2752 KB Output is correct
96 Correct 42 ms 4716 KB Output is correct
97 Correct 37 ms 4796 KB Output is correct
98 Correct 19 ms 4796 KB Output is correct
99 Correct 29 ms 2752 KB Output is correct
100 Correct 49 ms 4788 KB Output is correct