#include <stdio.h>
#define N 1000000
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
long long gcd(long long a, long long b) {
return b == 0 ? a : gcd(b, a % b);
}
long long xx[N * 2], yy[N * 4];
int compare_xy(int i, int j) {
return xx[i] != xx[j] ? (xx[i] < xx[j] ? -1 : 1) : (yy[i] == yy[j] ? 0 : (yy[i] < yy[j] ? -1 : 1));
}
int compare_x(int i, int j) {
return xx[i] == xx[j] ? 0 : (xx[i] < xx[j] ? -1 : 1);
}
int compare_y(int i, int j) {
return yy[i] == yy[j] ? 0 : (yy[i] < yy[j] ? -1 : 1);
}
int (*compare)(int, int);
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 int ll[N], rr[N], ii[N * 4], kk[N * 2];
int n, n_, i, small, x, cnt;
long long a, b, c, ans;
scanf("%d%lld%lld", &n, &a, &b), c = a / gcd(a, b + 1);
if (n == 1) {
long long l, r;
scanf("%lld%lld", &l, &r);
printf("%lld\n", c <= (r - l + 1) / b ? c * b : r - l + 1);
return 0;
}
small = 1;
for (i = 0; i < n; i++) {
scanf("%d%d", &ll[i], &rr[i]);
if (rr[i] - ll[i] + 1 > b)
small = 0;
}
if (!small) {
n_ = 0;
for (i = 0; i < n; i++)
while (ll[i] <= rr[i]) {
xx[n_] = (ll[i] + ll[i] / b) % a, yy[n_] = ll[i] % b, n_++;
ll[i]++;
}
n = n_;
for (i = 0; i < n; i++)
ii[i] = i;
compare = compare_xy, sort(ii, 0, n);
ans = 1;
for (i = 1; i < n; i++)
if (xx[ii[i]] != xx[ii[i - 1]] || yy[ii[i]] != yy[ii[i - 1]])
ans++;
} else {
n_ = 0;
for (i = 0; i < n; i++)
if (ll[i] / b == rr[i] / b)
xx[n_] = ll[i] / b * (b + 1) % a, yy[n_ << 1 | 0] = ll[i] % b, yy[n_ << 1 | 1] = rr[i] % b + 1, n_++;
else {
xx[n_] = ll[i] / b * (b + 1) % a, yy[n_ << 1 | 0] = ll[i] % b, yy[n_ << 1 | 1] = b, n_++;
xx[n_] = rr[i] / b * (b + 1) % a, yy[n_ << 1 | 0] = 0, yy[n_ << 1 | 1] = rr[i] % b + 1, n_++;
}
for (i = 0; i < n_; i++)
ii[i] = i;
compare = compare_x, sort(ii, 0, n_);
for (i = 0, x = 0; i < n_; i++)
xx[ii[i]] = i + 1 == n_ || xx[ii[i + 1]] != xx[ii[i]] ? x++ : x;
for (i = 0; i < n_ * 2; i++)
ii[i] = i;
compare = compare_y, sort(ii, 0, n_ * 2);
ans = 0, cnt = 0;
for (i = 0; i + 1 < n_ * 2; i++) {
int i_ = ii[i], x = xx[i_ >> 1];
if ((i_ & 1) == 0) {
if (kk[x]++ == 0)
cnt++;
} else {
if (--kk[x] == 0)
cnt--;
}
ans += (yy[ii[i + 1]] - yy[ii[i]]) * cnt;
}
}
printf("%lld\n", ans);
return 0;
}
Compilation message
strange_device.c: In function 'main':
strange_device.c:58:2: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
58 | scanf("%d%lld%lld", &n, &a, &b), c = a / gcd(a, b + 1);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
strange_device.c:62:3: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
62 | scanf("%lld%lld", &l, &r);
| ^~~~~~~~~~~~~~~~~~~~~~~~~
strange_device.c:68:3: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
68 | scanf("%d%d", &ll[i], &rr[i]);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Incorrect |
7 ms |
748 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Incorrect |
155 ms |
13036 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Incorrect |
582 ms |
27856 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Incorrect |
582 ms |
27856 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Incorrect |
582 ms |
27856 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Incorrect |
81 ms |
4332 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Incorrect |
7 ms |
748 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |