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 M 100000
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
long long cross2(int x1, int y1, int x2, int y2) {
return (long long) x1 * y2 - (long long) x2 * y1;
}
long long cross(int x0, int y0, int x1, int y1, int x2, int y2) {
return cross2(x1 - x0, y1 - y0, x2 - x0, y2 - y0);
}
int xx[N + M], yy[N + M], xx_[N * 2], yy_[N * 2], idx[N * 2];
int compare(int x1, int y1, int x2, int y2) {
int sgn1 = x1 < 0 || x1 == 0 && y1 < 0 ? -1 : 1;
int sgn2 = x2 < 0 || x2 == 0 && y2 < 0 ? -1 : 1;
long long c;
if (sgn1 != sgn2)
return sgn1 - sgn2;
c = cross2(x1, y1, x2, y2);
return c == 0 ? 0 : (c > 0 ? -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(xx_[ii[j]], yy_[ii[j]], xx_[i_], yy_[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 intersect(int l1, int r1, int l2, int r2) {
int l1r1 = l1 <= r1, r1l2 = r1 < l2, l2r2 = l2 <= r2, r2l1 = r2 < l1;
return !(l1r1 && r1l2 && l2r2 || r1l2 && l2r2 && r2l1 || l2r2 && r2l1 && l1r1 || r2l1 && l1r1 && r1l2);
}
int main() {
static int ii[N * 2], dd[N * 2 + 1];
int n, n_, m, i, j, p, q, k;
scanf("%d", &n);
for (i = 0; i < n; i++)
scanf("%d%d", &xx[i], &yy[i]);
scanf("%d", &m);
for (j = 0; j < m; j++)
scanf("%d%d", &xx[n + j], &yy[n + j]);
for (i = 0; i < n; i++)
for (j = 0; j < m; j++) {
p = (j - 1 + m) % m, q = (j + 1) % m;
if (cross(xx[i], yy[i], xx[n + p], yy[n + p], xx[n + j], yy[n + j]) >= 0
&& cross(xx[i], yy[i], xx[n + q], yy[n + q], xx[n + j], yy[n + j]) > 0)
xx_[i << 1 | 0] = xx[i] - xx[n + j], yy_[i << 1 | 0] = yy[i] - yy[n + j];
if (cross(xx[i], yy[i], xx[n + q], yy[n + q], xx[n + j], yy[n + j]) <= 0
&& cross(xx[i], yy[i], xx[n + p], yy[n + p], xx[n + j], yy[n + j]) < 0)
xx_[i << 1 | 1] = xx[n + j] - xx[i], yy_[i << 1 | 1] = yy[n + j] - yy[i];
}
for (i = 0; i < n * 2; i++)
ii[i] = i;
sort(ii, 0, n * 2);
for (i = 0, n_ = 0; i < n * 2; i++)
idx[ii[i]] = i + 1 == n * 2 || compare(xx_[ii[i + 1]], yy_[ii[i + 1]], xx_[ii[i]], yy_[ii[i]]) != 0 ? n_++ : n_;
for (i = 1; i < n; i++) {
int l = idx[i << 1 | 0], r = idx[i << 1 | 1];
if (intersect(idx[0 << 1 | 0], idx[0 << 1 | 1], l, r)) {
if (l < r)
dd[l]++, dd[r + 1]--;
else
dd[l]++, dd[n_]--, dd[0]++, dd[r + 1]--;
}
}
for (i = 1; i < n_; i++)
dd[i] += dd[i - 1];
for (i = 0; i < n_; i++)
if (dd[i] > 0)
dd[i] = 1;
for (i = 1; i < n_; i++)
dd[i] += dd[i - 1];
k = 0;
for (i = 1; i < n; i++) {
int l = idx[i << 1 | 0], r = idx[i << 1 | 1];
if ((l < r ? dd[r] - (l == 0 ? 0 : dd[l - 1]) : dd[n_ - 1] - dd[l - 1] + dd[r]) > 0)
k++;
}
printf("%d\n", k);
return 0;
}
Compilation message (stderr)
relay.c: In function 'compare':
relay.c:23:31: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
23 | int sgn1 = x1 < 0 || x1 == 0 && y1 < 0 ? -1 : 1;
| ~~~~~~~~^~~~~~~~~
relay.c:24:31: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
24 | int sgn2 = x2 < 0 || x2 == 0 && y2 < 0 ? -1 : 1;
| ~~~~~~~~^~~~~~~~~
relay.c: In function 'intersect':
relay.c:58:24: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
58 | return !(l1r1 && r1l2 && l2r2 || r1l2 && l2r2 && r2l1 || l2r2 && r2l1 && l1r1 || r2l1 && l1r1 && r1l2);
| ~~~~~~~~~~~~~^~~~~~~
relay.c:58:72: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
58 | return !(l1r1 && r1l2 && l2r2 || r1l2 && l2r2 && r2l1 || l2r2 && r2l1 && l1r1 || r2l1 && l1r1 && r1l2);
| ~~~~~~~~~~~~~^~~~~~~
relay.c:58:96: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
58 | return !(l1r1 && r1l2 && l2r2 || r1l2 && l2r2 && r2l1 || l2r2 && r2l1 && l1r1 || r2l1 && l1r1 && r1l2);
| ~~~~~~~~~~~~~^~~~~~~
relay.c: In function 'main':
relay.c:65:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
65 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
relay.c:67:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
67 | scanf("%d%d", &xx[i], &yy[i]);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
relay.c:68:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
68 | scanf("%d", &m);
| ^~~~~~~~~~~~~~~
relay.c:70:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
70 | scanf("%d%d", &xx[n + j], &yy[n + j]);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |