#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const ll infll = (ll)1e18 + 123;
int n, K, dx[] = { -1, 0, 1, 0 }, dy[] = { 0, 1, 0, -1 }, dir[200], a, b;
ll ans;
vector< array<ll, 4> > vecAns;
string s;
struct Type {
ll r_x, r_y, q_f, q_l;
Type (ll _r_x = 0, ll _r_y = 0, ll _q_f = 0, ll _q_l = 0) : r_x(_r_x), r_y(_r_y), q_f(_q_f), q_l(_q_l) {}
bool operator< (const Type &_) const { return array<ll, 4>{ r_x, r_y, q_f, q_l } < array<ll, 4>{ _.r_x, _.r_y, _.q_f, _.q_l }; }
bool operator> (const Type &_) const { return _ < *this; }
bool operator== (const Type &_) const { return !(*this < _ || _ < *this); }
};
vector<Type> type;
void get (ll r_x, ll r_y, int _1, int _2) {
int tmp = 0;
if (r_x == a) {
tmp = 1;
r_x -= a;
r_y -= b;
}
int pos = (int)(lower_bound(type.begin(), type.end(), Type(r_x, r_y, -infll, -infll) ) - type.begin() );
for (; pos < (int)type.size() && array<ll, 2>{ r_x, r_y } == array<ll, 2>{ type[pos].r_x, type[pos].r_y }; ++pos) {
vecAns.push_back(array<ll, 4>{ type[pos].q_f - tmp, _1, _2, 1 } );
vecAns.push_back(array<ll, 4>{ type[pos].q_l + 1 - tmp, _1, _2, -1 } );
}
}
void specCase () {
set< array<ll, 2> > set_;
for (int x = 0, y = 0, i = -1; i < n; ++i) {
if (i >= 0) x += dx[ dir[ s[i] - 'A' ] ], y += dy[ dir[ s[i] - 'A' ] ];
set_.insert(array<ll, 2>{ x, y } );
}
for (auto _ : set_) {
ans += (set_.find(array<ll, 2>{ _[0] + 1, _[1] + 1 } ) != set_.end() &&
set_.find(array<ll, 2>{ _[0], _[1] + 1 } ) != set_.end() &&
set_.find(array<ll, 2>{ _[0] + 1, _[1] } ) != set_.end() );
}
cout << ans;
}
int32_t main () {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> K >> s;
dir['E' - 'A'] = 2;
dir['N' - 'A'] = 1;
dir['W' - 'A'] = 0;
dir['S' - 'A'] = 3;
for (int i = 0; i < n; ++i) a += dx[ dir[ s[i] - 'A' ] ], b += dy[ dir[ s[i] - 'A' ] ];
if (a < 0) {
a = -a;
for (int i = 0; i < n; ++i) {
if (s[i] == 'E') s[i] = 'W';
else if (s[i] == 'W') s[i] = 'E';
}
}
if (b < 0) {
b = -b;
for (int i = 0; i < n; ++i) {
if (s[i] == 'N') s[i] = 'S';
else if (s[i] == 'S') s[i] = 'N';
}
}
if (!a && !b) {
specCase();
return 0;
}
if (!a) {
swap(a, b);
for (int i = 0; i < n; ++i) {
if (s[i] == 'E') s[i] = 'N';
else if (s[i] == 'N') s[i] = 'E';
else if (s[i] == 'W') s[i] = 'S';
else if (s[i] == 'S') s[i] = 'W';
}
}
for (int x = 0, y = 0, i = -1; i < n; ++i) {
if (i >= 0) x += dx[ dir[ s[i] - 'A' ] ], y += dy[ dir[ s[i] - 'A' ] ];
ll r_x, r_y, q_f, q_l;
r_x = x % a;
if (r_x < 0) r_x += a;
q_f = (-r_x + x) / a;
r_y = -(int)q_f * b + y;
q_l = q_f + K - 1;
type.push_back(Type(r_x, r_y, q_f, q_l) );
}
sort(type.begin(), type.end() ); type.erase(unique(type.begin(), type.end() ), type.end() );
for (int i = 0; i < (int)type.size(); ++i) {
vecAns.clear();
get(type[i].r_x, type[i].r_y, 0, 0);
get(type[i].r_x, type[i].r_y + 1, 0, 1);
get(type[i].r_x + 1, type[i].r_y, 1, 0);
get(type[i].r_x + 1, type[i].r_y + 1, 1, 1);
int cnt[2][2], Cnt = 0; memset(cnt, 0, sizeof cnt);
sort(vecAns.begin(), vecAns.end() );
ll lst = -infll;
for (auto _ : vecAns) {
if (!cnt[ _[1] ][ _[2] ] && _[3] == 1) {
if (Cnt == 3) lst = _[0];
++Cnt;
}
if (cnt[ _[1] ][ _[2] ] == 1 && _[3] == -1) {
if (Cnt == 4) ans += _[0] - lst;
--Cnt;
}
cnt[ _[1] ][ _[2] ] += (int)_[3];
}
for (int j = i + 1; j <= (int)type.size(); ++j) {
if (j == (int)type.size() ) {
i = j - 1;
break ;
}
if (array<ll, 2>{ type[i].r_x, type[i].r_y } != array<ll, 2>{ type[j].r_x, type[j].r_y } ) {
i = j - 1;
break ;
}
}
}
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
380 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
380 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
380 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
380 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
2 ms |
376 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
27 |
Correct |
4 ms |
764 KB |
Output is correct |
28 |
Correct |
5 ms |
660 KB |
Output is correct |
29 |
Correct |
22 ms |
2664 KB |
Output is correct |
30 |
Correct |
31 ms |
4776 KB |
Output is correct |
31 |
Correct |
87 ms |
4904 KB |
Output is correct |
32 |
Correct |
23 ms |
1784 KB |
Output is correct |
33 |
Correct |
46 ms |
4832 KB |
Output is correct |
34 |
Correct |
43 ms |
4904 KB |
Output is correct |
35 |
Correct |
45 ms |
4900 KB |
Output is correct |
36 |
Correct |
41 ms |
4904 KB |
Output is correct |
37 |
Correct |
24 ms |
2012 KB |
Output is correct |
38 |
Correct |
41 ms |
4908 KB |
Output is correct |
39 |
Correct |
43 ms |
4904 KB |
Output is correct |
40 |
Correct |
25 ms |
1912 KB |
Output is correct |
41 |
Correct |
45 ms |
4904 KB |
Output is correct |
42 |
Correct |
38 ms |
4904 KB |
Output is correct |
43 |
Correct |
44 ms |
4908 KB |
Output is correct |
44 |
Correct |
40 ms |
4904 KB |
Output is correct |
45 |
Correct |
42 ms |
4904 KB |
Output is correct |
46 |
Correct |
60 ms |
4904 KB |
Output is correct |
47 |
Correct |
73 ms |
4904 KB |
Output is correct |
48 |
Correct |
173 ms |
12120 KB |
Output is correct |
49 |
Correct |
88 ms |
4904 KB |
Output is correct |
50 |
Correct |
90 ms |
4904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
380 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
380 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
2 ms |
376 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
27 |
Correct |
2 ms |
376 KB |
Output is correct |
28 |
Correct |
2 ms |
376 KB |
Output is correct |
29 |
Correct |
2 ms |
376 KB |
Output is correct |
30 |
Correct |
2 ms |
380 KB |
Output is correct |
31 |
Correct |
2 ms |
376 KB |
Output is correct |
32 |
Correct |
2 ms |
376 KB |
Output is correct |
33 |
Correct |
2 ms |
380 KB |
Output is correct |
34 |
Correct |
2 ms |
376 KB |
Output is correct |
35 |
Correct |
2 ms |
376 KB |
Output is correct |
36 |
Correct |
2 ms |
376 KB |
Output is correct |
37 |
Correct |
2 ms |
376 KB |
Output is correct |
38 |
Correct |
2 ms |
380 KB |
Output is correct |
39 |
Correct |
2 ms |
376 KB |
Output is correct |
40 |
Correct |
2 ms |
376 KB |
Output is correct |
41 |
Correct |
2 ms |
376 KB |
Output is correct |
42 |
Correct |
2 ms |
376 KB |
Output is correct |
43 |
Correct |
2 ms |
376 KB |
Output is correct |
44 |
Correct |
2 ms |
376 KB |
Output is correct |
45 |
Correct |
2 ms |
376 KB |
Output is correct |
46 |
Correct |
2 ms |
376 KB |
Output is correct |
47 |
Correct |
2 ms |
380 KB |
Output is correct |
48 |
Correct |
2 ms |
376 KB |
Output is correct |
49 |
Correct |
2 ms |
376 KB |
Output is correct |
50 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
380 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
380 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
2 ms |
376 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
27 |
Correct |
4 ms |
764 KB |
Output is correct |
28 |
Correct |
5 ms |
660 KB |
Output is correct |
29 |
Correct |
22 ms |
2664 KB |
Output is correct |
30 |
Correct |
31 ms |
4776 KB |
Output is correct |
31 |
Correct |
87 ms |
4904 KB |
Output is correct |
32 |
Correct |
23 ms |
1784 KB |
Output is correct |
33 |
Correct |
46 ms |
4832 KB |
Output is correct |
34 |
Correct |
43 ms |
4904 KB |
Output is correct |
35 |
Correct |
45 ms |
4900 KB |
Output is correct |
36 |
Correct |
41 ms |
4904 KB |
Output is correct |
37 |
Correct |
24 ms |
2012 KB |
Output is correct |
38 |
Correct |
41 ms |
4908 KB |
Output is correct |
39 |
Correct |
43 ms |
4904 KB |
Output is correct |
40 |
Correct |
25 ms |
1912 KB |
Output is correct |
41 |
Correct |
45 ms |
4904 KB |
Output is correct |
42 |
Correct |
38 ms |
4904 KB |
Output is correct |
43 |
Correct |
44 ms |
4908 KB |
Output is correct |
44 |
Correct |
40 ms |
4904 KB |
Output is correct |
45 |
Correct |
42 ms |
4904 KB |
Output is correct |
46 |
Correct |
60 ms |
4904 KB |
Output is correct |
47 |
Correct |
73 ms |
4904 KB |
Output is correct |
48 |
Correct |
173 ms |
12120 KB |
Output is correct |
49 |
Correct |
88 ms |
4904 KB |
Output is correct |
50 |
Correct |
90 ms |
4904 KB |
Output is correct |
51 |
Correct |
2 ms |
376 KB |
Output is correct |
52 |
Correct |
2 ms |
376 KB |
Output is correct |
53 |
Correct |
2 ms |
376 KB |
Output is correct |
54 |
Correct |
2 ms |
376 KB |
Output is correct |
55 |
Correct |
2 ms |
380 KB |
Output is correct |
56 |
Correct |
2 ms |
376 KB |
Output is correct |
57 |
Correct |
2 ms |
376 KB |
Output is correct |
58 |
Correct |
2 ms |
380 KB |
Output is correct |
59 |
Correct |
2 ms |
376 KB |
Output is correct |
60 |
Correct |
2 ms |
376 KB |
Output is correct |
61 |
Correct |
2 ms |
376 KB |
Output is correct |
62 |
Correct |
2 ms |
376 KB |
Output is correct |
63 |
Correct |
2 ms |
380 KB |
Output is correct |
64 |
Correct |
2 ms |
376 KB |
Output is correct |
65 |
Correct |
2 ms |
376 KB |
Output is correct |
66 |
Correct |
2 ms |
376 KB |
Output is correct |
67 |
Correct |
2 ms |
376 KB |
Output is correct |
68 |
Correct |
2 ms |
376 KB |
Output is correct |
69 |
Correct |
2 ms |
376 KB |
Output is correct |
70 |
Correct |
2 ms |
376 KB |
Output is correct |
71 |
Correct |
2 ms |
376 KB |
Output is correct |
72 |
Correct |
2 ms |
380 KB |
Output is correct |
73 |
Correct |
2 ms |
376 KB |
Output is correct |
74 |
Correct |
2 ms |
376 KB |
Output is correct |
75 |
Correct |
2 ms |
376 KB |
Output is correct |
76 |
Correct |
5 ms |
760 KB |
Output is correct |
77 |
Correct |
4 ms |
504 KB |
Output is correct |
78 |
Correct |
18 ms |
1900 KB |
Output is correct |
79 |
Correct |
39 ms |
2668 KB |
Output is correct |
80 |
Correct |
34 ms |
4776 KB |
Output is correct |
81 |
Correct |
28 ms |
2168 KB |
Output is correct |
82 |
Correct |
41 ms |
4904 KB |
Output is correct |
83 |
Correct |
39 ms |
4904 KB |
Output is correct |
84 |
Correct |
41 ms |
4908 KB |
Output is correct |
85 |
Correct |
43 ms |
4904 KB |
Output is correct |
86 |
Correct |
88 ms |
9888 KB |
Output is correct |
87 |
Correct |
44 ms |
4908 KB |
Output is correct |
88 |
Correct |
44 ms |
4904 KB |
Output is correct |
89 |
Correct |
42 ms |
4904 KB |
Output is correct |
90 |
Correct |
25 ms |
2040 KB |
Output is correct |
91 |
Correct |
42 ms |
4904 KB |
Output is correct |
92 |
Correct |
42 ms |
4908 KB |
Output is correct |
93 |
Correct |
42 ms |
4908 KB |
Output is correct |
94 |
Correct |
38 ms |
4904 KB |
Output is correct |
95 |
Correct |
37 ms |
4904 KB |
Output is correct |
96 |
Correct |
56 ms |
4904 KB |
Output is correct |
97 |
Correct |
69 ms |
4904 KB |
Output is correct |
98 |
Correct |
79 ms |
12244 KB |
Output is correct |
99 |
Correct |
92 ms |
4904 KB |
Output is correct |
100 |
Correct |
101 ms |
4904 KB |
Output is correct |