#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;
if (log2(a) + log2(b) > log2(1e18) + 1) {
sum = 1;
} else {
ll k = lcm(a, b + 1) / (b + 1);
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 |
7 ms |
1364 KB |
Output is correct |
3 |
Correct |
9 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 |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 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 |
340 KB |
Output is correct |
16 |
Correct |
6 ms |
1444 KB |
Output is correct |
17 |
Correct |
58 ms |
9260 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
# |
결과 |
실행 시간 |
메모리 |
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 |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
376 ms |
80204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
534 ms |
80188 KB |
Output is correct |
3 |
Correct |
541 ms |
80188 KB |
Output is correct |
4 |
Correct |
542 ms |
117408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
534 ms |
80188 KB |
Output is correct |
3 |
Correct |
541 ms |
80188 KB |
Output is correct |
4 |
Correct |
542 ms |
117408 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Incorrect |
534 ms |
117416 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
534 ms |
80188 KB |
Output is correct |
3 |
Correct |
541 ms |
80188 KB |
Output is correct |
4 |
Correct |
542 ms |
117408 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
60 ms |
12872 KB |
Output is correct |
7 |
Correct |
56 ms |
12888 KB |
Output is correct |
8 |
Correct |
53 ms |
12948 KB |
Output is correct |
9 |
Correct |
54 ms |
12872 KB |
Output is correct |
10 |
Correct |
54 ms |
12888 KB |
Output is correct |
11 |
Correct |
56 ms |
12868 KB |
Output is correct |
12 |
Correct |
53 ms |
12872 KB |
Output is correct |
13 |
Correct |
61 ms |
12880 KB |
Output is correct |
14 |
Correct |
53 ms |
12872 KB |
Output is correct |
15 |
Correct |
64 ms |
12872 KB |
Output is correct |
16 |
Correct |
60 ms |
12880 KB |
Output is correct |
17 |
Incorrect |
36 ms |
7128 KB |
Output isn't correct |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
57 ms |
9184 KB |
Output is correct |
3 |
Correct |
64 ms |
9192 KB |
Output is correct |
4 |
Correct |
655 ms |
80308 KB |
Output is correct |
5 |
Correct |
58 ms |
9208 KB |
Output is correct |
6 |
Incorrect |
63 ms |
9176 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
7 ms |
1364 KB |
Output is correct |
3 |
Correct |
9 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 |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 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 |
340 KB |
Output is correct |
16 |
Correct |
6 ms |
1444 KB |
Output is correct |
17 |
Correct |
58 ms |
9260 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 |
2 ms |
340 KB |
Output is correct |
28 |
Correct |
376 ms |
80204 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
534 ms |
80188 KB |
Output is correct |
31 |
Correct |
541 ms |
80188 KB |
Output is correct |
32 |
Correct |
542 ms |
117408 KB |
Output is correct |
33 |
Correct |
1 ms |
332 KB |
Output is correct |
34 |
Incorrect |
534 ms |
117416 KB |
Output isn't correct |
35 |
Halted |
0 ms |
0 KB |
- |