This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |