#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MX=100010, inf=2e9;
const int dx[4]={0,1,1,0};
const int dy[4]={0,0,1,1};
int n, k;
int X[MX], Y[MX];
pii disp;
void solve0(){
int ans=0;
map<pii, bool> B, C;
for(int i=0; i<=n; i++){
B[pii(X[i], Y[i])]=true;
}
for(int i=0; i<=n; i++){
int x=X[i], y=Y[i];
ans+=(!C[pii(x,y)] && B[pii(x+1,y)] && B[pii(x,y+1)] && B[pii(x+1,y+1)]);
// if(!C[pii(x,y)] && B[pii(x+1,y)] && B[pii(x,y+1)] && B[pii(x+1,y+1)])
// cout<<x<<' '<<y<<'\n';
C[pii(x,y)]=true;
}
cout<<ans;
exit(0);
}
void flip_h(){
for(int i=0; i<=n; i++)
X[i]=-X[i];
}
void flip_v(){
for(int i=0; i<=n; i++)
Y[i]=-Y[i];
}
void flip_x(){
for(int i=0; i<=n; i++)
swap(X[i], Y[i]);
}
void trans(){
int xmn=inf, ymn=inf;
for(int i=0; i<=n; i++)
xmn=min(xmn, X[i]), ymn=min(ymn, Y[i]);
for(int i=0; i<=n; i++)
X[i]-=xmn, Y[i]-=ymn;
}
void norm(int a, int b){
if(a==0 && b==0) solve0();
if(a<0) flip_h(), a=abs(a);
if(b<0) flip_v(), b=abs(b);
if(a==0) flip_x(), swap(a,b);
trans();
disp=pii(a, b);
for(int i=0; i<=n; i++)
X[i]+=a, Y[i]+=b;
}
void init(){
cin>>n>>k;
int x=0, y=0;
for(int i=1; i<=n; i++){
char c; cin>>c;
if(c=='S') y--;
if(c=='N') y++;
if(c=='W') x--;
if(c=='E') x++;
X[i]=x, Y[i]=y;
}
norm(x,y);
}
map<pii, vector<pii> > T;
void put(int x, int y, int l, int r){
auto &V=T[pii(x,y)];
V.push_back(pii(l,r));
}
void intst(map<int, int> &A, const vector<pii> &B){
for(const pii &p:B){
int l,r; tie(l,r)=p;
A[l]++, A[r+1]--;
}
}
void unite(vector<pii> &V){
sort(V.begin(), V.end());
vector<pii> W;
for(pii &p:V){
if(W.empty()) W.push_back(p);
else{
int l,r; tie(l,r)=p;
pii &q=W[W.size()-1U];
if(q.second<l-1) W.push_back(p);
else q=pii(q.first, max(q.second, r));
}
}
V=W;
}
void show(vector<pii> &V){
for(pii &p:V)
cout<<p.first<<" ~ "<<p.second<<", ";
cout<<'\n';
}
ll solve(){
ll ans=0;
int a, b; tie(a,b)=disp; // != 0
for(int i=0; i<=n; i++){
int q=X[i]/a, x=X[i]-q*a, y=Y[i]-q*b;
put(x, y, q, q+k-1);
if(x==0) put(x+a, y+b, q-1, q+k-2);
}
for(int i=0; i<=n; i++){
int q=X[i]/a, x=X[i]-q*a, y=Y[i]-q*b;
unite(T[pii(x,y)]);
// cout<<x<<' '<<y<<": ";
// show(T[pii(x,y)]);
if(x==0) unite(T[pii(x+a,y+b)]);
}
map<pii, bool> done;
for(int i=0; i<=n; i++){
int q=X[i]/a, x=X[i]-q*a, y=Y[i]-q*b;
if(done[pii(x,y)]) continue;
done[pii(x,y)]=true;
map<int, int> B;
for(int k=0; k<4; k++){
int u=x+dx[k], v=y+dy[k];
intst(B, T[pii(u,v)]);
}
int now=0, prv=-inf, cnt=0;
for(pii p:B){
int x, diff; tie(x,diff)=p;
if(cnt==4) now+=x-prv;
cnt+=diff;
prv=x;
}
// if(now!=0)
// cout<<X[i]<<' '<<Y[i]<<": "<<now<<'\n';
ans+=now;
}
return ans;
}
int main(){
ios::sync_with_stdio(0); cin.tie(0);
init();
// cout<<"\nDISP: "<<disp.first<<' '<<disp.second<<'\n';
cout<<solve()<<'\n';
// solve0();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
484 KB |
Output is correct |
3 |
Correct |
3 ms |
564 KB |
Output is correct |
4 |
Correct |
2 ms |
564 KB |
Output is correct |
5 |
Correct |
2 ms |
564 KB |
Output is correct |
6 |
Correct |
2 ms |
564 KB |
Output is correct |
7 |
Correct |
2 ms |
584 KB |
Output is correct |
8 |
Correct |
2 ms |
584 KB |
Output is correct |
9 |
Correct |
3 ms |
584 KB |
Output is correct |
10 |
Correct |
2 ms |
588 KB |
Output is correct |
11 |
Correct |
2 ms |
592 KB |
Output is correct |
12 |
Correct |
2 ms |
612 KB |
Output is correct |
13 |
Correct |
2 ms |
616 KB |
Output is correct |
14 |
Correct |
2 ms |
620 KB |
Output is correct |
15 |
Correct |
3 ms |
624 KB |
Output is correct |
16 |
Correct |
2 ms |
628 KB |
Output is correct |
17 |
Correct |
2 ms |
632 KB |
Output is correct |
18 |
Correct |
3 ms |
636 KB |
Output is correct |
19 |
Correct |
3 ms |
636 KB |
Output is correct |
20 |
Correct |
2 ms |
648 KB |
Output is correct |
21 |
Correct |
3 ms |
692 KB |
Output is correct |
22 |
Correct |
4 ms |
812 KB |
Output is correct |
23 |
Correct |
2 ms |
812 KB |
Output is correct |
24 |
Correct |
2 ms |
812 KB |
Output is correct |
25 |
Correct |
2 ms |
812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
484 KB |
Output is correct |
3 |
Correct |
3 ms |
564 KB |
Output is correct |
4 |
Correct |
2 ms |
564 KB |
Output is correct |
5 |
Correct |
2 ms |
564 KB |
Output is correct |
6 |
Correct |
2 ms |
564 KB |
Output is correct |
7 |
Correct |
2 ms |
584 KB |
Output is correct |
8 |
Correct |
2 ms |
584 KB |
Output is correct |
9 |
Correct |
3 ms |
584 KB |
Output is correct |
10 |
Correct |
2 ms |
588 KB |
Output is correct |
11 |
Correct |
2 ms |
592 KB |
Output is correct |
12 |
Correct |
2 ms |
612 KB |
Output is correct |
13 |
Correct |
2 ms |
616 KB |
Output is correct |
14 |
Correct |
2 ms |
620 KB |
Output is correct |
15 |
Correct |
3 ms |
624 KB |
Output is correct |
16 |
Correct |
2 ms |
628 KB |
Output is correct |
17 |
Correct |
2 ms |
632 KB |
Output is correct |
18 |
Correct |
3 ms |
636 KB |
Output is correct |
19 |
Correct |
3 ms |
636 KB |
Output is correct |
20 |
Correct |
2 ms |
648 KB |
Output is correct |
21 |
Correct |
3 ms |
692 KB |
Output is correct |
22 |
Correct |
4 ms |
812 KB |
Output is correct |
23 |
Correct |
2 ms |
812 KB |
Output is correct |
24 |
Correct |
2 ms |
812 KB |
Output is correct |
25 |
Correct |
2 ms |
812 KB |
Output is correct |
26 |
Correct |
2 ms |
812 KB |
Output is correct |
27 |
Correct |
6 ms |
1092 KB |
Output is correct |
28 |
Correct |
8 ms |
1360 KB |
Output is correct |
29 |
Correct |
38 ms |
4208 KB |
Output is correct |
30 |
Correct |
49 ms |
4260 KB |
Output is correct |
31 |
Correct |
187 ms |
23432 KB |
Output is correct |
32 |
Correct |
41 ms |
23432 KB |
Output is correct |
33 |
Correct |
79 ms |
23432 KB |
Output is correct |
34 |
Correct |
79 ms |
23432 KB |
Output is correct |
35 |
Correct |
72 ms |
23432 KB |
Output is correct |
36 |
Correct |
66 ms |
23432 KB |
Output is correct |
37 |
Correct |
41 ms |
23432 KB |
Output is correct |
38 |
Correct |
75 ms |
23432 KB |
Output is correct |
39 |
Correct |
72 ms |
23432 KB |
Output is correct |
40 |
Correct |
41 ms |
23432 KB |
Output is correct |
41 |
Correct |
56 ms |
23432 KB |
Output is correct |
42 |
Correct |
59 ms |
23432 KB |
Output is correct |
43 |
Correct |
80 ms |
23432 KB |
Output is correct |
44 |
Correct |
72 ms |
23432 KB |
Output is correct |
45 |
Correct |
60 ms |
23432 KB |
Output is correct |
46 |
Correct |
161 ms |
23432 KB |
Output is correct |
47 |
Correct |
227 ms |
23432 KB |
Output is correct |
48 |
Correct |
34 ms |
23432 KB |
Output is correct |
49 |
Correct |
214 ms |
28580 KB |
Output is correct |
50 |
Correct |
221 ms |
28720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
484 KB |
Output is correct |
3 |
Correct |
3 ms |
564 KB |
Output is correct |
4 |
Correct |
2 ms |
564 KB |
Output is correct |
5 |
Correct |
2 ms |
564 KB |
Output is correct |
6 |
Correct |
2 ms |
564 KB |
Output is correct |
7 |
Correct |
2 ms |
584 KB |
Output is correct |
8 |
Correct |
2 ms |
584 KB |
Output is correct |
9 |
Correct |
3 ms |
584 KB |
Output is correct |
10 |
Correct |
2 ms |
588 KB |
Output is correct |
11 |
Correct |
2 ms |
592 KB |
Output is correct |
12 |
Correct |
2 ms |
612 KB |
Output is correct |
13 |
Correct |
2 ms |
616 KB |
Output is correct |
14 |
Correct |
2 ms |
620 KB |
Output is correct |
15 |
Correct |
3 ms |
624 KB |
Output is correct |
16 |
Correct |
2 ms |
628 KB |
Output is correct |
17 |
Correct |
2 ms |
632 KB |
Output is correct |
18 |
Correct |
3 ms |
636 KB |
Output is correct |
19 |
Correct |
3 ms |
636 KB |
Output is correct |
20 |
Correct |
2 ms |
648 KB |
Output is correct |
21 |
Correct |
3 ms |
692 KB |
Output is correct |
22 |
Correct |
4 ms |
812 KB |
Output is correct |
23 |
Correct |
2 ms |
812 KB |
Output is correct |
24 |
Correct |
2 ms |
812 KB |
Output is correct |
25 |
Correct |
2 ms |
812 KB |
Output is correct |
26 |
Correct |
2 ms |
28720 KB |
Output is correct |
27 |
Correct |
2 ms |
28720 KB |
Output is correct |
28 |
Correct |
2 ms |
28720 KB |
Output is correct |
29 |
Correct |
2 ms |
28720 KB |
Output is correct |
30 |
Correct |
3 ms |
28720 KB |
Output is correct |
31 |
Correct |
2 ms |
28720 KB |
Output is correct |
32 |
Correct |
3 ms |
28720 KB |
Output is correct |
33 |
Correct |
2 ms |
28720 KB |
Output is correct |
34 |
Correct |
2 ms |
28720 KB |
Output is correct |
35 |
Correct |
2 ms |
28720 KB |
Output is correct |
36 |
Correct |
2 ms |
28720 KB |
Output is correct |
37 |
Correct |
2 ms |
28720 KB |
Output is correct |
38 |
Correct |
3 ms |
28720 KB |
Output is correct |
39 |
Correct |
3 ms |
28720 KB |
Output is correct |
40 |
Correct |
3 ms |
28720 KB |
Output is correct |
41 |
Correct |
2 ms |
28720 KB |
Output is correct |
42 |
Correct |
2 ms |
28720 KB |
Output is correct |
43 |
Correct |
2 ms |
28720 KB |
Output is correct |
44 |
Correct |
2 ms |
28720 KB |
Output is correct |
45 |
Correct |
2 ms |
28720 KB |
Output is correct |
46 |
Correct |
3 ms |
28720 KB |
Output is correct |
47 |
Correct |
2 ms |
28720 KB |
Output is correct |
48 |
Correct |
2 ms |
28720 KB |
Output is correct |
49 |
Correct |
2 ms |
28720 KB |
Output is correct |
50 |
Correct |
3 ms |
28720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
484 KB |
Output is correct |
3 |
Correct |
3 ms |
564 KB |
Output is correct |
4 |
Correct |
2 ms |
564 KB |
Output is correct |
5 |
Correct |
2 ms |
564 KB |
Output is correct |
6 |
Correct |
2 ms |
564 KB |
Output is correct |
7 |
Correct |
2 ms |
584 KB |
Output is correct |
8 |
Correct |
2 ms |
584 KB |
Output is correct |
9 |
Correct |
3 ms |
584 KB |
Output is correct |
10 |
Correct |
2 ms |
588 KB |
Output is correct |
11 |
Correct |
2 ms |
592 KB |
Output is correct |
12 |
Correct |
2 ms |
612 KB |
Output is correct |
13 |
Correct |
2 ms |
616 KB |
Output is correct |
14 |
Correct |
2 ms |
620 KB |
Output is correct |
15 |
Correct |
3 ms |
624 KB |
Output is correct |
16 |
Correct |
2 ms |
628 KB |
Output is correct |
17 |
Correct |
2 ms |
632 KB |
Output is correct |
18 |
Correct |
3 ms |
636 KB |
Output is correct |
19 |
Correct |
3 ms |
636 KB |
Output is correct |
20 |
Correct |
2 ms |
648 KB |
Output is correct |
21 |
Correct |
3 ms |
692 KB |
Output is correct |
22 |
Correct |
4 ms |
812 KB |
Output is correct |
23 |
Correct |
2 ms |
812 KB |
Output is correct |
24 |
Correct |
2 ms |
812 KB |
Output is correct |
25 |
Correct |
2 ms |
812 KB |
Output is correct |
26 |
Correct |
2 ms |
812 KB |
Output is correct |
27 |
Correct |
6 ms |
1092 KB |
Output is correct |
28 |
Correct |
8 ms |
1360 KB |
Output is correct |
29 |
Correct |
38 ms |
4208 KB |
Output is correct |
30 |
Correct |
49 ms |
4260 KB |
Output is correct |
31 |
Correct |
187 ms |
23432 KB |
Output is correct |
32 |
Correct |
41 ms |
23432 KB |
Output is correct |
33 |
Correct |
79 ms |
23432 KB |
Output is correct |
34 |
Correct |
79 ms |
23432 KB |
Output is correct |
35 |
Correct |
72 ms |
23432 KB |
Output is correct |
36 |
Correct |
66 ms |
23432 KB |
Output is correct |
37 |
Correct |
41 ms |
23432 KB |
Output is correct |
38 |
Correct |
75 ms |
23432 KB |
Output is correct |
39 |
Correct |
72 ms |
23432 KB |
Output is correct |
40 |
Correct |
41 ms |
23432 KB |
Output is correct |
41 |
Correct |
56 ms |
23432 KB |
Output is correct |
42 |
Correct |
59 ms |
23432 KB |
Output is correct |
43 |
Correct |
80 ms |
23432 KB |
Output is correct |
44 |
Correct |
72 ms |
23432 KB |
Output is correct |
45 |
Correct |
60 ms |
23432 KB |
Output is correct |
46 |
Correct |
161 ms |
23432 KB |
Output is correct |
47 |
Correct |
227 ms |
23432 KB |
Output is correct |
48 |
Correct |
34 ms |
23432 KB |
Output is correct |
49 |
Correct |
214 ms |
28580 KB |
Output is correct |
50 |
Correct |
221 ms |
28720 KB |
Output is correct |
51 |
Correct |
2 ms |
28720 KB |
Output is correct |
52 |
Correct |
2 ms |
28720 KB |
Output is correct |
53 |
Correct |
2 ms |
28720 KB |
Output is correct |
54 |
Correct |
2 ms |
28720 KB |
Output is correct |
55 |
Correct |
3 ms |
28720 KB |
Output is correct |
56 |
Correct |
2 ms |
28720 KB |
Output is correct |
57 |
Correct |
3 ms |
28720 KB |
Output is correct |
58 |
Correct |
2 ms |
28720 KB |
Output is correct |
59 |
Correct |
2 ms |
28720 KB |
Output is correct |
60 |
Correct |
2 ms |
28720 KB |
Output is correct |
61 |
Correct |
2 ms |
28720 KB |
Output is correct |
62 |
Correct |
2 ms |
28720 KB |
Output is correct |
63 |
Correct |
3 ms |
28720 KB |
Output is correct |
64 |
Correct |
3 ms |
28720 KB |
Output is correct |
65 |
Correct |
3 ms |
28720 KB |
Output is correct |
66 |
Correct |
2 ms |
28720 KB |
Output is correct |
67 |
Correct |
2 ms |
28720 KB |
Output is correct |
68 |
Correct |
2 ms |
28720 KB |
Output is correct |
69 |
Correct |
2 ms |
28720 KB |
Output is correct |
70 |
Correct |
2 ms |
28720 KB |
Output is correct |
71 |
Correct |
3 ms |
28720 KB |
Output is correct |
72 |
Correct |
2 ms |
28720 KB |
Output is correct |
73 |
Correct |
2 ms |
28720 KB |
Output is correct |
74 |
Correct |
2 ms |
28720 KB |
Output is correct |
75 |
Correct |
3 ms |
28720 KB |
Output is correct |
76 |
Correct |
6 ms |
28720 KB |
Output is correct |
77 |
Correct |
5 ms |
28720 KB |
Output is correct |
78 |
Correct |
26 ms |
28720 KB |
Output is correct |
79 |
Correct |
95 ms |
28720 KB |
Output is correct |
80 |
Correct |
53 ms |
28720 KB |
Output is correct |
81 |
Correct |
45 ms |
28720 KB |
Output is correct |
82 |
Correct |
68 ms |
28720 KB |
Output is correct |
83 |
Correct |
47 ms |
28720 KB |
Output is correct |
84 |
Correct |
75 ms |
28720 KB |
Output is correct |
85 |
Correct |
109 ms |
28720 KB |
Output is correct |
86 |
Correct |
134 ms |
28720 KB |
Output is correct |
87 |
Correct |
86 ms |
28720 KB |
Output is correct |
88 |
Correct |
67 ms |
28720 KB |
Output is correct |
89 |
Correct |
75 ms |
28720 KB |
Output is correct |
90 |
Correct |
42 ms |
28720 KB |
Output is correct |
91 |
Correct |
72 ms |
28720 KB |
Output is correct |
92 |
Correct |
79 ms |
28720 KB |
Output is correct |
93 |
Correct |
81 ms |
28720 KB |
Output is correct |
94 |
Correct |
75 ms |
28720 KB |
Output is correct |
95 |
Correct |
64 ms |
28720 KB |
Output is correct |
96 |
Correct |
153 ms |
28720 KB |
Output is correct |
97 |
Correct |
224 ms |
28720 KB |
Output is correct |
98 |
Correct |
29 ms |
28720 KB |
Output is correct |
99 |
Correct |
214 ms |
31108 KB |
Output is correct |
100 |
Correct |
237 ms |
31108 KB |
Output is correct |