#include <bits/stdc++.h>
using namespace std;
#ifdef ngu
#include "debug.h"
#else
#define debug(...) 42
#endif
const int N = (int) 1e6 + 5;
const long long LIM = (long long) 1e18;
int n, m;
long long a, b, c;
long long l[N], r[N];
long long f[4 * N];
vector<long long> rem;
void upd(int l, int r, long long x) {
f[l] += x;
f[r + 1] -= x;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
#ifdef ngu
freopen("test.inp", "r", stdin);
freopen("test.out", "w", stdout);
#endif
cin >> n >> a >> b;
a /= __gcd(a, b + 1);
if (a <= LIM / b) {
c = a * b;
} else {
c = LIM + 1;
}
rem.emplace_back(0);
rem.emplace_back(c);
for (int i = 1; i <= n; ++i) {
cin >> l[i] >> r[i];
rem.emplace_back(l[i] % c);
if (l[i] % c - 1 >= 0) {
rem.emplace_back(l[i] % c - 1);
}
rem.emplace_back(r[i] % c);
if (r[i] % c + 1 < c) {
rem.emplace_back(r[i] % c + 1);
}
}
sort(rem.begin(), rem.end());
rem.erase(unique(rem.begin(), rem.end()), rem.end());
m = (int) rem.size() - 2;
auto get_pos = [&](long long i) {
return lower_bound(rem.begin(), rem.end(), i) - rem.begin();
};
for (int i = 1; i <= n; ++i) {
long long cnt = r[i] / c - l[i] / c;
int L = get_pos(l[i] % c);
int R = get_pos(r[i] % c);
if (cnt) {
if (R < L) {
if (R + 1 <= L - 1) {
upd(R + 1, L - 1, cnt - 1);
}
upd(0, R, cnt);
upd(L, m, cnt);
} else {
upd(L, R, cnt + 1);
if (0 <= L - 1) {
upd(0, L - 1, cnt);
}
if (R + 1 <= m) {
upd(R + 1, m, cnt);
}
}
} else {
upd(L, R, 1);
}
}
long long res = 0;
for (int i = 0; i <= m; ++i) {
if (i) {
f[i] += f[i - 1];
}
res += (f[i] > 0) * (rem[i + 1] - rem[i]);
}
cout << res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
8 ms |
7648 KB |
Output is correct |
3 |
Correct |
7 ms |
7648 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4556 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4440 KB |
Output is correct |
16 |
Correct |
7 ms |
7644 KB |
Output is correct |
17 |
Correct |
64 ms |
15804 KB |
Output is correct |
18 |
Correct |
1 ms |
4440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4568 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4576 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
2 ms |
4444 KB |
Output is correct |
5 |
Correct |
278 ms |
66412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
648 ms |
88904 KB |
Output is correct |
3 |
Correct |
652 ms |
97516 KB |
Output is correct |
4 |
Correct |
551 ms |
101432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
648 ms |
88904 KB |
Output is correct |
3 |
Correct |
652 ms |
97516 KB |
Output is correct |
4 |
Correct |
551 ms |
101432 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
582 ms |
102672 KB |
Output is correct |
7 |
Correct |
555 ms |
93820 KB |
Output is correct |
8 |
Correct |
597 ms |
101448 KB |
Output is correct |
9 |
Correct |
757 ms |
101396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
648 ms |
88904 KB |
Output is correct |
3 |
Correct |
652 ms |
97516 KB |
Output is correct |
4 |
Correct |
551 ms |
101432 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
64 ms |
16580 KB |
Output is correct |
7 |
Correct |
66 ms |
16300 KB |
Output is correct |
8 |
Correct |
49 ms |
15812 KB |
Output is correct |
9 |
Correct |
56 ms |
16368 KB |
Output is correct |
10 |
Correct |
62 ms |
15808 KB |
Output is correct |
11 |
Correct |
62 ms |
15808 KB |
Output is correct |
12 |
Correct |
58 ms |
15808 KB |
Output is correct |
13 |
Correct |
75 ms |
16652 KB |
Output is correct |
14 |
Correct |
57 ms |
16568 KB |
Output is correct |
15 |
Correct |
70 ms |
15636 KB |
Output is correct |
16 |
Correct |
73 ms |
15652 KB |
Output is correct |
17 |
Correct |
55 ms |
15808 KB |
Output is correct |
18 |
Correct |
614 ms |
101364 KB |
Output is correct |
19 |
Correct |
593 ms |
101640 KB |
Output is correct |
20 |
Correct |
714 ms |
102168 KB |
Output is correct |
21 |
Correct |
73 ms |
15804 KB |
Output is correct |
22 |
Correct |
70 ms |
15972 KB |
Output is correct |
23 |
Correct |
107 ms |
31416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
67 ms |
15960 KB |
Output is correct |
3 |
Correct |
68 ms |
15804 KB |
Output is correct |
4 |
Correct |
740 ms |
100880 KB |
Output is correct |
5 |
Correct |
75 ms |
16316 KB |
Output is correct |
6 |
Correct |
77 ms |
15636 KB |
Output is correct |
7 |
Correct |
69 ms |
16060 KB |
Output is correct |
8 |
Correct |
65 ms |
15676 KB |
Output is correct |
9 |
Correct |
64 ms |
15808 KB |
Output is correct |
10 |
Correct |
68 ms |
16508 KB |
Output is correct |
11 |
Correct |
70 ms |
16320 KB |
Output is correct |
12 |
Correct |
51 ms |
15808 KB |
Output is correct |
13 |
Correct |
69 ms |
15804 KB |
Output is correct |
14 |
Correct |
741 ms |
99564 KB |
Output is correct |
15 |
Correct |
66 ms |
15708 KB |
Output is correct |
16 |
Correct |
597 ms |
102320 KB |
Output is correct |
17 |
Correct |
609 ms |
102484 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
8 ms |
7648 KB |
Output is correct |
3 |
Correct |
7 ms |
7648 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4556 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4440 KB |
Output is correct |
16 |
Correct |
7 ms |
7644 KB |
Output is correct |
17 |
Correct |
64 ms |
15804 KB |
Output is correct |
18 |
Correct |
1 ms |
4440 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4568 KB |
Output is correct |
23 |
Correct |
1 ms |
4444 KB |
Output is correct |
24 |
Correct |
1 ms |
4444 KB |
Output is correct |
25 |
Correct |
1 ms |
4576 KB |
Output is correct |
26 |
Correct |
1 ms |
4444 KB |
Output is correct |
27 |
Correct |
2 ms |
4444 KB |
Output is correct |
28 |
Correct |
278 ms |
66412 KB |
Output is correct |
29 |
Correct |
1 ms |
4440 KB |
Output is correct |
30 |
Correct |
648 ms |
88904 KB |
Output is correct |
31 |
Correct |
652 ms |
97516 KB |
Output is correct |
32 |
Correct |
551 ms |
101432 KB |
Output is correct |
33 |
Correct |
1 ms |
4444 KB |
Output is correct |
34 |
Correct |
582 ms |
102672 KB |
Output is correct |
35 |
Correct |
555 ms |
93820 KB |
Output is correct |
36 |
Correct |
597 ms |
101448 KB |
Output is correct |
37 |
Correct |
757 ms |
101396 KB |
Output is correct |
38 |
Correct |
1 ms |
4444 KB |
Output is correct |
39 |
Correct |
64 ms |
16580 KB |
Output is correct |
40 |
Correct |
66 ms |
16300 KB |
Output is correct |
41 |
Correct |
49 ms |
15812 KB |
Output is correct |
42 |
Correct |
56 ms |
16368 KB |
Output is correct |
43 |
Correct |
62 ms |
15808 KB |
Output is correct |
44 |
Correct |
62 ms |
15808 KB |
Output is correct |
45 |
Correct |
58 ms |
15808 KB |
Output is correct |
46 |
Correct |
75 ms |
16652 KB |
Output is correct |
47 |
Correct |
57 ms |
16568 KB |
Output is correct |
48 |
Correct |
70 ms |
15636 KB |
Output is correct |
49 |
Correct |
73 ms |
15652 KB |
Output is correct |
50 |
Correct |
55 ms |
15808 KB |
Output is correct |
51 |
Correct |
614 ms |
101364 KB |
Output is correct |
52 |
Correct |
593 ms |
101640 KB |
Output is correct |
53 |
Correct |
714 ms |
102168 KB |
Output is correct |
54 |
Correct |
73 ms |
15804 KB |
Output is correct |
55 |
Correct |
70 ms |
15972 KB |
Output is correct |
56 |
Correct |
107 ms |
31416 KB |
Output is correct |
57 |
Correct |
1 ms |
4440 KB |
Output is correct |
58 |
Correct |
67 ms |
15960 KB |
Output is correct |
59 |
Correct |
68 ms |
15804 KB |
Output is correct |
60 |
Correct |
740 ms |
100880 KB |
Output is correct |
61 |
Correct |
75 ms |
16316 KB |
Output is correct |
62 |
Correct |
77 ms |
15636 KB |
Output is correct |
63 |
Correct |
69 ms |
16060 KB |
Output is correct |
64 |
Correct |
65 ms |
15676 KB |
Output is correct |
65 |
Correct |
64 ms |
15808 KB |
Output is correct |
66 |
Correct |
68 ms |
16508 KB |
Output is correct |
67 |
Correct |
70 ms |
16320 KB |
Output is correct |
68 |
Correct |
51 ms |
15808 KB |
Output is correct |
69 |
Correct |
69 ms |
15804 KB |
Output is correct |
70 |
Correct |
741 ms |
99564 KB |
Output is correct |
71 |
Correct |
66 ms |
15708 KB |
Output is correct |
72 |
Correct |
597 ms |
102320 KB |
Output is correct |
73 |
Correct |
609 ms |
102484 KB |
Output is correct |
74 |
Correct |
1 ms |
4444 KB |
Output is correct |
75 |
Correct |
1 ms |
4440 KB |
Output is correct |
76 |
Correct |
1 ms |
4444 KB |
Output is correct |
77 |
Correct |
1 ms |
4560 KB |
Output is correct |
78 |
Correct |
1 ms |
4444 KB |
Output is correct |
79 |
Correct |
6 ms |
7648 KB |
Output is correct |
80 |
Correct |
758 ms |
101432 KB |
Output is correct |
81 |
Correct |
758 ms |
101576 KB |
Output is correct |
82 |
Correct |
726 ms |
101836 KB |
Output is correct |
83 |
Correct |
737 ms |
102284 KB |
Output is correct |
84 |
Correct |
731 ms |
101496 KB |
Output is correct |
85 |
Correct |
778 ms |
101504 KB |
Output is correct |
86 |
Correct |
105 ms |
31160 KB |
Output is correct |
87 |
Correct |
1 ms |
4440 KB |
Output is correct |