#include <stdio.h>
#define N 100000
#define N_ ((N + 1) * 8)
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
long long cross(int x1, int y1, int x2, int y2) {
return (long long) x1 * y2 - (long long) x2 * y1;
}
long long dot(int x1, int y1, int x2, int y2) {
return (long long) x1 * x2 + (long long) y1 * y2;
}
long long xxp[N + 1], xxq[N + 1], yy[N + 1], xxp_[N_], xxq_[N_], yy_[N_], d_; int vv[N_], x_, y_;
long long *xxp1, *xxq1, *yy1;
int compare(int i, int j) {
if (yy1[i] != yy1[j])
return yy1[i] < yy1[j] ? -1 : 1;
if (xxq1[i] != xxq1[j])
return xxq1[i] < xxq1[j] ? -1 : 1;
return xxp1[i] == xxp1[j] ? 0 : (xxp1[i] < xxp1[j] ? -1 : 1);
}
void sort(int *ii, int l, int r) {
while (l < r) {
int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;
while (j < k) {
int c = compare(ii[j], i_);
if (c == 0)
j++;
else if (c < 0) {
tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
i++, j++;
} else {
k--;
tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
}
}
sort(ii, l, i);
l = k;
}
}
int main() {
static char cc[N + 1];
static int ii[N + 1], ii_[N_];
int n, n_, k, h, h_, i, j, x, y, v;
long long d, ans;
scanf("%d%d%s", &n, &k, cc);
x_ = 0, y_ = 0;
for (i = 0; i < n; i++)
if (cc[i] == 'E')
x_++;
else if (cc[i] == 'W')
x_--;
else if (cc[i] == 'N')
y_++;
else
y_--;
if (x_ == 0 && y_ == 0)
x_ = 1, y_ = 0, k = 1;
d_ = dot(x_, y_, x_, y_);
x = 0, y = 0;
for (i = 0; i < n; i++) {
if (cc[i] == 'E')
x++;
else if (cc[i] == 'W')
x--;
else if (cc[i] == 'N')
y++;
else
y--;
d = dot(x, y, x_, y_);
xxq[i + 1] = d % d_;
if (xxq[i + 1] < 0)
xxq[i + 1] += d_;
xxp[i + 1] = (d - xxq[i + 1]) / d_;
yy[i + 1] = cross(x, y, x_, y_);
}
for (i = 0; i <= n; i++)
ii[i] = i;
xxp1 = xxp, xxq1 = xxq, yy1 = yy, sort(ii, 0, n + 1);
n_ = 0;
for (i = 0; i <= n; i = j) {
long long xq = xxq[ii[i]], y = yy[ii[i]];
j = i + 1;
while (j <= n && xxq[ii[j]] == xq && yy[ii[j]] == y)
j++;
for (h = i; h < j; h = h_) {
long long xp1, xq1, y1, xp2, xq2, y2;
h_ = h + 1;
while (h_ < j && xxp[ii[h_]] - xxp[ii[h_ - 1]] <= k)
h_++;
xp1 = xxp[ii[h]], xq1 = xq, y1 = y;
xp2 = xxp[ii[h_ - 1]] + k, xq2 = xq, y2 = y;
xxp_[n_] = xp1, xxq_[n_] = xq1, yy_[n_] = y1, vv[n_] = 1, n_++;
xxp_[n_] = xp2, xxq_[n_] = xq2, yy_[n_] = y2, vv[n_] = -1, n_++;
xq1 += y_;
if (xq1 < 0)
xp1--, xq1 += d_;
if (xq1 >= d_)
xp1++, xq1 -= d_;
y1 -= x_;
xq2 += y_;
if (xq2 < 0)
xp2--, xq2 += d_;
if (xq2 >= d_)
xp2++, xq2 -= d_;
y2 -= x_;
xxp_[n_] = xp1, xxq_[n_] = xq1, yy_[n_] = y1, vv[n_] = 1, n_++;
xxp_[n_] = xp2, xxq_[n_] = xq2, yy_[n_] = y2, vv[n_] = -1, n_++;
xq1 += x_;
if (xq1 < 0)
xp1--, xq1 += d_;
if (xq1 >= d_)
xp1++, xq1 -= d_;
y1 += y_;
xq2 += x_;
if (xq2 < 0)
xp2--, xq2 += d_;
if (xq2 >= d_)
xp2++, xq2 -= d_;
y2 += y_;
xxp_[n_] = xp1, xxq_[n_] = xq1, yy_[n_] = y1, vv[n_] = 1, n_++;
xxp_[n_] = xp2, xxq_[n_] = xq2, yy_[n_] = y2, vv[n_] = -1, n_++;
xq1 -= y_;
if (xq1 < 0)
xp1--, xq1 += d_;
if (xq1 >= d_)
xp1++, xq1 -= d_;
y1 += x_;
xq2 -= y_;
if (xq2 < 0)
xp2--, xq2 += d_;
if (xq2 >= d_)
xp2++, xq2 -= d_;
y2 += x_;
xxp_[n_] = xp1, xxq_[n_] = xq1, yy_[n_] = y1, vv[n_] = 1, n_++;
xxp_[n_] = xp2, xxq_[n_] = xq2, yy_[n_] = y2, vv[n_] = -1, n_++;
}
}
for (i = 0; i < n_; i++)
ii_[i] = i;
xxp1 = xxp_, xxq1 = xxq_, yy1 = yy_, sort(ii_, 0, n_);
ans = 0;
for (i = 0; i < n_; i = j) {
long long xq = xxq_[ii_[i]], y = yy_[ii_[i]];
j = i + 1;
while (j < n_ && xxq_[ii_[j]] == xq && yy_[ii_[j]] == y)
j++;
v = 0;
for (h = i; h + 1 < j; h++)
if ((v += vv[ii_[h]]) == 4)
ans += xxp_[ii_[h + 1]] - xxp_[ii_[h]];
}
printf("%lld\n", ans);
return 0;
}
Compilation message
2016_ho_t4.c: In function 'main':
2016_ho_t4.c:60:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
60 | scanf("%d%d%s", &n, &k, cc);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
288 KB |
Output is correct |
3 |
Correct |
1 ms |
292 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
292 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
292 KB |
Output is correct |
8 |
Correct |
0 ms |
288 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
292 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
288 KB |
Output is correct |
19 |
Correct |
0 ms |
288 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
292 KB |
Output is correct |
24 |
Correct |
0 ms |
292 KB |
Output is correct |
25 |
Correct |
0 ms |
292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
288 KB |
Output is correct |
3 |
Correct |
1 ms |
292 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
292 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
292 KB |
Output is correct |
8 |
Correct |
0 ms |
288 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
292 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
288 KB |
Output is correct |
19 |
Correct |
0 ms |
288 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
292 KB |
Output is correct |
24 |
Correct |
0 ms |
292 KB |
Output is correct |
25 |
Correct |
0 ms |
292 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
2 ms |
724 KB |
Output is correct |
28 |
Correct |
3 ms |
852 KB |
Output is correct |
29 |
Correct |
19 ms |
5284 KB |
Output is correct |
30 |
Correct |
17 ms |
5340 KB |
Output is correct |
31 |
Correct |
73 ms |
24120 KB |
Output is correct |
32 |
Correct |
14 ms |
3756 KB |
Output is correct |
33 |
Correct |
30 ms |
9388 KB |
Output is correct |
34 |
Correct |
30 ms |
9428 KB |
Output is correct |
35 |
Correct |
29 ms |
8888 KB |
Output is correct |
36 |
Correct |
24 ms |
6928 KB |
Output is correct |
37 |
Correct |
15 ms |
3912 KB |
Output is correct |
38 |
Correct |
27 ms |
8488 KB |
Output is correct |
39 |
Correct |
31 ms |
8900 KB |
Output is correct |
40 |
Correct |
14 ms |
3796 KB |
Output is correct |
41 |
Correct |
22 ms |
5684 KB |
Output is correct |
42 |
Correct |
22 ms |
6608 KB |
Output is correct |
43 |
Correct |
31 ms |
8756 KB |
Output is correct |
44 |
Correct |
34 ms |
8760 KB |
Output is correct |
45 |
Correct |
26 ms |
7840 KB |
Output is correct |
46 |
Correct |
46 ms |
15736 KB |
Output is correct |
47 |
Correct |
40 ms |
15752 KB |
Output is correct |
48 |
Correct |
11 ms |
3156 KB |
Output is correct |
49 |
Correct |
104 ms |
28192 KB |
Output is correct |
50 |
Correct |
106 ms |
28272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
288 KB |
Output is correct |
3 |
Correct |
1 ms |
292 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
292 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
292 KB |
Output is correct |
8 |
Correct |
0 ms |
288 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
292 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
288 KB |
Output is correct |
19 |
Correct |
0 ms |
288 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
292 KB |
Output is correct |
24 |
Correct |
0 ms |
292 KB |
Output is correct |
25 |
Correct |
0 ms |
292 KB |
Output is correct |
26 |
Correct |
1 ms |
352 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
0 ms |
352 KB |
Output is correct |
30 |
Correct |
0 ms |
304 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
304 KB |
Output is correct |
33 |
Correct |
0 ms |
340 KB |
Output is correct |
34 |
Correct |
0 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
288 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
292 KB |
Output is correct |
39 |
Correct |
0 ms |
296 KB |
Output is correct |
40 |
Correct |
0 ms |
340 KB |
Output is correct |
41 |
Correct |
0 ms |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
0 ms |
292 KB |
Output is correct |
44 |
Correct |
0 ms |
340 KB |
Output is correct |
45 |
Correct |
0 ms |
340 KB |
Output is correct |
46 |
Correct |
0 ms |
288 KB |
Output is correct |
47 |
Correct |
0 ms |
300 KB |
Output is correct |
48 |
Correct |
1 ms |
300 KB |
Output is correct |
49 |
Correct |
0 ms |
352 KB |
Output is correct |
50 |
Correct |
0 ms |
352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
288 KB |
Output is correct |
3 |
Correct |
1 ms |
292 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
292 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
292 KB |
Output is correct |
8 |
Correct |
0 ms |
288 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
292 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
288 KB |
Output is correct |
19 |
Correct |
0 ms |
288 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
292 KB |
Output is correct |
24 |
Correct |
0 ms |
292 KB |
Output is correct |
25 |
Correct |
0 ms |
292 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
2 ms |
724 KB |
Output is correct |
28 |
Correct |
3 ms |
852 KB |
Output is correct |
29 |
Correct |
19 ms |
5284 KB |
Output is correct |
30 |
Correct |
17 ms |
5340 KB |
Output is correct |
31 |
Correct |
73 ms |
24120 KB |
Output is correct |
32 |
Correct |
14 ms |
3756 KB |
Output is correct |
33 |
Correct |
30 ms |
9388 KB |
Output is correct |
34 |
Correct |
30 ms |
9428 KB |
Output is correct |
35 |
Correct |
29 ms |
8888 KB |
Output is correct |
36 |
Correct |
24 ms |
6928 KB |
Output is correct |
37 |
Correct |
15 ms |
3912 KB |
Output is correct |
38 |
Correct |
27 ms |
8488 KB |
Output is correct |
39 |
Correct |
31 ms |
8900 KB |
Output is correct |
40 |
Correct |
14 ms |
3796 KB |
Output is correct |
41 |
Correct |
22 ms |
5684 KB |
Output is correct |
42 |
Correct |
22 ms |
6608 KB |
Output is correct |
43 |
Correct |
31 ms |
8756 KB |
Output is correct |
44 |
Correct |
34 ms |
8760 KB |
Output is correct |
45 |
Correct |
26 ms |
7840 KB |
Output is correct |
46 |
Correct |
46 ms |
15736 KB |
Output is correct |
47 |
Correct |
40 ms |
15752 KB |
Output is correct |
48 |
Correct |
11 ms |
3156 KB |
Output is correct |
49 |
Correct |
104 ms |
28192 KB |
Output is correct |
50 |
Correct |
106 ms |
28272 KB |
Output is correct |
51 |
Correct |
1 ms |
352 KB |
Output is correct |
52 |
Correct |
0 ms |
348 KB |
Output is correct |
53 |
Correct |
1 ms |
340 KB |
Output is correct |
54 |
Correct |
0 ms |
352 KB |
Output is correct |
55 |
Correct |
0 ms |
304 KB |
Output is correct |
56 |
Correct |
0 ms |
348 KB |
Output is correct |
57 |
Correct |
0 ms |
304 KB |
Output is correct |
58 |
Correct |
0 ms |
340 KB |
Output is correct |
59 |
Correct |
0 ms |
340 KB |
Output is correct |
60 |
Correct |
1 ms |
288 KB |
Output is correct |
61 |
Correct |
1 ms |
340 KB |
Output is correct |
62 |
Correct |
1 ms |
340 KB |
Output is correct |
63 |
Correct |
1 ms |
292 KB |
Output is correct |
64 |
Correct |
0 ms |
296 KB |
Output is correct |
65 |
Correct |
0 ms |
340 KB |
Output is correct |
66 |
Correct |
0 ms |
340 KB |
Output is correct |
67 |
Correct |
1 ms |
340 KB |
Output is correct |
68 |
Correct |
0 ms |
292 KB |
Output is correct |
69 |
Correct |
0 ms |
340 KB |
Output is correct |
70 |
Correct |
0 ms |
340 KB |
Output is correct |
71 |
Correct |
0 ms |
288 KB |
Output is correct |
72 |
Correct |
0 ms |
300 KB |
Output is correct |
73 |
Correct |
1 ms |
300 KB |
Output is correct |
74 |
Correct |
0 ms |
352 KB |
Output is correct |
75 |
Correct |
0 ms |
352 KB |
Output is correct |
76 |
Correct |
2 ms |
736 KB |
Output is correct |
77 |
Correct |
1 ms |
608 KB |
Output is correct |
78 |
Correct |
10 ms |
3424 KB |
Output is correct |
79 |
Correct |
41 ms |
13744 KB |
Output is correct |
80 |
Correct |
17 ms |
5308 KB |
Output is correct |
81 |
Correct |
16 ms |
4172 KB |
Output is correct |
82 |
Correct |
36 ms |
8364 KB |
Output is correct |
83 |
Correct |
21 ms |
4648 KB |
Output is correct |
84 |
Correct |
35 ms |
8768 KB |
Output is correct |
85 |
Correct |
27 ms |
8788 KB |
Output is correct |
86 |
Correct |
29 ms |
9508 KB |
Output is correct |
87 |
Correct |
34 ms |
10088 KB |
Output is correct |
88 |
Correct |
25 ms |
7852 KB |
Output is correct |
89 |
Correct |
26 ms |
8108 KB |
Output is correct |
90 |
Correct |
15 ms |
4064 KB |
Output is correct |
91 |
Correct |
29 ms |
9312 KB |
Output is correct |
92 |
Correct |
33 ms |
9432 KB |
Output is correct |
93 |
Correct |
30 ms |
9056 KB |
Output is correct |
94 |
Correct |
25 ms |
6964 KB |
Output is correct |
95 |
Correct |
23 ms |
7036 KB |
Output is correct |
96 |
Correct |
28 ms |
9524 KB |
Output is correct |
97 |
Correct |
46 ms |
15768 KB |
Output is correct |
98 |
Correct |
11 ms |
3168 KB |
Output is correct |
99 |
Correct |
93 ms |
28248 KB |
Output is correct |
100 |
Correct |
107 ms |
28280 KB |
Output is correct |