#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define ld long double
#define ll long long
mt19937 rnd(51);
ll gcd(ll a, ll b) {
if (b == 0) {
return a;
} else {
return gcd(b, a % b);
}
}
__int128 lcm(ll a, ll b) {
return (__int128)a * b / gcd(a, b);
}
ll solve(ll n, ll a, ll b, vector<ll> l, vector<ll> r) {
ll ans = 0, need;
bool was = 0, sum = 0;
ll k = lcm(a, b + 1) / (__int128)(b + 1);
if (log2(k) + log2(b) > log2(1e18) + 1) {
sum = 1;
} else {
need = k * b;
}
vector<pair<ll, ll>> seg, u;
for (int i = 0; i < n; i++) {
if (sum) {
ans += r[i] - l[i] + 1;
} else {
if (r[i] - l[i] + 1 >= need) {
was = 1;
} else {
ll l1 = l[i] % (need), r1 = r[i] % (need);
if (l1 <= r1) {
seg.pb({l1, r1});
} else {
seg.pb({l1, need - 1});
seg.pb({0, r1});
}
}
}
}
if (was) {
return need;
}
if (sum) {
return ans;
} else {
for (auto to : seg) {
u.pb({to.first, 1});
u.pb({to.second + 1, -1});
}
sort(u.begin(), u.end());
ll bal = 0, last = -1;
for (auto to : u) {
if (bal > 0) {
ans += to.first - last;
}
bal += to.second;
last = to.first;
}
return ans;
}
}
signed main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif // LCOAL
ios_base::sync_with_stdio(0);
cin.tie(0);
ll n, a, b;
cin >> n >> a >> b;
vector<ll> l(n), r(n);
for (int i = 0; i < n; i++) {
cin >> l[i] >> r[i];
}
cout << solve(n, a, b, l, r) << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
5 ms |
1364 KB |
Output is correct |
3 |
Correct |
6 ms |
1364 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
6 ms |
1364 KB |
Output is correct |
17 |
Correct |
59 ms |
9240 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
373 ms |
80328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
521 ms |
80196 KB |
Output is correct |
3 |
Correct |
551 ms |
80292 KB |
Output is correct |
4 |
Correct |
509 ms |
80344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
521 ms |
80196 KB |
Output is correct |
3 |
Correct |
551 ms |
80292 KB |
Output is correct |
4 |
Correct |
509 ms |
80344 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
540 ms |
80280 KB |
Output is correct |
7 |
Correct |
556 ms |
117408 KB |
Output is correct |
8 |
Correct |
562 ms |
117316 KB |
Output is correct |
9 |
Correct |
634 ms |
117300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
521 ms |
80196 KB |
Output is correct |
3 |
Correct |
551 ms |
80292 KB |
Output is correct |
4 |
Correct |
509 ms |
80344 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
53 ms |
9188 KB |
Output is correct |
7 |
Correct |
51 ms |
9248 KB |
Output is correct |
8 |
Correct |
50 ms |
9176 KB |
Output is correct |
9 |
Correct |
55 ms |
9188 KB |
Output is correct |
10 |
Correct |
51 ms |
9252 KB |
Output is correct |
11 |
Correct |
52 ms |
9188 KB |
Output is correct |
12 |
Correct |
47 ms |
9208 KB |
Output is correct |
13 |
Correct |
55 ms |
9192 KB |
Output is correct |
14 |
Correct |
49 ms |
9160 KB |
Output is correct |
15 |
Correct |
60 ms |
9188 KB |
Output is correct |
16 |
Correct |
62 ms |
9192 KB |
Output is correct |
17 |
Correct |
52 ms |
9284 KB |
Output is correct |
18 |
Correct |
555 ms |
117420 KB |
Output is correct |
19 |
Correct |
536 ms |
117420 KB |
Output is correct |
20 |
Correct |
639 ms |
117356 KB |
Output is correct |
21 |
Correct |
67 ms |
12996 KB |
Output is correct |
22 |
Correct |
49 ms |
12884 KB |
Output is correct |
23 |
Correct |
152 ms |
45428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
59 ms |
9188 KB |
Output is correct |
3 |
Correct |
56 ms |
9160 KB |
Output is correct |
4 |
Correct |
646 ms |
80196 KB |
Output is correct |
5 |
Correct |
56 ms |
9160 KB |
Output is correct |
6 |
Correct |
56 ms |
9192 KB |
Output is correct |
7 |
Correct |
56 ms |
9196 KB |
Output is correct |
8 |
Correct |
58 ms |
9160 KB |
Output is correct |
9 |
Correct |
56 ms |
9188 KB |
Output is correct |
10 |
Correct |
60 ms |
9232 KB |
Output is correct |
11 |
Correct |
56 ms |
9212 KB |
Output is correct |
12 |
Correct |
50 ms |
9188 KB |
Output is correct |
13 |
Correct |
59 ms |
9184 KB |
Output is correct |
14 |
Correct |
591 ms |
80188 KB |
Output is correct |
15 |
Correct |
60 ms |
9296 KB |
Output is correct |
16 |
Correct |
497 ms |
80196 KB |
Output is correct |
17 |
Correct |
522 ms |
80288 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
5 ms |
1364 KB |
Output is correct |
3 |
Correct |
6 ms |
1364 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
6 ms |
1364 KB |
Output is correct |
17 |
Correct |
59 ms |
9240 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
373 ms |
80328 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
521 ms |
80196 KB |
Output is correct |
31 |
Correct |
551 ms |
80292 KB |
Output is correct |
32 |
Correct |
509 ms |
80344 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
540 ms |
80280 KB |
Output is correct |
35 |
Correct |
556 ms |
117408 KB |
Output is correct |
36 |
Correct |
562 ms |
117316 KB |
Output is correct |
37 |
Correct |
634 ms |
117300 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
53 ms |
9188 KB |
Output is correct |
40 |
Correct |
51 ms |
9248 KB |
Output is correct |
41 |
Correct |
50 ms |
9176 KB |
Output is correct |
42 |
Correct |
55 ms |
9188 KB |
Output is correct |
43 |
Correct |
51 ms |
9252 KB |
Output is correct |
44 |
Correct |
52 ms |
9188 KB |
Output is correct |
45 |
Correct |
47 ms |
9208 KB |
Output is correct |
46 |
Correct |
55 ms |
9192 KB |
Output is correct |
47 |
Correct |
49 ms |
9160 KB |
Output is correct |
48 |
Correct |
60 ms |
9188 KB |
Output is correct |
49 |
Correct |
62 ms |
9192 KB |
Output is correct |
50 |
Correct |
52 ms |
9284 KB |
Output is correct |
51 |
Correct |
555 ms |
117420 KB |
Output is correct |
52 |
Correct |
536 ms |
117420 KB |
Output is correct |
53 |
Correct |
639 ms |
117356 KB |
Output is correct |
54 |
Correct |
67 ms |
12996 KB |
Output is correct |
55 |
Correct |
49 ms |
12884 KB |
Output is correct |
56 |
Correct |
152 ms |
45428 KB |
Output is correct |
57 |
Correct |
0 ms |
212 KB |
Output is correct |
58 |
Correct |
59 ms |
9188 KB |
Output is correct |
59 |
Correct |
56 ms |
9160 KB |
Output is correct |
60 |
Correct |
646 ms |
80196 KB |
Output is correct |
61 |
Correct |
56 ms |
9160 KB |
Output is correct |
62 |
Correct |
56 ms |
9192 KB |
Output is correct |
63 |
Correct |
56 ms |
9196 KB |
Output is correct |
64 |
Correct |
58 ms |
9160 KB |
Output is correct |
65 |
Correct |
56 ms |
9188 KB |
Output is correct |
66 |
Correct |
60 ms |
9232 KB |
Output is correct |
67 |
Correct |
56 ms |
9212 KB |
Output is correct |
68 |
Correct |
50 ms |
9188 KB |
Output is correct |
69 |
Correct |
59 ms |
9184 KB |
Output is correct |
70 |
Correct |
591 ms |
80188 KB |
Output is correct |
71 |
Correct |
60 ms |
9296 KB |
Output is correct |
72 |
Correct |
497 ms |
80196 KB |
Output is correct |
73 |
Correct |
522 ms |
80288 KB |
Output is correct |
74 |
Correct |
0 ms |
212 KB |
Output is correct |
75 |
Correct |
1 ms |
212 KB |
Output is correct |
76 |
Correct |
1 ms |
212 KB |
Output is correct |
77 |
Correct |
1 ms |
212 KB |
Output is correct |
78 |
Correct |
1 ms |
212 KB |
Output is correct |
79 |
Correct |
6 ms |
1748 KB |
Output is correct |
80 |
Correct |
650 ms |
117424 KB |
Output is correct |
81 |
Correct |
665 ms |
117316 KB |
Output is correct |
82 |
Correct |
614 ms |
117488 KB |
Output is correct |
83 |
Correct |
622 ms |
117536 KB |
Output is correct |
84 |
Correct |
628 ms |
117308 KB |
Output is correct |
85 |
Correct |
617 ms |
117420 KB |
Output is correct |
86 |
Correct |
156 ms |
45376 KB |
Output is correct |
87 |
Correct |
1 ms |
328 KB |
Output is correct |