#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define ld long double
#define ll long long
mt19937 rnd(51);
ll gcd(int a, int 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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
6 ms |
1364 KB |
Output is correct |
3 |
Correct |
6 ms |
1364 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
216 KB |
Output is correct |
16 |
Correct |
6 ms |
1368 KB |
Output is correct |
17 |
Correct |
65 ms |
9188 KB |
Output is correct |
18 |
Correct |
1 ms |
220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
394 ms |
80296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
216 KB |
Output is correct |
2 |
Correct |
561 ms |
80380 KB |
Output is correct |
3 |
Correct |
566 ms |
80332 KB |
Output is correct |
4 |
Correct |
517 ms |
80244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
216 KB |
Output is correct |
2 |
Correct |
561 ms |
80380 KB |
Output is correct |
3 |
Correct |
566 ms |
80332 KB |
Output is correct |
4 |
Correct |
517 ms |
80244 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Incorrect |
516 ms |
80188 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
216 KB |
Output is correct |
2 |
Correct |
561 ms |
80380 KB |
Output is correct |
3 |
Correct |
566 ms |
80332 KB |
Output is correct |
4 |
Correct |
517 ms |
80244 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
56 ms |
9188 KB |
Output is correct |
7 |
Correct |
53 ms |
9176 KB |
Output is correct |
8 |
Correct |
51 ms |
9172 KB |
Output is correct |
9 |
Correct |
54 ms |
9160 KB |
Output is correct |
10 |
Correct |
50 ms |
9192 KB |
Output is correct |
11 |
Correct |
56 ms |
9184 KB |
Output is correct |
12 |
Correct |
50 ms |
9192 KB |
Output is correct |
13 |
Correct |
55 ms |
9180 KB |
Output is correct |
14 |
Correct |
50 ms |
9164 KB |
Output is correct |
15 |
Correct |
63 ms |
9200 KB |
Output is correct |
16 |
Correct |
59 ms |
9192 KB |
Output is correct |
17 |
Incorrect |
32 ms |
3404 KB |
Output isn't correct |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
57 ms |
9180 KB |
Output is correct |
3 |
Correct |
59 ms |
9192 KB |
Output is correct |
4 |
Correct |
684 ms |
80184 KB |
Output is correct |
5 |
Correct |
58 ms |
9196 KB |
Output is correct |
6 |
Incorrect |
59 ms |
9160 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
6 ms |
1364 KB |
Output is correct |
3 |
Correct |
6 ms |
1364 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
216 KB |
Output is correct |
16 |
Correct |
6 ms |
1368 KB |
Output is correct |
17 |
Correct |
65 ms |
9188 KB |
Output is correct |
18 |
Correct |
1 ms |
220 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
2 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
394 ms |
80296 KB |
Output is correct |
29 |
Correct |
1 ms |
216 KB |
Output is correct |
30 |
Correct |
561 ms |
80380 KB |
Output is correct |
31 |
Correct |
566 ms |
80332 KB |
Output is correct |
32 |
Correct |
517 ms |
80244 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Incorrect |
516 ms |
80188 KB |
Output isn't correct |
35 |
Halted |
0 ms |
0 KB |
- |