#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fi first
#define se second
#define ll long long
#define ld long double
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mpp make_pair
#define ve vector
using namespace std;
using namespace __gnu_pbds;
template<class T> using oset = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
const ll inf = 4e18; const int iinf = 1e9;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T> inline bool chmin(T& a, T b) { return (a > b ? a = b, 1 : 0); }
template <typename T> inline bool chmax(T& a, T b) { return (a < b ? a = b, 1 : 0); }
inline void solve() {
ll n, a, b;
cin >> n >> a >> b;
ll T;
ll g = gcd(a, b + 1);
ll f = a / g;
if (f > inf / b) T = inf;
else T = f * b;
ve<pll> ev;
for (int i = 0; i < n; ++i) {
ll l, r;
cin >> l >> r;
if (r - l + 1 >= T) {
return void(cout << T);
}
l %= T, r %= T;
if (l <= r) {
ev.pb({l, +1});
ev.pb({r + 1, -1});
}
else {
ev.pb({0, +1});
ev.pb({r + 1, -1});
ev.pb({l, +1});
ev.pb({T, -1});
}
}
sort(all(ev));
ll ans = 0, sum = 0;
for (int i = 0; i < sz(ev); ) {
int j = i;
ll prev = (!i ? 0 : ev[i - 1].fi);
if (sum > 0) ans += (ev[i].fi - prev);
while (j < sz(ev) && ev[j].fi == ev[i].fi) {
sum += ev[j].se;
++j;
}
i = j;
}
cout << ans;
}
signed main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int q = 1; // cin >> q;
while (q--) solve();
cerr << fixed << setprecision(3) << "Time execution: " << (double)clock() / CLOCKS_PER_SEC << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
6 ms |
1104 KB |
Output is correct |
3 |
Correct |
6 ms |
1104 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
1 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 |
0 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 |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
6 ms |
1064 KB |
Output is correct |
17 |
Correct |
56 ms |
4624 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 |
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 |
1 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 |
380 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
380 KB |
Output is correct |
5 |
Correct |
327 ms |
33492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
514 ms |
33488 KB |
Output is correct |
3 |
Correct |
500 ms |
33300 KB |
Output is correct |
4 |
Correct |
476 ms |
33432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
514 ms |
33488 KB |
Output is correct |
3 |
Correct |
500 ms |
33300 KB |
Output is correct |
4 |
Correct |
476 ms |
33432 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
488 ms |
33464 KB |
Output is correct |
7 |
Correct |
472 ms |
33432 KB |
Output is correct |
8 |
Correct |
494 ms |
33500 KB |
Output is correct |
9 |
Correct |
553 ms |
33472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
514 ms |
33488 KB |
Output is correct |
3 |
Correct |
500 ms |
33300 KB |
Output is correct |
4 |
Correct |
476 ms |
33432 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
49 ms |
4716 KB |
Output is correct |
7 |
Correct |
52 ms |
4812 KB |
Output is correct |
8 |
Correct |
45 ms |
4792 KB |
Output is correct |
9 |
Correct |
47 ms |
4824 KB |
Output is correct |
10 |
Correct |
45 ms |
4804 KB |
Output is correct |
11 |
Correct |
48 ms |
4712 KB |
Output is correct |
12 |
Correct |
46 ms |
4828 KB |
Output is correct |
13 |
Correct |
50 ms |
4812 KB |
Output is correct |
14 |
Correct |
46 ms |
4812 KB |
Output is correct |
15 |
Correct |
56 ms |
4748 KB |
Output is correct |
16 |
Correct |
53 ms |
4700 KB |
Output is correct |
17 |
Correct |
46 ms |
4756 KB |
Output is correct |
18 |
Correct |
594 ms |
33520 KB |
Output is correct |
19 |
Correct |
470 ms |
33448 KB |
Output is correct |
20 |
Correct |
565 ms |
33508 KB |
Output is correct |
21 |
Correct |
61 ms |
4724 KB |
Output is correct |
22 |
Correct |
43 ms |
4800 KB |
Output is correct |
23 |
Correct |
137 ms |
17092 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
52 ms |
4624 KB |
Output is correct |
3 |
Correct |
53 ms |
4608 KB |
Output is correct |
4 |
Correct |
595 ms |
33628 KB |
Output is correct |
5 |
Correct |
51 ms |
4824 KB |
Output is correct |
6 |
Correct |
54 ms |
4804 KB |
Output is correct |
7 |
Correct |
51 ms |
4884 KB |
Output is correct |
8 |
Correct |
56 ms |
4832 KB |
Output is correct |
9 |
Correct |
50 ms |
4708 KB |
Output is correct |
10 |
Correct |
56 ms |
4764 KB |
Output is correct |
11 |
Correct |
55 ms |
4804 KB |
Output is correct |
12 |
Correct |
45 ms |
4740 KB |
Output is correct |
13 |
Correct |
54 ms |
4672 KB |
Output is correct |
14 |
Correct |
556 ms |
33512 KB |
Output is correct |
15 |
Correct |
55 ms |
4728 KB |
Output is correct |
16 |
Correct |
454 ms |
33208 KB |
Output is correct |
17 |
Correct |
478 ms |
33396 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
6 ms |
1104 KB |
Output is correct |
3 |
Correct |
6 ms |
1104 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
1 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 |
0 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 |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
6 ms |
1064 KB |
Output is correct |
17 |
Correct |
56 ms |
4624 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
324 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 |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
380 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
380 KB |
Output is correct |
28 |
Correct |
327 ms |
33492 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
514 ms |
33488 KB |
Output is correct |
31 |
Correct |
500 ms |
33300 KB |
Output is correct |
32 |
Correct |
476 ms |
33432 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
488 ms |
33464 KB |
Output is correct |
35 |
Correct |
472 ms |
33432 KB |
Output is correct |
36 |
Correct |
494 ms |
33500 KB |
Output is correct |
37 |
Correct |
553 ms |
33472 KB |
Output is correct |
38 |
Correct |
1 ms |
256 KB |
Output is correct |
39 |
Correct |
49 ms |
4716 KB |
Output is correct |
40 |
Correct |
52 ms |
4812 KB |
Output is correct |
41 |
Correct |
45 ms |
4792 KB |
Output is correct |
42 |
Correct |
47 ms |
4824 KB |
Output is correct |
43 |
Correct |
45 ms |
4804 KB |
Output is correct |
44 |
Correct |
48 ms |
4712 KB |
Output is correct |
45 |
Correct |
46 ms |
4828 KB |
Output is correct |
46 |
Correct |
50 ms |
4812 KB |
Output is correct |
47 |
Correct |
46 ms |
4812 KB |
Output is correct |
48 |
Correct |
56 ms |
4748 KB |
Output is correct |
49 |
Correct |
53 ms |
4700 KB |
Output is correct |
50 |
Correct |
46 ms |
4756 KB |
Output is correct |
51 |
Correct |
594 ms |
33520 KB |
Output is correct |
52 |
Correct |
470 ms |
33448 KB |
Output is correct |
53 |
Correct |
565 ms |
33508 KB |
Output is correct |
54 |
Correct |
61 ms |
4724 KB |
Output is correct |
55 |
Correct |
43 ms |
4800 KB |
Output is correct |
56 |
Correct |
137 ms |
17092 KB |
Output is correct |
57 |
Correct |
1 ms |
320 KB |
Output is correct |
58 |
Correct |
52 ms |
4624 KB |
Output is correct |
59 |
Correct |
53 ms |
4608 KB |
Output is correct |
60 |
Correct |
595 ms |
33628 KB |
Output is correct |
61 |
Correct |
51 ms |
4824 KB |
Output is correct |
62 |
Correct |
54 ms |
4804 KB |
Output is correct |
63 |
Correct |
51 ms |
4884 KB |
Output is correct |
64 |
Correct |
56 ms |
4832 KB |
Output is correct |
65 |
Correct |
50 ms |
4708 KB |
Output is correct |
66 |
Correct |
56 ms |
4764 KB |
Output is correct |
67 |
Correct |
55 ms |
4804 KB |
Output is correct |
68 |
Correct |
45 ms |
4740 KB |
Output is correct |
69 |
Correct |
54 ms |
4672 KB |
Output is correct |
70 |
Correct |
556 ms |
33512 KB |
Output is correct |
71 |
Correct |
55 ms |
4728 KB |
Output is correct |
72 |
Correct |
454 ms |
33208 KB |
Output is correct |
73 |
Correct |
478 ms |
33396 KB |
Output is correct |
74 |
Correct |
0 ms |
212 KB |
Output is correct |
75 |
Correct |
1 ms |
212 KB |
Output is correct |
76 |
Correct |
0 ms |
212 KB |
Output is correct |
77 |
Correct |
1 ms |
212 KB |
Output is correct |
78 |
Correct |
1 ms |
320 KB |
Output is correct |
79 |
Correct |
5 ms |
1348 KB |
Output is correct |
80 |
Correct |
681 ms |
48088 KB |
Output is correct |
81 |
Correct |
600 ms |
47956 KB |
Output is correct |
82 |
Correct |
573 ms |
44108 KB |
Output is correct |
83 |
Correct |
552 ms |
44068 KB |
Output is correct |
84 |
Correct |
581 ms |
51652 KB |
Output is correct |
85 |
Correct |
569 ms |
48964 KB |
Output is correct |
86 |
Correct |
134 ms |
26708 KB |
Output is correct |
87 |
Correct |
0 ms |
212 KB |
Output is correct |