#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pp;
typedef pair<ll,ll> pll;
void read(int& x){ scanf("%d",&x); }
void read(ll& x){ scanf("%lld",&x); }
void read(pp& x){ scanf("%d%d",&x.first, &x.second); }
void read(pll& x){ scanf("%lld%lld",&x.first, &x.second); }
template<typename T,typename... Args>
void read(T& a,Args&... b){ read(a); read(b...); }
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define eb emplace_back
#define x first
#define y second
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define sz(x) (int)(x).size()
char s[100010];
int n;
ll k;
pp p[100010];
int pn;
int dx, dy;
bool exist(int x, int y){
pp t{x, y};
int k=lower_bound(p, p+pn, t)-p;
return 0 <= k && k < pn && p[k] == t;
}
typedef tuple<ll,int,int> t3;
map<t3, int> group;
int gn;
ll gl[100010];
int gx[100010], gy[100010];
set<pp> gug[100010];
int Mod(int n, int r){
r = max(1, abs(r));
return (n%r+r)%r;
}
pp get_grp(ll x, ll y, bool make=true){
ll lv = dx*1ll*y - dy*1ll*x;
int mx = Mod(x, dx), my = Mod(y, dy);
auto tpl = make_tuple(lv, mx, my);
auto it = group.find(tpl);
if(it != group.end()){
return {it->second, dx ? (x-mx)/abs(dx) : (y-my)/abs(dy)};
}
if(!make) return {-1, -1};
group[tpl] = ++gn;
gl[gn] = lv; gx[gn] = x; gy[gn] = y;
return {gn, dx ? (x-mx)/abs(dx) : (y-my)/abs(dy)};
}
void gi(set<pp>& s, int l, int r){
auto it = s.insert(pp{l, r}).first;
if(it != s.begin()) --it;
auto mrg = [&](){
while(true){
auto it2 = it;
++it2;
if(it2 == s.end()) break;
if(it->y+1 < it2->x) break;
pp tmp{it->x, it2->y};
s.erase(it); s.erase(it2);
it = s.insert(tmp).first;
}
};
mrg();
++it;
if(it != s.end()) mrg();
}
ll Count(set<pp>& s){
ll ret = 0;
for(auto& tmp:s) ret += tmp.y - tmp.x + 1;
return ret;
}
int main()
{
read(n, k); scanf("%s", s+1);
rrep(i, n){
if(s[i]=='N') ++dy;
else if(s[i]=='E') ++dx;
else if(s[i]=='S') --dy;
else if(s[i]=='W') --dx;
p[i]={dx, dy};
}
sort(p, p+n+1);
pn = unique(p, p+n+1)-p;
if(dx==0 && dy==0){
int ans = 0;
rep(i, pn){
int x, y; tie(x, y) = p[i];
if(exist(x+1, y) && exist(x, y+1) && exist(x+1, y+1)) ++ans;
}
printf("%d\n", ans);
return 0;
}
rep(i, pn){
int x, y; tie(x, y) = p[i];
int grp, df; tie(grp, df) = get_grp(x, y);
gi(gug[grp], df, df+k-1);
}
ll ans = 0;
rrep(i, gn){
set<pp> G = gug[i];
int x=gx[i], y=gy[i];
int myir = get_grp(x, y).second;
auto add = [&](int qx, int qy){
int X = (x+qx), Y = (y+qy);
int tg, ir;
tie(tg, ir) = get_grp(X, Y, false);
ir -= myir;
if(tg == -1){
G.clear();
return;
}
set<pp> tmp;
auto &S1 = G, &S2 = gug[tg];
auto i1 = G.begin(), i2 = gug[tg].begin();
while(i1 != S1.end() && i2 != S2.end()){
int zl=max(i1->x, i2->x-ir);
int zr=min(i1->y, i2->y-ir);
if(zl <= zr) tmp.emplace(zl, zr);
if(i1->y < i2->y-ir) ++i1;
else ++i2;
}
swap(G, tmp);
};
add(1, 0);
add(0, 1);
add(1, 1);
ans += Count(G);
}
printf("%lld\n", ans);
return 0;
}
Compilation message
2016_ho_t4.cpp: In function 'void read(int&)':
2016_ho_t4.cpp:6:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
6 | void read(int& x){ scanf("%d",&x); }
| ~~~~~^~~~~~~~~
2016_ho_t4.cpp: In function 'void read(ll&)':
2016_ho_t4.cpp:7:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
7 | void read(ll& x){ scanf("%lld",&x); }
| ~~~~~^~~~~~~~~~~
2016_ho_t4.cpp: In function 'void read(pp&)':
2016_ho_t4.cpp:8:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
8 | void read(pp& x){ scanf("%d%d",&x.first, &x.second); }
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
2016_ho_t4.cpp: In function 'void read(pll&)':
2016_ho_t4.cpp:9:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
9 | void read(pll& x){ scanf("%lld%lld",&x.first, &x.second); }
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2016_ho_t4.cpp: In function 'int main()':
2016_ho_t4.cpp:91:19: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
91 | read(n, k); scanf("%s", s+1);
| ~~~~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
4 ms |
4992 KB |
Output is correct |
3 |
Correct |
5 ms |
4992 KB |
Output is correct |
4 |
Correct |
4 ms |
4992 KB |
Output is correct |
5 |
Correct |
5 ms |
4992 KB |
Output is correct |
6 |
Correct |
4 ms |
4992 KB |
Output is correct |
7 |
Correct |
4 ms |
4992 KB |
Output is correct |
8 |
Correct |
5 ms |
4992 KB |
Output is correct |
9 |
Correct |
4 ms |
4992 KB |
Output is correct |
10 |
Correct |
4 ms |
5008 KB |
Output is correct |
11 |
Correct |
4 ms |
5120 KB |
Output is correct |
12 |
Correct |
5 ms |
4992 KB |
Output is correct |
13 |
Correct |
3 ms |
4992 KB |
Output is correct |
14 |
Correct |
4 ms |
4992 KB |
Output is correct |
15 |
Correct |
3 ms |
4992 KB |
Output is correct |
16 |
Correct |
5 ms |
4992 KB |
Output is correct |
17 |
Correct |
4 ms |
5080 KB |
Output is correct |
18 |
Correct |
4 ms |
4992 KB |
Output is correct |
19 |
Correct |
4 ms |
4992 KB |
Output is correct |
20 |
Correct |
4 ms |
4992 KB |
Output is correct |
21 |
Correct |
4 ms |
4992 KB |
Output is correct |
22 |
Correct |
4 ms |
4992 KB |
Output is correct |
23 |
Correct |
4 ms |
4992 KB |
Output is correct |
24 |
Correct |
4 ms |
4992 KB |
Output is correct |
25 |
Correct |
4 ms |
4992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
4 ms |
4992 KB |
Output is correct |
3 |
Correct |
5 ms |
4992 KB |
Output is correct |
4 |
Correct |
4 ms |
4992 KB |
Output is correct |
5 |
Correct |
5 ms |
4992 KB |
Output is correct |
6 |
Correct |
4 ms |
4992 KB |
Output is correct |
7 |
Correct |
4 ms |
4992 KB |
Output is correct |
8 |
Correct |
5 ms |
4992 KB |
Output is correct |
9 |
Correct |
4 ms |
4992 KB |
Output is correct |
10 |
Correct |
4 ms |
5008 KB |
Output is correct |
11 |
Correct |
4 ms |
5120 KB |
Output is correct |
12 |
Correct |
5 ms |
4992 KB |
Output is correct |
13 |
Correct |
3 ms |
4992 KB |
Output is correct |
14 |
Correct |
4 ms |
4992 KB |
Output is correct |
15 |
Correct |
3 ms |
4992 KB |
Output is correct |
16 |
Correct |
5 ms |
4992 KB |
Output is correct |
17 |
Correct |
4 ms |
5080 KB |
Output is correct |
18 |
Correct |
4 ms |
4992 KB |
Output is correct |
19 |
Correct |
4 ms |
4992 KB |
Output is correct |
20 |
Correct |
4 ms |
4992 KB |
Output is correct |
21 |
Correct |
4 ms |
4992 KB |
Output is correct |
22 |
Correct |
4 ms |
4992 KB |
Output is correct |
23 |
Correct |
4 ms |
4992 KB |
Output is correct |
24 |
Correct |
4 ms |
4992 KB |
Output is correct |
25 |
Correct |
4 ms |
4992 KB |
Output is correct |
26 |
Correct |
5 ms |
5076 KB |
Output is correct |
27 |
Correct |
6 ms |
5248 KB |
Output is correct |
28 |
Correct |
6 ms |
5120 KB |
Output is correct |
29 |
Correct |
31 ms |
7296 KB |
Output is correct |
30 |
Correct |
26 ms |
7168 KB |
Output is correct |
31 |
Correct |
107 ms |
16504 KB |
Output is correct |
32 |
Correct |
18 ms |
6016 KB |
Output is correct |
33 |
Correct |
43 ms |
9088 KB |
Output is correct |
34 |
Correct |
54 ms |
9080 KB |
Output is correct |
35 |
Correct |
59 ms |
8864 KB |
Output is correct |
36 |
Correct |
30 ms |
7716 KB |
Output is correct |
37 |
Correct |
19 ms |
6016 KB |
Output is correct |
38 |
Correct |
44 ms |
8660 KB |
Output is correct |
39 |
Correct |
36 ms |
8824 KB |
Output is correct |
40 |
Correct |
17 ms |
6016 KB |
Output is correct |
41 |
Correct |
26 ms |
6784 KB |
Output is correct |
42 |
Correct |
28 ms |
7680 KB |
Output is correct |
43 |
Correct |
42 ms |
8796 KB |
Output is correct |
44 |
Correct |
42 ms |
8724 KB |
Output is correct |
45 |
Correct |
36 ms |
8448 KB |
Output is correct |
46 |
Correct |
72 ms |
10360 KB |
Output is correct |
47 |
Correct |
68 ms |
12280 KB |
Output is correct |
48 |
Correct |
38 ms |
6016 KB |
Output is correct |
49 |
Correct |
116 ms |
18444 KB |
Output is correct |
50 |
Correct |
109 ms |
18564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
4 ms |
4992 KB |
Output is correct |
3 |
Correct |
5 ms |
4992 KB |
Output is correct |
4 |
Correct |
4 ms |
4992 KB |
Output is correct |
5 |
Correct |
5 ms |
4992 KB |
Output is correct |
6 |
Correct |
4 ms |
4992 KB |
Output is correct |
7 |
Correct |
4 ms |
4992 KB |
Output is correct |
8 |
Correct |
5 ms |
4992 KB |
Output is correct |
9 |
Correct |
4 ms |
4992 KB |
Output is correct |
10 |
Correct |
4 ms |
5008 KB |
Output is correct |
11 |
Correct |
4 ms |
5120 KB |
Output is correct |
12 |
Correct |
5 ms |
4992 KB |
Output is correct |
13 |
Correct |
3 ms |
4992 KB |
Output is correct |
14 |
Correct |
4 ms |
4992 KB |
Output is correct |
15 |
Correct |
3 ms |
4992 KB |
Output is correct |
16 |
Correct |
5 ms |
4992 KB |
Output is correct |
17 |
Correct |
4 ms |
5080 KB |
Output is correct |
18 |
Correct |
4 ms |
4992 KB |
Output is correct |
19 |
Correct |
4 ms |
4992 KB |
Output is correct |
20 |
Correct |
4 ms |
4992 KB |
Output is correct |
21 |
Correct |
4 ms |
4992 KB |
Output is correct |
22 |
Correct |
4 ms |
4992 KB |
Output is correct |
23 |
Correct |
4 ms |
4992 KB |
Output is correct |
24 |
Correct |
4 ms |
4992 KB |
Output is correct |
25 |
Correct |
4 ms |
4992 KB |
Output is correct |
26 |
Correct |
5 ms |
4992 KB |
Output is correct |
27 |
Correct |
4 ms |
4992 KB |
Output is correct |
28 |
Correct |
4 ms |
4992 KB |
Output is correct |
29 |
Correct |
4 ms |
4992 KB |
Output is correct |
30 |
Correct |
4 ms |
4992 KB |
Output is correct |
31 |
Correct |
4 ms |
4992 KB |
Output is correct |
32 |
Correct |
4 ms |
4992 KB |
Output is correct |
33 |
Correct |
4 ms |
5120 KB |
Output is correct |
34 |
Correct |
5 ms |
4992 KB |
Output is correct |
35 |
Correct |
4 ms |
4992 KB |
Output is correct |
36 |
Correct |
4 ms |
4992 KB |
Output is correct |
37 |
Correct |
4 ms |
4992 KB |
Output is correct |
38 |
Correct |
3 ms |
4992 KB |
Output is correct |
39 |
Correct |
4 ms |
4992 KB |
Output is correct |
40 |
Correct |
5 ms |
4992 KB |
Output is correct |
41 |
Correct |
5 ms |
4992 KB |
Output is correct |
42 |
Correct |
4 ms |
4992 KB |
Output is correct |
43 |
Correct |
4 ms |
4992 KB |
Output is correct |
44 |
Correct |
5 ms |
5092 KB |
Output is correct |
45 |
Correct |
5 ms |
4992 KB |
Output is correct |
46 |
Correct |
3 ms |
4992 KB |
Output is correct |
47 |
Correct |
4 ms |
4992 KB |
Output is correct |
48 |
Correct |
4 ms |
4992 KB |
Output is correct |
49 |
Correct |
4 ms |
4992 KB |
Output is correct |
50 |
Correct |
4 ms |
4992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
4 ms |
4992 KB |
Output is correct |
3 |
Correct |
5 ms |
4992 KB |
Output is correct |
4 |
Correct |
4 ms |
4992 KB |
Output is correct |
5 |
Correct |
5 ms |
4992 KB |
Output is correct |
6 |
Correct |
4 ms |
4992 KB |
Output is correct |
7 |
Correct |
4 ms |
4992 KB |
Output is correct |
8 |
Correct |
5 ms |
4992 KB |
Output is correct |
9 |
Correct |
4 ms |
4992 KB |
Output is correct |
10 |
Correct |
4 ms |
5008 KB |
Output is correct |
11 |
Correct |
4 ms |
5120 KB |
Output is correct |
12 |
Correct |
5 ms |
4992 KB |
Output is correct |
13 |
Correct |
3 ms |
4992 KB |
Output is correct |
14 |
Correct |
4 ms |
4992 KB |
Output is correct |
15 |
Correct |
3 ms |
4992 KB |
Output is correct |
16 |
Correct |
5 ms |
4992 KB |
Output is correct |
17 |
Correct |
4 ms |
5080 KB |
Output is correct |
18 |
Correct |
4 ms |
4992 KB |
Output is correct |
19 |
Correct |
4 ms |
4992 KB |
Output is correct |
20 |
Correct |
4 ms |
4992 KB |
Output is correct |
21 |
Correct |
4 ms |
4992 KB |
Output is correct |
22 |
Correct |
4 ms |
4992 KB |
Output is correct |
23 |
Correct |
4 ms |
4992 KB |
Output is correct |
24 |
Correct |
4 ms |
4992 KB |
Output is correct |
25 |
Correct |
4 ms |
4992 KB |
Output is correct |
26 |
Correct |
5 ms |
5076 KB |
Output is correct |
27 |
Correct |
6 ms |
5248 KB |
Output is correct |
28 |
Correct |
6 ms |
5120 KB |
Output is correct |
29 |
Correct |
31 ms |
7296 KB |
Output is correct |
30 |
Correct |
26 ms |
7168 KB |
Output is correct |
31 |
Correct |
107 ms |
16504 KB |
Output is correct |
32 |
Correct |
18 ms |
6016 KB |
Output is correct |
33 |
Correct |
43 ms |
9088 KB |
Output is correct |
34 |
Correct |
54 ms |
9080 KB |
Output is correct |
35 |
Correct |
59 ms |
8864 KB |
Output is correct |
36 |
Correct |
30 ms |
7716 KB |
Output is correct |
37 |
Correct |
19 ms |
6016 KB |
Output is correct |
38 |
Correct |
44 ms |
8660 KB |
Output is correct |
39 |
Correct |
36 ms |
8824 KB |
Output is correct |
40 |
Correct |
17 ms |
6016 KB |
Output is correct |
41 |
Correct |
26 ms |
6784 KB |
Output is correct |
42 |
Correct |
28 ms |
7680 KB |
Output is correct |
43 |
Correct |
42 ms |
8796 KB |
Output is correct |
44 |
Correct |
42 ms |
8724 KB |
Output is correct |
45 |
Correct |
36 ms |
8448 KB |
Output is correct |
46 |
Correct |
72 ms |
10360 KB |
Output is correct |
47 |
Correct |
68 ms |
12280 KB |
Output is correct |
48 |
Correct |
38 ms |
6016 KB |
Output is correct |
49 |
Correct |
116 ms |
18444 KB |
Output is correct |
50 |
Correct |
109 ms |
18564 KB |
Output is correct |
51 |
Correct |
5 ms |
4992 KB |
Output is correct |
52 |
Correct |
4 ms |
4992 KB |
Output is correct |
53 |
Correct |
4 ms |
4992 KB |
Output is correct |
54 |
Correct |
4 ms |
4992 KB |
Output is correct |
55 |
Correct |
4 ms |
4992 KB |
Output is correct |
56 |
Correct |
4 ms |
4992 KB |
Output is correct |
57 |
Correct |
4 ms |
4992 KB |
Output is correct |
58 |
Correct |
4 ms |
5120 KB |
Output is correct |
59 |
Correct |
5 ms |
4992 KB |
Output is correct |
60 |
Correct |
4 ms |
4992 KB |
Output is correct |
61 |
Correct |
4 ms |
4992 KB |
Output is correct |
62 |
Correct |
4 ms |
4992 KB |
Output is correct |
63 |
Correct |
3 ms |
4992 KB |
Output is correct |
64 |
Correct |
4 ms |
4992 KB |
Output is correct |
65 |
Correct |
5 ms |
4992 KB |
Output is correct |
66 |
Correct |
5 ms |
4992 KB |
Output is correct |
67 |
Correct |
4 ms |
4992 KB |
Output is correct |
68 |
Correct |
4 ms |
4992 KB |
Output is correct |
69 |
Correct |
5 ms |
5092 KB |
Output is correct |
70 |
Correct |
5 ms |
4992 KB |
Output is correct |
71 |
Correct |
3 ms |
4992 KB |
Output is correct |
72 |
Correct |
4 ms |
4992 KB |
Output is correct |
73 |
Correct |
4 ms |
4992 KB |
Output is correct |
74 |
Correct |
4 ms |
4992 KB |
Output is correct |
75 |
Correct |
4 ms |
4992 KB |
Output is correct |
76 |
Correct |
7 ms |
5152 KB |
Output is correct |
77 |
Correct |
6 ms |
5180 KB |
Output is correct |
78 |
Correct |
21 ms |
6524 KB |
Output is correct |
79 |
Correct |
62 ms |
11532 KB |
Output is correct |
80 |
Correct |
28 ms |
7208 KB |
Output is correct |
81 |
Correct |
20 ms |
6016 KB |
Output is correct |
82 |
Correct |
36 ms |
8576 KB |
Output is correct |
83 |
Correct |
24 ms |
6720 KB |
Output is correct |
84 |
Correct |
56 ms |
8784 KB |
Output is correct |
85 |
Correct |
51 ms |
8824 KB |
Output is correct |
86 |
Correct |
73 ms |
9080 KB |
Output is correct |
87 |
Correct |
51 ms |
9480 KB |
Output is correct |
88 |
Correct |
40 ms |
8320 KB |
Output is correct |
89 |
Correct |
39 ms |
8440 KB |
Output is correct |
90 |
Correct |
20 ms |
6016 KB |
Output is correct |
91 |
Correct |
47 ms |
9080 KB |
Output is correct |
92 |
Correct |
43 ms |
9080 KB |
Output is correct |
93 |
Correct |
46 ms |
8960 KB |
Output is correct |
94 |
Correct |
34 ms |
7948 KB |
Output is correct |
95 |
Correct |
31 ms |
7936 KB |
Output is correct |
96 |
Correct |
48 ms |
9088 KB |
Output is correct |
97 |
Correct |
77 ms |
12308 KB |
Output is correct |
98 |
Correct |
33 ms |
6016 KB |
Output is correct |
99 |
Correct |
117 ms |
18556 KB |
Output is correct |
100 |
Correct |
125 ms |
18552 KB |
Output is correct |