#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
using ll = long long;
using int128 = __int128_t;
using pll = pair<ll, ll>;
template <class T>
using vec = vector<T>;
template <class T>
using indexed_set =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define FOR(i, s, e) for (ll i = (ll)s; i < (ll)e; i++)
#define CFOR(i, s, e) for (ll i = (ll)s; i <= (ll)e; i++)
#define TRAV(e, a) for (const auto &e : a)
#define all(x) x.begin(), x.end()
#define dbg(x) cerr << "ln" << __LINE__ << ": " << #x << " = " << x << endl;
template <class T, class = decay_t<decltype(*begin(declval<T>()))>,
class = enable_if_t<!is_same<T, string>::value>>
ostream &operator<<(ostream &out, const T &obj) {
out << "[";
for (auto it = obj.begin(); it != obj.end(); it++) {
out << &", "[2 * (it == obj.begin())] << *it;
}
return out << "]";
}
template <class K, class V>
ostream &operator<<(ostream &out, const pair<K, V> &obj) {
return out << "(" << obj.first << ", " << obj.second << ")";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
ll n, a, b;
cin >> n >> a >> b;
ll sz = min((int128)a * b / gcd(a, b + 1), (int128)1e18 + 5);
set<pll> ss;
auto insert = [&](pll p) {
auto it = ss.lower_bound(p);
while (it != ss.end() && it->first <= p.second) {
p.second = max(p.second, it->second);
it = ss.erase(it);
}
if (it != ss.begin() && p.first <= prev(it)->second) {
p.first = min(p.first, prev(it)->first);
p.second = max(p.second, prev(it)->second);
ss.erase(prev(it));
}
ss.insert(p);
};
FOR(i, 0, n) {
ll l, r;
cin >> l >> r;
if (r - l + 1 >= sz) {
insert({0, sz - 1});
} else {
l %= sz;
r %= sz;
if (l <= r) {
insert({l, r});
} else {
insert({0, r});
insert({l, sz - 1});
}
}
}
ll ans = 0;
TRAV(e, ss) ans += e.second - e.first + 1;
cout << ans << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
7 ms |
964 KB |
Output is correct |
3 |
Correct |
5 ms |
860 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
5 ms |
860 KB |
Output is correct |
17 |
Correct |
47 ms |
6592 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
228 ms |
25544 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
524 ms |
63020 KB |
Output is correct |
3 |
Correct |
545 ms |
100272 KB |
Output is correct |
4 |
Correct |
607 ms |
100180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
524 ms |
63020 KB |
Output is correct |
3 |
Correct |
545 ms |
100272 KB |
Output is correct |
4 |
Correct |
607 ms |
100180 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
532 ms |
100072 KB |
Output is correct |
7 |
Correct |
558 ms |
100056 KB |
Output is correct |
8 |
Correct |
561 ms |
100328 KB |
Output is correct |
9 |
Correct |
635 ms |
100180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
524 ms |
63020 KB |
Output is correct |
3 |
Correct |
545 ms |
100272 KB |
Output is correct |
4 |
Correct |
607 ms |
100180 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
40 ms |
10316 KB |
Output is correct |
7 |
Correct |
41 ms |
10324 KB |
Output is correct |
8 |
Correct |
43 ms |
10324 KB |
Output is correct |
9 |
Correct |
50 ms |
10324 KB |
Output is correct |
10 |
Correct |
42 ms |
10324 KB |
Output is correct |
11 |
Correct |
42 ms |
10328 KB |
Output is correct |
12 |
Correct |
60 ms |
10244 KB |
Output is correct |
13 |
Correct |
64 ms |
10228 KB |
Output is correct |
14 |
Correct |
45 ms |
10128 KB |
Output is correct |
15 |
Correct |
57 ms |
10408 KB |
Output is correct |
16 |
Correct |
53 ms |
10412 KB |
Output is correct |
17 |
Correct |
52 ms |
10180 KB |
Output is correct |
18 |
Correct |
519 ms |
100176 KB |
Output is correct |
19 |
Correct |
601 ms |
100192 KB |
Output is correct |
20 |
Correct |
606 ms |
100212 KB |
Output is correct |
21 |
Correct |
45 ms |
10324 KB |
Output is correct |
22 |
Correct |
47 ms |
10320 KB |
Output is correct |
23 |
Correct |
104 ms |
12976 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
54 ms |
10184 KB |
Output is correct |
3 |
Correct |
53 ms |
10328 KB |
Output is correct |
4 |
Correct |
634 ms |
100036 KB |
Output is correct |
5 |
Correct |
51 ms |
10320 KB |
Output is correct |
6 |
Correct |
52 ms |
10320 KB |
Output is correct |
7 |
Correct |
52 ms |
10384 KB |
Output is correct |
8 |
Correct |
54 ms |
10416 KB |
Output is correct |
9 |
Correct |
51 ms |
10324 KB |
Output is correct |
10 |
Correct |
56 ms |
10324 KB |
Output is correct |
11 |
Correct |
48 ms |
10192 KB |
Output is correct |
12 |
Correct |
47 ms |
10324 KB |
Output is correct |
13 |
Correct |
57 ms |
10364 KB |
Output is correct |
14 |
Correct |
614 ms |
100128 KB |
Output is correct |
15 |
Correct |
41 ms |
9552 KB |
Output is correct |
16 |
Correct |
551 ms |
99972 KB |
Output is correct |
17 |
Correct |
544 ms |
100172 KB |
Output is correct |
18 |
Correct |
1 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
7 ms |
964 KB |
Output is correct |
3 |
Correct |
5 ms |
860 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
5 ms |
860 KB |
Output is correct |
17 |
Correct |
47 ms |
6592 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
228 ms |
25544 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
524 ms |
63020 KB |
Output is correct |
31 |
Correct |
545 ms |
100272 KB |
Output is correct |
32 |
Correct |
607 ms |
100180 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
532 ms |
100072 KB |
Output is correct |
35 |
Correct |
558 ms |
100056 KB |
Output is correct |
36 |
Correct |
561 ms |
100328 KB |
Output is correct |
37 |
Correct |
635 ms |
100180 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
40 ms |
10316 KB |
Output is correct |
40 |
Correct |
41 ms |
10324 KB |
Output is correct |
41 |
Correct |
43 ms |
10324 KB |
Output is correct |
42 |
Correct |
50 ms |
10324 KB |
Output is correct |
43 |
Correct |
42 ms |
10324 KB |
Output is correct |
44 |
Correct |
42 ms |
10328 KB |
Output is correct |
45 |
Correct |
60 ms |
10244 KB |
Output is correct |
46 |
Correct |
64 ms |
10228 KB |
Output is correct |
47 |
Correct |
45 ms |
10128 KB |
Output is correct |
48 |
Correct |
57 ms |
10408 KB |
Output is correct |
49 |
Correct |
53 ms |
10412 KB |
Output is correct |
50 |
Correct |
52 ms |
10180 KB |
Output is correct |
51 |
Correct |
519 ms |
100176 KB |
Output is correct |
52 |
Correct |
601 ms |
100192 KB |
Output is correct |
53 |
Correct |
606 ms |
100212 KB |
Output is correct |
54 |
Correct |
45 ms |
10324 KB |
Output is correct |
55 |
Correct |
47 ms |
10320 KB |
Output is correct |
56 |
Correct |
104 ms |
12976 KB |
Output is correct |
57 |
Correct |
0 ms |
348 KB |
Output is correct |
58 |
Correct |
54 ms |
10184 KB |
Output is correct |
59 |
Correct |
53 ms |
10328 KB |
Output is correct |
60 |
Correct |
634 ms |
100036 KB |
Output is correct |
61 |
Correct |
51 ms |
10320 KB |
Output is correct |
62 |
Correct |
52 ms |
10320 KB |
Output is correct |
63 |
Correct |
52 ms |
10384 KB |
Output is correct |
64 |
Correct |
54 ms |
10416 KB |
Output is correct |
65 |
Correct |
51 ms |
10324 KB |
Output is correct |
66 |
Correct |
56 ms |
10324 KB |
Output is correct |
67 |
Correct |
48 ms |
10192 KB |
Output is correct |
68 |
Correct |
47 ms |
10324 KB |
Output is correct |
69 |
Correct |
57 ms |
10364 KB |
Output is correct |
70 |
Correct |
614 ms |
100128 KB |
Output is correct |
71 |
Correct |
41 ms |
9552 KB |
Output is correct |
72 |
Correct |
551 ms |
99972 KB |
Output is correct |
73 |
Correct |
544 ms |
100172 KB |
Output is correct |
74 |
Correct |
1 ms |
600 KB |
Output is correct |
75 |
Correct |
1 ms |
348 KB |
Output is correct |
76 |
Correct |
0 ms |
348 KB |
Output is correct |
77 |
Correct |
1 ms |
348 KB |
Output is correct |
78 |
Correct |
1 ms |
344 KB |
Output is correct |
79 |
Correct |
5 ms |
1372 KB |
Output is correct |
80 |
Correct |
590 ms |
100128 KB |
Output is correct |
81 |
Correct |
556 ms |
99920 KB |
Output is correct |
82 |
Correct |
687 ms |
100420 KB |
Output is correct |
83 |
Correct |
619 ms |
100560 KB |
Output is correct |
84 |
Correct |
620 ms |
100328 KB |
Output is correct |
85 |
Correct |
636 ms |
100156 KB |
Output is correct |
86 |
Correct |
106 ms |
13236 KB |
Output is correct |
87 |
Correct |
1 ms |
344 KB |
Output is correct |