#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ld long double
#define fi first
#define se second
ll l[1000005], r[1000005];
vector<pair<ll, ll>> v, ans;
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
/*
t = xb;
xb + x = y * a
x * (b + 1) = y * a
x = lcm(b + 1, a) / (b + 1)
a/gcd(b + 1, a) = lcm(b + 1, a) / (b + 1);
x = a/gcd(b + 1, a)
banyaknya possibility = x * b -> x = a / gcd(b + 1, a);
x * b >= r
b / x
*/
int n;
ll a, b;
cin >> n >> a >> b;
ll x = a / gcd(b + 1, a), sum = 0, ans1 = 0;
for (int i = 1; i <= n; i++) {
cin >> l[i] >> r[i];
sum += r[i] - l[i] + 1;
}
if ((r[n] - 1) / x < b) {
cout << sum << "\n";
return 0;
}
for (int i = 1; i <= n; i++) {
if (r[i] - l[i] + 1 >= x * b) {
cout << x * b;
return 0;
}
l[i] %= (x * b), r[i] %= (x * b);
if (l[i] <= r[i]) v.push_back({l[i], r[i]});
else v.push_back({l[i], x * b - 1}), v.push_back({0, r[i]});
}
sort(v.begin(), v.end());
for (int i = 0; i < (int)v.size(); i++) {
if (ans.empty()) {
ans.push_back(v[i]);
continue;
}
if (v[i].fi > ans.back().se) ans.push_back(v[i]);
else ans.back().se = max(ans.back().se, v[i].se);
}
for (auto u : ans) ans1 += u.se - u.fi + 1;
cout << ans1 << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
324 KB |
Output is correct |
2 |
Correct |
4 ms |
1372 KB |
Output is correct |
3 |
Correct |
4 ms |
1368 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
340 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 |
220 KB |
Output is correct |
11 |
Correct |
1 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 |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
4 ms |
1368 KB |
Output is correct |
17 |
Correct |
36 ms |
9880 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
0 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
221 ms |
35220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
356 ms |
60508 KB |
Output is correct |
3 |
Correct |
341 ms |
56984 KB |
Output is correct |
4 |
Correct |
331 ms |
57072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
356 ms |
60508 KB |
Output is correct |
3 |
Correct |
341 ms |
56984 KB |
Output is correct |
4 |
Correct |
331 ms |
57072 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
321 ms |
57032 KB |
Output is correct |
7 |
Correct |
334 ms |
57064 KB |
Output is correct |
8 |
Correct |
329 ms |
57044 KB |
Output is correct |
9 |
Correct |
343 ms |
57008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
356 ms |
60508 KB |
Output is correct |
3 |
Correct |
341 ms |
56984 KB |
Output is correct |
4 |
Correct |
331 ms |
57072 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
32 ms |
10140 KB |
Output is correct |
7 |
Correct |
36 ms |
9800 KB |
Output is correct |
8 |
Correct |
38 ms |
9900 KB |
Output is correct |
9 |
Correct |
33 ms |
9872 KB |
Output is correct |
10 |
Correct |
39 ms |
9860 KB |
Output is correct |
11 |
Correct |
36 ms |
9880 KB |
Output is correct |
12 |
Correct |
34 ms |
9868 KB |
Output is correct |
13 |
Correct |
44 ms |
9872 KB |
Output is correct |
14 |
Correct |
33 ms |
9772 KB |
Output is correct |
15 |
Correct |
38 ms |
9760 KB |
Output is correct |
16 |
Correct |
36 ms |
9876 KB |
Output is correct |
17 |
Correct |
36 ms |
9792 KB |
Output is correct |
18 |
Correct |
327 ms |
57028 KB |
Output is correct |
19 |
Correct |
318 ms |
57040 KB |
Output is correct |
20 |
Correct |
347 ms |
57004 KB |
Output is correct |
21 |
Correct |
36 ms |
9992 KB |
Output is correct |
22 |
Correct |
31 ms |
9888 KB |
Output is correct |
23 |
Correct |
101 ms |
21104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
35 ms |
10020 KB |
Output is correct |
3 |
Correct |
42 ms |
10156 KB |
Output is correct |
4 |
Correct |
380 ms |
57100 KB |
Output is correct |
5 |
Correct |
40 ms |
10072 KB |
Output is correct |
6 |
Correct |
35 ms |
10084 KB |
Output is correct |
7 |
Correct |
35 ms |
10056 KB |
Output is correct |
8 |
Correct |
37 ms |
10080 KB |
Output is correct |
9 |
Correct |
35 ms |
10056 KB |
Output is correct |
10 |
Correct |
34 ms |
10128 KB |
Output is correct |
11 |
Correct |
36 ms |
10052 KB |
Output is correct |
12 |
Correct |
31 ms |
10124 KB |
Output is correct |
13 |
Correct |
34 ms |
10100 KB |
Output is correct |
14 |
Correct |
345 ms |
57184 KB |
Output is correct |
15 |
Correct |
36 ms |
10268 KB |
Output is correct |
16 |
Correct |
321 ms |
57064 KB |
Output is correct |
17 |
Correct |
323 ms |
56984 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
324 KB |
Output is correct |
2 |
Correct |
4 ms |
1372 KB |
Output is correct |
3 |
Correct |
4 ms |
1368 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
340 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 |
220 KB |
Output is correct |
11 |
Correct |
1 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 |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
4 ms |
1368 KB |
Output is correct |
17 |
Correct |
36 ms |
9880 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
328 KB |
Output is correct |
20 |
Correct |
0 ms |
328 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
221 ms |
35220 KB |
Output is correct |
29 |
Correct |
1 ms |
328 KB |
Output is correct |
30 |
Correct |
356 ms |
60508 KB |
Output is correct |
31 |
Correct |
341 ms |
56984 KB |
Output is correct |
32 |
Correct |
331 ms |
57072 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
321 ms |
57032 KB |
Output is correct |
35 |
Correct |
334 ms |
57064 KB |
Output is correct |
36 |
Correct |
329 ms |
57044 KB |
Output is correct |
37 |
Correct |
343 ms |
57008 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
32 ms |
10140 KB |
Output is correct |
40 |
Correct |
36 ms |
9800 KB |
Output is correct |
41 |
Correct |
38 ms |
9900 KB |
Output is correct |
42 |
Correct |
33 ms |
9872 KB |
Output is correct |
43 |
Correct |
39 ms |
9860 KB |
Output is correct |
44 |
Correct |
36 ms |
9880 KB |
Output is correct |
45 |
Correct |
34 ms |
9868 KB |
Output is correct |
46 |
Correct |
44 ms |
9872 KB |
Output is correct |
47 |
Correct |
33 ms |
9772 KB |
Output is correct |
48 |
Correct |
38 ms |
9760 KB |
Output is correct |
49 |
Correct |
36 ms |
9876 KB |
Output is correct |
50 |
Correct |
36 ms |
9792 KB |
Output is correct |
51 |
Correct |
327 ms |
57028 KB |
Output is correct |
52 |
Correct |
318 ms |
57040 KB |
Output is correct |
53 |
Correct |
347 ms |
57004 KB |
Output is correct |
54 |
Correct |
36 ms |
9992 KB |
Output is correct |
55 |
Correct |
31 ms |
9888 KB |
Output is correct |
56 |
Correct |
101 ms |
21104 KB |
Output is correct |
57 |
Correct |
1 ms |
340 KB |
Output is correct |
58 |
Correct |
35 ms |
10020 KB |
Output is correct |
59 |
Correct |
42 ms |
10156 KB |
Output is correct |
60 |
Correct |
380 ms |
57100 KB |
Output is correct |
61 |
Correct |
40 ms |
10072 KB |
Output is correct |
62 |
Correct |
35 ms |
10084 KB |
Output is correct |
63 |
Correct |
35 ms |
10056 KB |
Output is correct |
64 |
Correct |
37 ms |
10080 KB |
Output is correct |
65 |
Correct |
35 ms |
10056 KB |
Output is correct |
66 |
Correct |
34 ms |
10128 KB |
Output is correct |
67 |
Correct |
36 ms |
10052 KB |
Output is correct |
68 |
Correct |
31 ms |
10124 KB |
Output is correct |
69 |
Correct |
34 ms |
10100 KB |
Output is correct |
70 |
Correct |
345 ms |
57184 KB |
Output is correct |
71 |
Correct |
36 ms |
10268 KB |
Output is correct |
72 |
Correct |
321 ms |
57064 KB |
Output is correct |
73 |
Correct |
323 ms |
56984 KB |
Output is correct |
74 |
Correct |
1 ms |
212 KB |
Output is correct |
75 |
Correct |
0 ms |
340 KB |
Output is correct |
76 |
Correct |
1 ms |
324 KB |
Output is correct |
77 |
Correct |
1 ms |
340 KB |
Output is correct |
78 |
Correct |
1 ms |
212 KB |
Output is correct |
79 |
Correct |
4 ms |
1368 KB |
Output is correct |
80 |
Correct |
370 ms |
58048 KB |
Output is correct |
81 |
Correct |
367 ms |
59056 KB |
Output is correct |
82 |
Correct |
345 ms |
59048 KB |
Output is correct |
83 |
Correct |
339 ms |
59000 KB |
Output is correct |
84 |
Correct |
356 ms |
57972 KB |
Output is correct |
85 |
Correct |
367 ms |
57976 KB |
Output is correct |
86 |
Correct |
102 ms |
17192 KB |
Output is correct |
87 |
Correct |
1 ms |
340 KB |
Output is correct |