#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], ll_[N * 2], rr_[N * 2];
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_l(int i, int j) {
return ll_[i] == ll_[j] ? 0 : (ll_[i] < ll_[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 long long ll[N], rr[N];
static int ii_[N * 4], ii1[N * 2], kk[N * 2];
int n, n_, n1, i, j, x, cnt;
long long a, b, l, r, ans;
scanf("%d%lld%lld", &n, &a, &b), a /= gcd(a, b + 1);
for (i = 0; i < n; i++)
scanf("%lld%lld", &ll[i], &rr[i]);
n_ = 0, n1 = 0;
for (i = 0; i < n; i++) {
l = ll[i] / b, r = rr[i] / b;
if (l == r)
xx[n_] = l % a, yy[n_ << 1 | 0] = ll[i] % b, yy[n_ << 1 | 1] = rr[i] % b + 1, n_++;
else {
xx[n_] = l % a, yy[n_ << 1 | 0] = ll[i] % b, yy[n_ << 1 | 1] = b, n_++;
xx[n_] = r % a, yy[n_ << 1 | 0] = 0, yy[n_ << 1 | 1] = rr[i] % b + 1, n_++;
if (r - l > 1) {
l++, r--;
if (r - l + 1 >= a) {
printf("%lld\n", a * b);
return 0;
}
l %= a, r %= a;
if (l <= r)
ll_[n1] = l, rr_[n1] = r, n1++;
else {
ll_[n1] = l, rr_[n1] = a - 1, n1++;
ll_[n1] = 0, rr_[n1] = r, n1++;
}
}
}
}
for (i = 0; i < n_; i++)
ii_[i] = i;
compare = compare_x, sort(ii_, 0, n_);
for (i = 0; i < n1; i++)
ii1[i] = i;
compare = compare_l, sort(ii1, 0, n1);
ans = 0;
for (i = 0, j = 0, r = -1, x = 0; i <= n_; i++) {
while (j < n1 && (i == n_ || ll_[ii1[j]] <= xx[ii_[i]])) {
int i1 = ii1[j++];
if (r < ll_[i1])
ans += rr_[i1] - ll_[i1] + 1, r = rr_[i1];
else if (r < rr_[i1])
ans += rr_[i1] - r, r = rr_[i1];
}
if (i < n_)
xx[ii_[i]] = r >= xx[ii_[i]] ? -1 : (i + 1 == n_ || xx[ii_[i + 1]] != xx[ii_[i]] ? x++ : x);
}
ans *= b;
for (i = 0; i < n_ * 2; i++)
ii_[i] = i;
compare = compare_y, sort(ii_, 0, n_ * 2);
cnt = 0;
for (i = 0; i + 1 < n_ * 2; i++) {
int i_ = ii_[i];
x = xx[i_ >> 1];
if ((i_ & 1) == 0) {
if (x != -1 && kk[x]++ == 0)
cnt++;
} else {
if (x != -1 && --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:59:2: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
59 | scanf("%d%lld%lld", &n, &a, &b), a /= gcd(a, b + 1);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
strange_device.c:61:3: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
61 | scanf("%lld%lld", &ll[i], &rr[i]);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
8 ms |
1132 KB |
Output is correct |
3 |
Correct |
8 ms |
1408 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
2 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
8 ms |
1260 KB |
Output is correct |
17 |
Correct |
68 ms |
8044 KB |
Output is correct |
18 |
Correct |
2 ms |
492 KB |
Output is correct |
# |
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 |
Correct |
2 ms |
512 KB |
Output is correct |
3 |
Correct |
2 ms |
492 KB |
Output is correct |
4 |
Correct |
2 ms |
492 KB |
Output is correct |
5 |
Correct |
460 ms |
99820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
626 ms |
53476 KB |
Output is correct |
3 |
Correct |
1014 ms |
106024 KB |
Output is correct |
4 |
Correct |
987 ms |
106392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
626 ms |
53476 KB |
Output is correct |
3 |
Correct |
1014 ms |
106024 KB |
Output is correct |
4 |
Correct |
987 ms |
106392 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
986 ms |
102396 KB |
Output is correct |
7 |
Correct |
590 ms |
48876 KB |
Output is correct |
8 |
Correct |
1036 ms |
106728 KB |
Output is correct |
9 |
Correct |
968 ms |
105836 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
626 ms |
53476 KB |
Output is correct |
3 |
Correct |
1014 ms |
106024 KB |
Output is correct |
4 |
Correct |
987 ms |
106392 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
80 ms |
11628 KB |
Output is correct |
7 |
Correct |
98 ms |
13676 KB |
Output is correct |
8 |
Correct |
96 ms |
12780 KB |
Output is correct |
9 |
Correct |
93 ms |
12780 KB |
Output is correct |
10 |
Correct |
86 ms |
10732 KB |
Output is correct |
11 |
Correct |
98 ms |
12780 KB |
Output is correct |
12 |
Correct |
97 ms |
13164 KB |
Output is correct |
13 |
Correct |
96 ms |
12780 KB |
Output is correct |
14 |
Correct |
75 ms |
8556 KB |
Output is correct |
15 |
Correct |
107 ms |
12396 KB |
Output is correct |
16 |
Correct |
90 ms |
7404 KB |
Output is correct |
17 |
Correct |
139 ms |
14316 KB |
Output is correct |
18 |
Correct |
1397 ms |
106468 KB |
Output is correct |
19 |
Correct |
1456 ms |
105980 KB |
Output is correct |
20 |
Correct |
1468 ms |
106148 KB |
Output is correct |
21 |
Correct |
109 ms |
14316 KB |
Output is correct |
22 |
Correct |
106 ms |
14316 KB |
Output is correct |
23 |
Correct |
194 ms |
37228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
80 ms |
6124 KB |
Output is correct |
3 |
Correct |
92 ms |
8172 KB |
Output is correct |
4 |
Correct |
1524 ms |
64836 KB |
Output is correct |
5 |
Correct |
97 ms |
7788 KB |
Output is correct |
6 |
Correct |
111 ms |
7788 KB |
Output is correct |
7 |
Correct |
95 ms |
8428 KB |
Output is correct |
8 |
Correct |
103 ms |
8300 KB |
Output is correct |
9 |
Correct |
101 ms |
8428 KB |
Output is correct |
10 |
Correct |
89 ms |
8428 KB |
Output is correct |
11 |
Correct |
79 ms |
8172 KB |
Output is correct |
12 |
Correct |
86 ms |
7680 KB |
Output is correct |
13 |
Correct |
99 ms |
7916 KB |
Output is correct |
14 |
Correct |
921 ms |
50796 KB |
Output is correct |
15 |
Correct |
75 ms |
7532 KB |
Output is correct |
16 |
Correct |
857 ms |
51052 KB |
Output is correct |
17 |
Correct |
1136 ms |
64256 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
8 ms |
1132 KB |
Output is correct |
3 |
Correct |
8 ms |
1408 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
2 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
8 ms |
1260 KB |
Output is correct |
17 |
Correct |
68 ms |
8044 KB |
Output is correct |
18 |
Correct |
2 ms |
492 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
2 ms |
512 KB |
Output is correct |
26 |
Correct |
2 ms |
492 KB |
Output is correct |
27 |
Correct |
2 ms |
492 KB |
Output is correct |
28 |
Correct |
460 ms |
99820 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
626 ms |
53476 KB |
Output is correct |
31 |
Correct |
1014 ms |
106024 KB |
Output is correct |
32 |
Correct |
987 ms |
106392 KB |
Output is correct |
33 |
Correct |
1 ms |
364 KB |
Output is correct |
34 |
Correct |
986 ms |
102396 KB |
Output is correct |
35 |
Correct |
590 ms |
48876 KB |
Output is correct |
36 |
Correct |
1036 ms |
106728 KB |
Output is correct |
37 |
Correct |
968 ms |
105836 KB |
Output is correct |
38 |
Correct |
1 ms |
364 KB |
Output is correct |
39 |
Correct |
80 ms |
11628 KB |
Output is correct |
40 |
Correct |
98 ms |
13676 KB |
Output is correct |
41 |
Correct |
96 ms |
12780 KB |
Output is correct |
42 |
Correct |
93 ms |
12780 KB |
Output is correct |
43 |
Correct |
86 ms |
10732 KB |
Output is correct |
44 |
Correct |
98 ms |
12780 KB |
Output is correct |
45 |
Correct |
97 ms |
13164 KB |
Output is correct |
46 |
Correct |
96 ms |
12780 KB |
Output is correct |
47 |
Correct |
75 ms |
8556 KB |
Output is correct |
48 |
Correct |
107 ms |
12396 KB |
Output is correct |
49 |
Correct |
90 ms |
7404 KB |
Output is correct |
50 |
Correct |
139 ms |
14316 KB |
Output is correct |
51 |
Correct |
1397 ms |
106468 KB |
Output is correct |
52 |
Correct |
1456 ms |
105980 KB |
Output is correct |
53 |
Correct |
1468 ms |
106148 KB |
Output is correct |
54 |
Correct |
109 ms |
14316 KB |
Output is correct |
55 |
Correct |
106 ms |
14316 KB |
Output is correct |
56 |
Correct |
194 ms |
37228 KB |
Output is correct |
57 |
Correct |
1 ms |
364 KB |
Output is correct |
58 |
Correct |
80 ms |
6124 KB |
Output is correct |
59 |
Correct |
92 ms |
8172 KB |
Output is correct |
60 |
Correct |
1524 ms |
64836 KB |
Output is correct |
61 |
Correct |
97 ms |
7788 KB |
Output is correct |
62 |
Correct |
111 ms |
7788 KB |
Output is correct |
63 |
Correct |
95 ms |
8428 KB |
Output is correct |
64 |
Correct |
103 ms |
8300 KB |
Output is correct |
65 |
Correct |
101 ms |
8428 KB |
Output is correct |
66 |
Correct |
89 ms |
8428 KB |
Output is correct |
67 |
Correct |
79 ms |
8172 KB |
Output is correct |
68 |
Correct |
86 ms |
7680 KB |
Output is correct |
69 |
Correct |
99 ms |
7916 KB |
Output is correct |
70 |
Correct |
921 ms |
50796 KB |
Output is correct |
71 |
Correct |
75 ms |
7532 KB |
Output is correct |
72 |
Correct |
857 ms |
51052 KB |
Output is correct |
73 |
Correct |
1136 ms |
64256 KB |
Output is correct |
74 |
Correct |
1 ms |
364 KB |
Output is correct |
75 |
Correct |
1 ms |
364 KB |
Output is correct |
76 |
Correct |
1 ms |
364 KB |
Output is correct |
77 |
Correct |
1 ms |
364 KB |
Output is correct |
78 |
Correct |
1 ms |
364 KB |
Output is correct |
79 |
Correct |
10 ms |
1388 KB |
Output is correct |
80 |
Correct |
1109 ms |
52108 KB |
Output is correct |
81 |
Correct |
1567 ms |
64492 KB |
Output is correct |
82 |
Correct |
1697 ms |
103276 KB |
Output is correct |
83 |
Correct |
1869 ms |
103232 KB |
Output is correct |
84 |
Correct |
1849 ms |
102124 KB |
Output is correct |
85 |
Correct |
1795 ms |
105068 KB |
Output is correct |
86 |
Correct |
194 ms |
38252 KB |
Output is correct |
87 |
Correct |
1 ms |
364 KB |
Output is correct |