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>
#include <string.h>
#define N 200000
#define N_ (1 << 18) /* N_ = pow2(ceil(log2(N + 1))) */
int max(int a, int b) { return a > b ? a : b; }
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
int *zz;
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)
if (zz[ii[j]] == zz[i_])
j++;
else if (zz[ii[j]] < zz[i_]) {
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 yy[N_ * 2], kk[N_ * 2], n_;
void build(int n) {
n_ = 1;
while (n_ <= n)
n_ <<= 1;
memset(yy, -1, n_ * 2 * sizeof *yy);
}
int count(int i, int y) {
int k = 0;
while (i < n_)
if (y >= yy[i << 1 | 1])
i = i << 1 | 0;
else
k += kk[i] - kk[i << 1 | 1], i = i << 1 | 1;
if (y < yy[i])
k++;
return k;
}
void pul(int i) {
int l = i << 1, r = l | 1;
yy[i] = max(yy[l], yy[r]);
kk[i] = kk[r] + count(l, yy[r]);
}
void update(int i, int y) {
i += n_;
yy[i] = y, kk[i] = 1;
while (i > 1)
pul(i >>= 1);
}
int query(int r) {
int l = 0, y = -1, k = 0;
for (l += n_, r += n_; l <= r; l >>= 1, r >>= 1)
if ((r & 1) == 0)
k += count(r, y), y = max(y, yy[r]), r--;
return k;
}
int main() {
static int xx[N], yy[N], ii[N];
int n, i, i_;
long long cnt;
scanf("%d", &n);
for (i = 0; i < n; i++)
scanf("%d%d", &xx[i], &yy[i]);
for (i = 0; i < n; i++)
ii[i] = i;
zz = xx, sort(ii, 0, n);
for (i = 0; i < n; i++)
xx[ii[i]] = i;
zz = yy, sort(ii, 0, n);
build(n);
cnt = 0;
for (i = 0; i < n; i++) {
i_ = ii[i];
cnt += query(xx[i_]);
update(xx[i_], yy[i_]);
}
printf("%lld\n", cnt);
return 0;
}
Compilation message (stderr)
scarecrows.c: In function 'main':
scarecrows.c:86:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
86 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
scarecrows.c:88:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
88 | scanf("%d%d", &xx[i], &yy[i]);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |