#include <bits/stdc++.h>
using namespace std;
const int MN = 5e5 + 5;
struct Stupid {
map<long long,long long> row,row2;
set<long long> dub,dub2,intersect;
void add (long long i, long long v) {
if (!row.count(i)) {
row[i] += v;
//assert(row[i] >= 0);
if (row.count(i-1)) {
dub.insert(i-1);
if (dub2.count(i-1)) intersect.insert(i-1);
}
if (row.count(i+1)) {
dub.insert(i);
if (dub2.count(i)) intersect.insert(i);
}
return;
}
if (!(row[i] += v)) {
row.erase(i);
if (row.count(i-1)) {
dub.erase(i-1);
if (dub2.count(i-1)) intersect.erase(i-1);
}
if (row.count(i+1)) {
dub.erase(i);
if (dub2.count(i)) intersect.erase(i);
}
}
}
void add2 (long long i, long long v) {
if (!row2.count(i)) {
row2[i] += v;
if (row2.count(i-1)) {
dub2.insert(i-1);
if (dub.count(i-1)) intersect.insert(i-1);
}
if (row2.count(i+1)) {
dub2.insert(i);
if (dub.count(i)) intersect.insert(i);
}
return;
}
if (!(row2[i] += v)) {
row2.erase(i);
if (row2.count(i-1)) {
dub2.erase(i-1);
if (dub.count(i-1)) intersect.erase(i-1);
}
if (row2.count(i+1)) {
dub2.erase(i);
if (dub.count(i)) intersect.erase(i);
}
}
}
};
char s[MN];
int main() {
int n; long long k;
scanf ("%d %lld\n%s",&n,&k,s+1);
int dx = 0, dy = 0;
for (int i = 1; i <= n; i++) {
if (s[i] == 'E') dx++;
else if (s[i] == 'W') dx--;
else if (s[i] == 'S') dy++;
else dy--;
}
if (dx < 0) {
for (int i = 1; i <= n; i++) {
if (s[i] == 'E') s[i] = 'W';
else if (s[i] == 'W') s[i] = 'E';
}
dx = -dx;
}
if (dy < 0) {
for (int i = 1; i <= n; i++) {
if (s[i] == 'S') s[i] = 'N';
else if (s[i] == 'N') s[i] = 'S';
}
dy = -dy;
}
if (dx == 0 && dy == 0) { //TODO special case stupid
set<pair<int,int>> st;
dx = 0, dy = 0; st.emplace(0,0);
for (int i = 1; i <= n; i++) {
if (s[i] == 'E') dx++;
else if (s[i] == 'W') dx--;
else if (s[i] == 'S') dy++;
else dy--;
st.emplace(dx,dy);
}
int res = 0;
for (auto [x,y] : st) {
if (st.count({x+1,y}) && st.count({x,y+1}) && st.count({x+1,y+1})) {
res++;
}
}
printf ("%d\n",res);
return 0;
}
if (dy == 0) {
for (int i = 1; i <= n; i++) {
if (s[i] == 'E') s[i] = 'S';
else if (s[i] == 'W') s[i] = 'N';
else if (s[i] == 'S') s[i] = 'E';
else s[i] = 'W';
}
swap(dx,dy);
}
//we have dy > 0, dx >= 0
int top = 0, cur = 0;
for (int i = 1; i <= n; i++) {
if (s[i] == 'N') top = min(top,--cur);
else if (s[i] == 'S') ++cur;
}
vector<map<long long,int>> monke(MN);
vector<Stupid> row(MN);
int curx = 0, cury = 0, mxy = -top;
monke[-top][0]++;
for (int i = 1; i <= n; i++) {
if (s[i] == 'E') ++curx;
else if (s[i] == 'W') --curx;
else if (s[i] == 'N') --cury;
else ++cury;
mxy = max(mxy,cury - top);
monke[cury - top][curx]++;
}
long long ret = 0, L = mxy + 2, R = k * dy - 1;
vector<long long> lz(MN); vector<int> lst(MN);
for (int i = 0; i < L; i++) {
lst[i%dy] = i;
if (i >= dy) {
lz[i] = lz[i-dy] + dx; swap(row[i],row[i-dy]);
for (auto [j,cnt] : monke[i]) row[i].add(j-lz[i],cnt);
for (auto [j,cnt] : monke[i-1]) row[i].add2(j-lz[i],cnt);
} else {
for (auto [j,cnt] : monke[i])
row[i].add(j,cnt);
if (i) for (auto [j,cnt] : monke[i-1])
row[i].add2(j,cnt);
}
if (i >= k * dy) {
for (auto [j,cnt] : monke[i-dy*k]) {
row[i].add(j + k * dx - lz[i],-cnt);
}
if (i - 1 >= k * dy) {
for (auto [j,cnt] : monke[i - dy * k - 1]) {
row[i].add2(j + k * dx - lz[i],-cnt);
}
}
}
if (!row[i].row.empty()) {
ret += (int)row[i].intersect.size();
}
}
//printf ("ret %lld\n",ret);
for (int res = 0; res < dy; res++) {
long long cnt = max(0LL,(R - res) / dy - (L - 1 - res) / dy);
//printf ("res %d cnt %lld\n",res,cnt);
ret += (long long)row[lst[res]].intersect.size() * cnt; lz[lst[res]] += dx * cnt;
}
//printf ("ret %lld\n",ret);
for (long long i = max(L,R+1); i <= R + 2 + mxy; i++) {
int idx = lst[i % dy]; lz[idx] += dx;
//printf ("i %lld idx %d\n",i,idx);
for (auto [j,cnt] : monke[i - dy * k]) {
row[idx].add(j + k * dx - lz[idx],-cnt);
}
if (i >= dy * k + 1) for (auto [j,cnt] : monke[i - dy * k - 1]) {
row[idx].add2(j + k * dx - lz[idx], -cnt);
}
if (!row[idx].row.empty()) {
ret += (int)row[idx].intersect.size();
}
}
printf ("%lld\n",ret);
return 0;
}
Compilation message
2016_ho_t4.cpp: In function 'int main()':
2016_ho_t4.cpp:62:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
62 | scanf ("%d %lld\n%s",&n,&k,s+1);
| ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
104 ms |
147180 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
105 ms |
147052 KB |
Output is correct |
4 |
Correct |
103 ms |
147072 KB |
Output is correct |
5 |
Correct |
104 ms |
147052 KB |
Output is correct |
6 |
Correct |
105 ms |
147052 KB |
Output is correct |
7 |
Correct |
104 ms |
147180 KB |
Output is correct |
8 |
Correct |
104 ms |
147052 KB |
Output is correct |
9 |
Correct |
102 ms |
147180 KB |
Output is correct |
10 |
Correct |
105 ms |
147052 KB |
Output is correct |
11 |
Correct |
103 ms |
147128 KB |
Output is correct |
12 |
Correct |
104 ms |
147052 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
104 ms |
147052 KB |
Output is correct |
15 |
Correct |
104 ms |
147052 KB |
Output is correct |
16 |
Correct |
104 ms |
147052 KB |
Output is correct |
17 |
Correct |
103 ms |
147052 KB |
Output is correct |
18 |
Correct |
103 ms |
147052 KB |
Output is correct |
19 |
Correct |
104 ms |
147052 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
103 ms |
147052 KB |
Output is correct |
22 |
Correct |
104 ms |
147052 KB |
Output is correct |
23 |
Correct |
104 ms |
147052 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
104 ms |
147052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
104 ms |
147180 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
105 ms |
147052 KB |
Output is correct |
4 |
Correct |
103 ms |
147072 KB |
Output is correct |
5 |
Correct |
104 ms |
147052 KB |
Output is correct |
6 |
Correct |
105 ms |
147052 KB |
Output is correct |
7 |
Correct |
104 ms |
147180 KB |
Output is correct |
8 |
Correct |
104 ms |
147052 KB |
Output is correct |
9 |
Correct |
102 ms |
147180 KB |
Output is correct |
10 |
Correct |
105 ms |
147052 KB |
Output is correct |
11 |
Correct |
103 ms |
147128 KB |
Output is correct |
12 |
Correct |
104 ms |
147052 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
104 ms |
147052 KB |
Output is correct |
15 |
Correct |
104 ms |
147052 KB |
Output is correct |
16 |
Correct |
104 ms |
147052 KB |
Output is correct |
17 |
Correct |
103 ms |
147052 KB |
Output is correct |
18 |
Correct |
103 ms |
147052 KB |
Output is correct |
19 |
Correct |
104 ms |
147052 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
103 ms |
147052 KB |
Output is correct |
22 |
Correct |
104 ms |
147052 KB |
Output is correct |
23 |
Correct |
104 ms |
147052 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
104 ms |
147052 KB |
Output is correct |
26 |
Correct |
104 ms |
147180 KB |
Output is correct |
27 |
Correct |
107 ms |
147308 KB |
Output is correct |
28 |
Correct |
3 ms |
492 KB |
Output is correct |
29 |
Correct |
132 ms |
149996 KB |
Output is correct |
30 |
Correct |
132 ms |
149740 KB |
Output is correct |
31 |
Correct |
198 ms |
157548 KB |
Output is correct |
32 |
Correct |
18 ms |
1516 KB |
Output is correct |
33 |
Correct |
144 ms |
154604 KB |
Output is correct |
34 |
Correct |
153 ms |
151400 KB |
Output is correct |
35 |
Correct |
150 ms |
154092 KB |
Output is correct |
36 |
Correct |
142 ms |
151020 KB |
Output is correct |
37 |
Correct |
20 ms |
1644 KB |
Output is correct |
38 |
Correct |
148 ms |
151020 KB |
Output is correct |
39 |
Correct |
143 ms |
153964 KB |
Output is correct |
40 |
Correct |
20 ms |
1516 KB |
Output is correct |
41 |
Correct |
137 ms |
149296 KB |
Output is correct |
42 |
Correct |
135 ms |
151276 KB |
Output is correct |
43 |
Correct |
155 ms |
152556 KB |
Output is correct |
44 |
Correct |
144 ms |
151660 KB |
Output is correct |
45 |
Correct |
142 ms |
152940 KB |
Output is correct |
46 |
Correct |
181 ms |
157036 KB |
Output is correct |
47 |
Correct |
249 ms |
163692 KB |
Output is correct |
48 |
Correct |
122 ms |
153580 KB |
Output is correct |
49 |
Correct |
139 ms |
166252 KB |
Output is correct |
50 |
Correct |
138 ms |
166124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
104 ms |
147180 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
105 ms |
147052 KB |
Output is correct |
4 |
Correct |
103 ms |
147072 KB |
Output is correct |
5 |
Correct |
104 ms |
147052 KB |
Output is correct |
6 |
Correct |
105 ms |
147052 KB |
Output is correct |
7 |
Correct |
104 ms |
147180 KB |
Output is correct |
8 |
Correct |
104 ms |
147052 KB |
Output is correct |
9 |
Correct |
102 ms |
147180 KB |
Output is correct |
10 |
Correct |
105 ms |
147052 KB |
Output is correct |
11 |
Correct |
103 ms |
147128 KB |
Output is correct |
12 |
Correct |
104 ms |
147052 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
104 ms |
147052 KB |
Output is correct |
15 |
Correct |
104 ms |
147052 KB |
Output is correct |
16 |
Correct |
104 ms |
147052 KB |
Output is correct |
17 |
Correct |
103 ms |
147052 KB |
Output is correct |
18 |
Correct |
103 ms |
147052 KB |
Output is correct |
19 |
Correct |
104 ms |
147052 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
103 ms |
147052 KB |
Output is correct |
22 |
Correct |
104 ms |
147052 KB |
Output is correct |
23 |
Correct |
104 ms |
147052 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
104 ms |
147052 KB |
Output is correct |
26 |
Correct |
101 ms |
147052 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
103 ms |
147052 KB |
Output is correct |
29 |
Correct |
101 ms |
147052 KB |
Output is correct |
30 |
Correct |
103 ms |
147052 KB |
Output is correct |
31 |
Correct |
102 ms |
147052 KB |
Output is correct |
32 |
Correct |
101 ms |
147052 KB |
Output is correct |
33 |
Correct |
102 ms |
147052 KB |
Output is correct |
34 |
Correct |
108 ms |
147180 KB |
Output is correct |
35 |
Correct |
102 ms |
147052 KB |
Output is correct |
36 |
Correct |
101 ms |
147052 KB |
Output is correct |
37 |
Correct |
102 ms |
147052 KB |
Output is correct |
38 |
Correct |
100 ms |
147052 KB |
Output is correct |
39 |
Correct |
100 ms |
147052 KB |
Output is correct |
40 |
Correct |
1 ms |
364 KB |
Output is correct |
41 |
Correct |
102 ms |
147052 KB |
Output is correct |
42 |
Correct |
1 ms |
364 KB |
Output is correct |
43 |
Correct |
100 ms |
147052 KB |
Output is correct |
44 |
Correct |
101 ms |
147052 KB |
Output is correct |
45 |
Correct |
101 ms |
147052 KB |
Output is correct |
46 |
Correct |
102 ms |
147052 KB |
Output is correct |
47 |
Correct |
103 ms |
147180 KB |
Output is correct |
48 |
Correct |
102 ms |
147052 KB |
Output is correct |
49 |
Correct |
102 ms |
147052 KB |
Output is correct |
50 |
Correct |
100 ms |
147052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
104 ms |
147180 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
105 ms |
147052 KB |
Output is correct |
4 |
Correct |
103 ms |
147072 KB |
Output is correct |
5 |
Correct |
104 ms |
147052 KB |
Output is correct |
6 |
Correct |
105 ms |
147052 KB |
Output is correct |
7 |
Correct |
104 ms |
147180 KB |
Output is correct |
8 |
Correct |
104 ms |
147052 KB |
Output is correct |
9 |
Correct |
102 ms |
147180 KB |
Output is correct |
10 |
Correct |
105 ms |
147052 KB |
Output is correct |
11 |
Correct |
103 ms |
147128 KB |
Output is correct |
12 |
Correct |
104 ms |
147052 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
104 ms |
147052 KB |
Output is correct |
15 |
Correct |
104 ms |
147052 KB |
Output is correct |
16 |
Correct |
104 ms |
147052 KB |
Output is correct |
17 |
Correct |
103 ms |
147052 KB |
Output is correct |
18 |
Correct |
103 ms |
147052 KB |
Output is correct |
19 |
Correct |
104 ms |
147052 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
103 ms |
147052 KB |
Output is correct |
22 |
Correct |
104 ms |
147052 KB |
Output is correct |
23 |
Correct |
104 ms |
147052 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
104 ms |
147052 KB |
Output is correct |
26 |
Correct |
104 ms |
147180 KB |
Output is correct |
27 |
Correct |
107 ms |
147308 KB |
Output is correct |
28 |
Correct |
3 ms |
492 KB |
Output is correct |
29 |
Correct |
132 ms |
149996 KB |
Output is correct |
30 |
Correct |
132 ms |
149740 KB |
Output is correct |
31 |
Correct |
198 ms |
157548 KB |
Output is correct |
32 |
Correct |
18 ms |
1516 KB |
Output is correct |
33 |
Correct |
144 ms |
154604 KB |
Output is correct |
34 |
Correct |
153 ms |
151400 KB |
Output is correct |
35 |
Correct |
150 ms |
154092 KB |
Output is correct |
36 |
Correct |
142 ms |
151020 KB |
Output is correct |
37 |
Correct |
20 ms |
1644 KB |
Output is correct |
38 |
Correct |
148 ms |
151020 KB |
Output is correct |
39 |
Correct |
143 ms |
153964 KB |
Output is correct |
40 |
Correct |
20 ms |
1516 KB |
Output is correct |
41 |
Correct |
137 ms |
149296 KB |
Output is correct |
42 |
Correct |
135 ms |
151276 KB |
Output is correct |
43 |
Correct |
155 ms |
152556 KB |
Output is correct |
44 |
Correct |
144 ms |
151660 KB |
Output is correct |
45 |
Correct |
142 ms |
152940 KB |
Output is correct |
46 |
Correct |
181 ms |
157036 KB |
Output is correct |
47 |
Correct |
249 ms |
163692 KB |
Output is correct |
48 |
Correct |
122 ms |
153580 KB |
Output is correct |
49 |
Correct |
139 ms |
166252 KB |
Output is correct |
50 |
Correct |
138 ms |
166124 KB |
Output is correct |
51 |
Correct |
101 ms |
147052 KB |
Output is correct |
52 |
Correct |
1 ms |
384 KB |
Output is correct |
53 |
Correct |
103 ms |
147052 KB |
Output is correct |
54 |
Correct |
101 ms |
147052 KB |
Output is correct |
55 |
Correct |
103 ms |
147052 KB |
Output is correct |
56 |
Correct |
102 ms |
147052 KB |
Output is correct |
57 |
Correct |
101 ms |
147052 KB |
Output is correct |
58 |
Correct |
102 ms |
147052 KB |
Output is correct |
59 |
Correct |
108 ms |
147180 KB |
Output is correct |
60 |
Correct |
102 ms |
147052 KB |
Output is correct |
61 |
Correct |
101 ms |
147052 KB |
Output is correct |
62 |
Correct |
102 ms |
147052 KB |
Output is correct |
63 |
Correct |
100 ms |
147052 KB |
Output is correct |
64 |
Correct |
100 ms |
147052 KB |
Output is correct |
65 |
Correct |
1 ms |
364 KB |
Output is correct |
66 |
Correct |
102 ms |
147052 KB |
Output is correct |
67 |
Correct |
1 ms |
364 KB |
Output is correct |
68 |
Correct |
100 ms |
147052 KB |
Output is correct |
69 |
Correct |
101 ms |
147052 KB |
Output is correct |
70 |
Correct |
101 ms |
147052 KB |
Output is correct |
71 |
Correct |
102 ms |
147052 KB |
Output is correct |
72 |
Correct |
103 ms |
147180 KB |
Output is correct |
73 |
Correct |
102 ms |
147052 KB |
Output is correct |
74 |
Correct |
102 ms |
147052 KB |
Output is correct |
75 |
Correct |
100 ms |
147052 KB |
Output is correct |
76 |
Correct |
102 ms |
147436 KB |
Output is correct |
77 |
Correct |
2 ms |
492 KB |
Output is correct |
78 |
Correct |
125 ms |
151100 KB |
Output is correct |
79 |
Correct |
166 ms |
158444 KB |
Output is correct |
80 |
Correct |
138 ms |
151276 KB |
Output is correct |
81 |
Correct |
20 ms |
1644 KB |
Output is correct |
82 |
Correct |
146 ms |
153580 KB |
Output is correct |
83 |
Correct |
123 ms |
149740 KB |
Output is correct |
84 |
Correct |
145 ms |
154148 KB |
Output is correct |
85 |
Correct |
157 ms |
154220 KB |
Output is correct |
86 |
Correct |
225 ms |
160364 KB |
Output is correct |
87 |
Correct |
150 ms |
155500 KB |
Output is correct |
88 |
Correct |
139 ms |
153256 KB |
Output is correct |
89 |
Correct |
152 ms |
153580 KB |
Output is correct |
90 |
Correct |
19 ms |
1644 KB |
Output is correct |
91 |
Correct |
145 ms |
154604 KB |
Output is correct |
92 |
Correct |
145 ms |
154860 KB |
Output is correct |
93 |
Correct |
150 ms |
154476 KB |
Output is correct |
94 |
Correct |
133 ms |
152192 KB |
Output is correct |
95 |
Correct |
140 ms |
152428 KB |
Output is correct |
96 |
Correct |
191 ms |
157164 KB |
Output is correct |
97 |
Correct |
247 ms |
163820 KB |
Output is correct |
98 |
Correct |
124 ms |
153716 KB |
Output is correct |
99 |
Correct |
138 ms |
166124 KB |
Output is correct |
100 |
Correct |
382 ms |
180204 KB |
Output is correct |