#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <random>
#include <iomanip>
#include <cassert>
#include <complex>
#include <functional>
using namespace std;
typedef long long ll;
pair <int, int> get(char c) {
if (c == 'N') return {-1, 0};
else if (c == 'S') return {1, 0};
else if (c == 'W') return {0, -1};
else return {0, 1};
}
int cnt(string s) {
set <pair <int, int>> vis;
int x = 0, y = 0;
vis.insert({x, y});
for (char c : s) {
x += get(c).first;
y += get(c).second;
vis.insert({x, y});
}
int cnt = 0;
for (auto p : vis) {
int x = p.first;
int y = p.second;
if (vis.count({x + 1, y}) && vis.count({x, y + 1}) && vis.count({x + 1, y + 1})) {
++cnt;
}
}
return cnt;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
int n, k;
cin >> n >> k;
string s;
cin >> s;
vector <int> px(n + 1), py(n + 1);
for (int i = 0; i < n; ++i) {
px[i + 1] = px[i] + get(s[i]).first;
py[i + 1] = py[i] + get(s[i]).second;
}
int cx = px.back();
int cy = py.back();
if (cx == 0 && cy == 0) {
cout << cnt(s) << '\n';
return 0;
}
if (cx == 0) {
swap(cx, cy);
for (int i = 0; i <= n; ++i) swap(px[i], py[i]);
}
if (cx < 0) {
cx *= -1;
for (int i = 0; i <= n; ++i) px[i] *= -1;
}
if (cy < 0) {
cy *= -1;
for (int i = 0; i <= n; ++i) py[i] *= -1;
}
map <pair <int, int>, vector <pair <int, int>>> mp;
auto get = [&] (int x, int y) {
int rem = (x % cx + cx) % cx;
int cnt = (rem - x) / cx;
y += cnt * cy;
x = rem;
return make_pair(x, y);
};
for (int i = 0; i <= n; ++i) {
auto pr = get(px[i], py[i]);
int x = pr.first, y = pr.second;
int cnt = (px[i] - x) / cx;
mp[{x, y}].push_back({cnt, cnt + k - 1});
mp[{x + cx, y + cy}].push_back({cnt - 1, cnt + k - 2});
mp[{x - cx, y - cy}].push_back({cnt + 1, cnt + k});
}
auto hv = [&] (int x, int y, int t) {
if (!mp.count({x, y})) return false;
for (auto p : mp[{x, y}]) {
if (p.first <= t && t <= p.second) return true;
}
return false;
};
for (auto &pr : mp) {
vector <pair <int, int>> ev;
for (auto [l, r] : pr.second) {
ev.emplace_back(l, -1);
ev.emplace_back(r, 1);
}
sort(ev.begin(), ev.end());
int bal = 0;
pr.second.clear();
int lst = 0;
for (auto p : ev) {
if (p.second == -1) {
++bal;
if (bal == 1) lst = p.first;
} else {
--bal;
if (bal == 0) pr.second.emplace_back(lst, p.first);
}
}
}
ll ans = 0;
for (auto pr : mp) {
int x = pr.first.first;
int y = pr.first.second;
if (make_pair(x, y) != get(x, y)) continue;
if (!mp.count({x, y}) || !mp.count({x + 1, y}) || !mp.count({x, y + 1}) || !mp.count({x + 1, y + 1})) continue;
vector <pair <int, int>> ev;
for (auto pr : mp[{x, y}]) {
ev.push_back({pr.first, -1});
ev.push_back({pr.second, 1});
}
for (auto pr : mp[{x + 1, y}]) {
ev.push_back({pr.first, -1});
ev.push_back({pr.second, 1});
}
for (auto pr : mp[{x, y + 1}]) {
ev.push_back({pr.first, -1});
ev.push_back({pr.second, 1});
}
for (auto pr : mp[{x + 1, y + 1}]) {
ev.push_back({pr.first, -1});
ev.push_back({pr.second, 1});
}
sort(ev.begin(), ev.end());
int lst = 0, bal = 0;
for (auto p : ev) {
if (p.second == -1) {
++bal;
if (bal == 4) lst = p.first;
} else {
--bal;
if (bal == 3) ans += p.first - lst + 1;
}
}
}
cout << ans << '\n';
}
Compilation message
2016_ho_t4.cpp: In function 'int main()':
2016_ho_t4.cpp:111:15: warning: decomposition declaration only available with -std=c++1z or -std=gnu++1z
for (auto [l, r] : pr.second) {
^
2016_ho_t4.cpp:101:8: warning: variable 'hv' set but not used [-Wunused-but-set-variable]
auto hv = [&] (int x, int y, int t) {
^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
508 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
376 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
6 ms |
376 KB |
Output is correct |
17 |
Correct |
5 ms |
380 KB |
Output is correct |
18 |
Correct |
5 ms |
376 KB |
Output is correct |
19 |
Correct |
5 ms |
376 KB |
Output is correct |
20 |
Correct |
5 ms |
376 KB |
Output is correct |
21 |
Correct |
5 ms |
380 KB |
Output is correct |
22 |
Correct |
5 ms |
376 KB |
Output is correct |
23 |
Correct |
5 ms |
376 KB |
Output is correct |
24 |
Correct |
5 ms |
380 KB |
Output is correct |
25 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
508 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
376 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
6 ms |
376 KB |
Output is correct |
17 |
Correct |
5 ms |
380 KB |
Output is correct |
18 |
Correct |
5 ms |
376 KB |
Output is correct |
19 |
Correct |
5 ms |
376 KB |
Output is correct |
20 |
Correct |
5 ms |
376 KB |
Output is correct |
21 |
Correct |
5 ms |
380 KB |
Output is correct |
22 |
Correct |
5 ms |
376 KB |
Output is correct |
23 |
Correct |
5 ms |
376 KB |
Output is correct |
24 |
Correct |
5 ms |
380 KB |
Output is correct |
25 |
Correct |
5 ms |
376 KB |
Output is correct |
26 |
Correct |
5 ms |
376 KB |
Output is correct |
27 |
Correct |
10 ms |
892 KB |
Output is correct |
28 |
Correct |
8 ms |
632 KB |
Output is correct |
29 |
Correct |
64 ms |
6392 KB |
Output is correct |
30 |
Correct |
72 ms |
6648 KB |
Output is correct |
31 |
Correct |
239 ms |
28920 KB |
Output is correct |
32 |
Correct |
22 ms |
2296 KB |
Output is correct |
33 |
Correct |
134 ms |
11384 KB |
Output is correct |
34 |
Correct |
137 ms |
11512 KB |
Output is correct |
35 |
Correct |
129 ms |
10872 KB |
Output is correct |
36 |
Correct |
101 ms |
8312 KB |
Output is correct |
37 |
Correct |
25 ms |
2552 KB |
Output is correct |
38 |
Correct |
121 ms |
10488 KB |
Output is correct |
39 |
Correct |
123 ms |
10872 KB |
Output is correct |
40 |
Correct |
22 ms |
2424 KB |
Output is correct |
41 |
Correct |
77 ms |
6264 KB |
Output is correct |
42 |
Correct |
91 ms |
8440 KB |
Output is correct |
43 |
Correct |
124 ms |
10616 KB |
Output is correct |
44 |
Correct |
123 ms |
10744 KB |
Output is correct |
45 |
Correct |
112 ms |
9816 KB |
Output is correct |
46 |
Correct |
125 ms |
13176 KB |
Output is correct |
47 |
Correct |
191 ms |
17784 KB |
Output is correct |
48 |
Correct |
85 ms |
7304 KB |
Output is correct |
49 |
Correct |
261 ms |
34296 KB |
Output is correct |
50 |
Correct |
250 ms |
34168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
508 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
376 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
6 ms |
376 KB |
Output is correct |
17 |
Correct |
5 ms |
380 KB |
Output is correct |
18 |
Correct |
5 ms |
376 KB |
Output is correct |
19 |
Correct |
5 ms |
376 KB |
Output is correct |
20 |
Correct |
5 ms |
376 KB |
Output is correct |
21 |
Correct |
5 ms |
380 KB |
Output is correct |
22 |
Correct |
5 ms |
376 KB |
Output is correct |
23 |
Correct |
5 ms |
376 KB |
Output is correct |
24 |
Correct |
5 ms |
380 KB |
Output is correct |
25 |
Correct |
5 ms |
376 KB |
Output is correct |
26 |
Correct |
5 ms |
376 KB |
Output is correct |
27 |
Correct |
5 ms |
376 KB |
Output is correct |
28 |
Correct |
5 ms |
376 KB |
Output is correct |
29 |
Correct |
5 ms |
376 KB |
Output is correct |
30 |
Correct |
5 ms |
376 KB |
Output is correct |
31 |
Correct |
5 ms |
376 KB |
Output is correct |
32 |
Correct |
5 ms |
376 KB |
Output is correct |
33 |
Correct |
5 ms |
376 KB |
Output is correct |
34 |
Correct |
5 ms |
376 KB |
Output is correct |
35 |
Correct |
5 ms |
508 KB |
Output is correct |
36 |
Correct |
5 ms |
376 KB |
Output is correct |
37 |
Correct |
5 ms |
376 KB |
Output is correct |
38 |
Correct |
5 ms |
376 KB |
Output is correct |
39 |
Correct |
5 ms |
376 KB |
Output is correct |
40 |
Correct |
5 ms |
376 KB |
Output is correct |
41 |
Correct |
5 ms |
376 KB |
Output is correct |
42 |
Correct |
5 ms |
376 KB |
Output is correct |
43 |
Correct |
5 ms |
376 KB |
Output is correct |
44 |
Correct |
5 ms |
248 KB |
Output is correct |
45 |
Correct |
5 ms |
376 KB |
Output is correct |
46 |
Correct |
5 ms |
376 KB |
Output is correct |
47 |
Correct |
5 ms |
376 KB |
Output is correct |
48 |
Correct |
5 ms |
376 KB |
Output is correct |
49 |
Correct |
5 ms |
376 KB |
Output is correct |
50 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
508 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
376 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
6 ms |
376 KB |
Output is correct |
17 |
Correct |
5 ms |
380 KB |
Output is correct |
18 |
Correct |
5 ms |
376 KB |
Output is correct |
19 |
Correct |
5 ms |
376 KB |
Output is correct |
20 |
Correct |
5 ms |
376 KB |
Output is correct |
21 |
Correct |
5 ms |
380 KB |
Output is correct |
22 |
Correct |
5 ms |
376 KB |
Output is correct |
23 |
Correct |
5 ms |
376 KB |
Output is correct |
24 |
Correct |
5 ms |
380 KB |
Output is correct |
25 |
Correct |
5 ms |
376 KB |
Output is correct |
26 |
Correct |
5 ms |
376 KB |
Output is correct |
27 |
Correct |
10 ms |
892 KB |
Output is correct |
28 |
Correct |
8 ms |
632 KB |
Output is correct |
29 |
Correct |
64 ms |
6392 KB |
Output is correct |
30 |
Correct |
72 ms |
6648 KB |
Output is correct |
31 |
Correct |
239 ms |
28920 KB |
Output is correct |
32 |
Correct |
22 ms |
2296 KB |
Output is correct |
33 |
Correct |
134 ms |
11384 KB |
Output is correct |
34 |
Correct |
137 ms |
11512 KB |
Output is correct |
35 |
Correct |
129 ms |
10872 KB |
Output is correct |
36 |
Correct |
101 ms |
8312 KB |
Output is correct |
37 |
Correct |
25 ms |
2552 KB |
Output is correct |
38 |
Correct |
121 ms |
10488 KB |
Output is correct |
39 |
Correct |
123 ms |
10872 KB |
Output is correct |
40 |
Correct |
22 ms |
2424 KB |
Output is correct |
41 |
Correct |
77 ms |
6264 KB |
Output is correct |
42 |
Correct |
91 ms |
8440 KB |
Output is correct |
43 |
Correct |
124 ms |
10616 KB |
Output is correct |
44 |
Correct |
123 ms |
10744 KB |
Output is correct |
45 |
Correct |
112 ms |
9816 KB |
Output is correct |
46 |
Correct |
125 ms |
13176 KB |
Output is correct |
47 |
Correct |
191 ms |
17784 KB |
Output is correct |
48 |
Correct |
85 ms |
7304 KB |
Output is correct |
49 |
Correct |
261 ms |
34296 KB |
Output is correct |
50 |
Correct |
250 ms |
34168 KB |
Output is correct |
51 |
Correct |
5 ms |
376 KB |
Output is correct |
52 |
Correct |
5 ms |
376 KB |
Output is correct |
53 |
Correct |
5 ms |
376 KB |
Output is correct |
54 |
Correct |
5 ms |
376 KB |
Output is correct |
55 |
Correct |
5 ms |
376 KB |
Output is correct |
56 |
Correct |
5 ms |
376 KB |
Output is correct |
57 |
Correct |
5 ms |
376 KB |
Output is correct |
58 |
Correct |
5 ms |
376 KB |
Output is correct |
59 |
Correct |
5 ms |
376 KB |
Output is correct |
60 |
Correct |
5 ms |
508 KB |
Output is correct |
61 |
Correct |
5 ms |
376 KB |
Output is correct |
62 |
Correct |
5 ms |
376 KB |
Output is correct |
63 |
Correct |
5 ms |
376 KB |
Output is correct |
64 |
Correct |
5 ms |
376 KB |
Output is correct |
65 |
Correct |
5 ms |
376 KB |
Output is correct |
66 |
Correct |
5 ms |
376 KB |
Output is correct |
67 |
Correct |
5 ms |
376 KB |
Output is correct |
68 |
Correct |
5 ms |
376 KB |
Output is correct |
69 |
Correct |
5 ms |
248 KB |
Output is correct |
70 |
Correct |
5 ms |
376 KB |
Output is correct |
71 |
Correct |
5 ms |
376 KB |
Output is correct |
72 |
Correct |
5 ms |
376 KB |
Output is correct |
73 |
Correct |
5 ms |
376 KB |
Output is correct |
74 |
Correct |
5 ms |
376 KB |
Output is correct |
75 |
Correct |
5 ms |
376 KB |
Output is correct |
76 |
Correct |
10 ms |
888 KB |
Output is correct |
77 |
Correct |
6 ms |
504 KB |
Output is correct |
78 |
Correct |
33 ms |
4216 KB |
Output is correct |
79 |
Correct |
131 ms |
16760 KB |
Output is correct |
80 |
Correct |
82 ms |
6776 KB |
Output is correct |
81 |
Correct |
24 ms |
2552 KB |
Output is correct |
82 |
Correct |
121 ms |
10224 KB |
Output is correct |
83 |
Correct |
72 ms |
6392 KB |
Output is correct |
84 |
Correct |
126 ms |
10744 KB |
Output is correct |
85 |
Correct |
130 ms |
10768 KB |
Output is correct |
86 |
Correct |
115 ms |
11660 KB |
Output is correct |
87 |
Correct |
139 ms |
12152 KB |
Output is correct |
88 |
Correct |
116 ms |
9848 KB |
Output is correct |
89 |
Correct |
119 ms |
10104 KB |
Output is correct |
90 |
Correct |
23 ms |
2552 KB |
Output is correct |
91 |
Correct |
132 ms |
11384 KB |
Output is correct |
92 |
Correct |
135 ms |
11512 KB |
Output is correct |
93 |
Correct |
120 ms |
11128 KB |
Output is correct |
94 |
Correct |
96 ms |
8828 KB |
Output is correct |
95 |
Correct |
94 ms |
8824 KB |
Output is correct |
96 |
Correct |
124 ms |
13048 KB |
Output is correct |
97 |
Correct |
201 ms |
17912 KB |
Output is correct |
98 |
Correct |
47 ms |
5604 KB |
Output is correct |
99 |
Correct |
253 ms |
34168 KB |
Output is correct |
100 |
Correct |
342 ms |
34424 KB |
Output is correct |