#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
using ull = unsigned long long;
using ll = long long;
using namespace std;
constexpr ll INF = 3e18;
int main() {
#ifdef HOME
ifstream cin("input.in");
ofstream cout("output.out");
#endif
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int n;
ll a, b;
cin >> n >> a >> b;
ll period;
if (INF / b < a / __gcd(a, b + 1)) {
period = INF;
} else {
period = (a / __gcd(a, b + 1)) * b;
}
multiset<pair<ll, ll>> mst;
for (int i = 0; i < n; i++) {
ll l, r;
cin >> l >> r;
if (r - l + 1 >= period) {
cout << period << "\n";
return 0;
}
auto Add = [&](ll l, ll r) {
ll new_l = l, new_r = r;
vector<pair<ll, ll>> temp;
auto itr = mst.insert({l, r});
auto prv = itr;
while (prv != mst.begin() && prv->second >= l) {
new_l = min(new_l, prv->first);
new_r = max(new_r, prv->second);
temp.push_back(*prv);
prv = prev(prv);
}
if (prv->second >= l) {
new_l = min(new_l, prv->first);
new_r = max(new_r, prv->second);
temp.push_back(*prv);
temp.push_back(*prv);
}
auto nxt = itr;
while (nxt != mst.end() && nxt->first <= r) {
new_l = min(new_l, nxt->first);
new_r = max(new_r, nxt->second);
temp.push_back(*nxt);
nxt = next(nxt);
}
for (auto s : temp) {
mst.erase(s);
}
mst.insert({new_l, new_r});
};
l %= period;
r %= period;
if (l > r) {
Add(l, period - 1);
Add(0, r);
} else {
Add(l, r);
}
}
ll answer = 0;
for (auto itr : mst) {
answer += itr.second - itr.first + 1;
}
cout << answer;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
6 ms |
860 KB |
Output is correct |
3 |
Correct |
6 ms |
1372 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
5 ms |
1240 KB |
Output is correct |
17 |
Correct |
70 ms |
10292 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
312 ms |
25640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
717 ms |
62880 KB |
Output is correct |
3 |
Correct |
805 ms |
62992 KB |
Output is correct |
4 |
Correct |
816 ms |
63196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
717 ms |
62880 KB |
Output is correct |
3 |
Correct |
805 ms |
62992 KB |
Output is correct |
4 |
Correct |
816 ms |
63196 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
744 ms |
63008 KB |
Output is correct |
7 |
Correct |
757 ms |
63264 KB |
Output is correct |
8 |
Correct |
723 ms |
63156 KB |
Output is correct |
9 |
Correct |
910 ms |
63060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
717 ms |
62880 KB |
Output is correct |
3 |
Correct |
805 ms |
62992 KB |
Output is correct |
4 |
Correct |
816 ms |
63196 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
55 ms |
6480 KB |
Output is correct |
7 |
Correct |
55 ms |
6628 KB |
Output is correct |
8 |
Correct |
58 ms |
6484 KB |
Output is correct |
9 |
Correct |
72 ms |
6604 KB |
Output is correct |
10 |
Correct |
73 ms |
6480 KB |
Output is correct |
11 |
Correct |
56 ms |
6532 KB |
Output is correct |
12 |
Correct |
60 ms |
6480 KB |
Output is correct |
13 |
Correct |
71 ms |
6480 KB |
Output is correct |
14 |
Correct |
57 ms |
6484 KB |
Output is correct |
15 |
Correct |
90 ms |
6588 KB |
Output is correct |
16 |
Correct |
76 ms |
6480 KB |
Output is correct |
17 |
Correct |
68 ms |
10200 KB |
Output is correct |
18 |
Correct |
715 ms |
100140 KB |
Output is correct |
19 |
Correct |
860 ms |
100316 KB |
Output is correct |
20 |
Correct |
990 ms |
100196 KB |
Output is correct |
21 |
Correct |
65 ms |
10344 KB |
Output is correct |
22 |
Correct |
63 ms |
10188 KB |
Output is correct |
23 |
Correct |
124 ms |
12972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
77 ms |
6576 KB |
Output is correct |
3 |
Correct |
80 ms |
6496 KB |
Output is correct |
4 |
Correct |
906 ms |
63048 KB |
Output is correct |
5 |
Correct |
78 ms |
6612 KB |
Output is correct |
6 |
Correct |
77 ms |
6872 KB |
Output is correct |
7 |
Correct |
77 ms |
6556 KB |
Output is correct |
8 |
Correct |
76 ms |
6576 KB |
Output is correct |
9 |
Correct |
72 ms |
6608 KB |
Output is correct |
10 |
Correct |
60 ms |
6452 KB |
Output is correct |
11 |
Correct |
72 ms |
6508 KB |
Output is correct |
12 |
Correct |
62 ms |
6448 KB |
Output is correct |
13 |
Correct |
74 ms |
6480 KB |
Output is correct |
14 |
Correct |
1105 ms |
63168 KB |
Output is correct |
15 |
Correct |
56 ms |
5972 KB |
Output is correct |
16 |
Correct |
718 ms |
100380 KB |
Output is correct |
17 |
Correct |
703 ms |
100376 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
6 ms |
860 KB |
Output is correct |
3 |
Correct |
6 ms |
1372 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
5 ms |
1240 KB |
Output is correct |
17 |
Correct |
70 ms |
10292 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
344 KB |
Output is correct |
24 |
Correct |
0 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 |
312 ms |
25640 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
717 ms |
62880 KB |
Output is correct |
31 |
Correct |
805 ms |
62992 KB |
Output is correct |
32 |
Correct |
816 ms |
63196 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
744 ms |
63008 KB |
Output is correct |
35 |
Correct |
757 ms |
63264 KB |
Output is correct |
36 |
Correct |
723 ms |
63156 KB |
Output is correct |
37 |
Correct |
910 ms |
63060 KB |
Output is correct |
38 |
Correct |
0 ms |
344 KB |
Output is correct |
39 |
Correct |
55 ms |
6480 KB |
Output is correct |
40 |
Correct |
55 ms |
6628 KB |
Output is correct |
41 |
Correct |
58 ms |
6484 KB |
Output is correct |
42 |
Correct |
72 ms |
6604 KB |
Output is correct |
43 |
Correct |
73 ms |
6480 KB |
Output is correct |
44 |
Correct |
56 ms |
6532 KB |
Output is correct |
45 |
Correct |
60 ms |
6480 KB |
Output is correct |
46 |
Correct |
71 ms |
6480 KB |
Output is correct |
47 |
Correct |
57 ms |
6484 KB |
Output is correct |
48 |
Correct |
90 ms |
6588 KB |
Output is correct |
49 |
Correct |
76 ms |
6480 KB |
Output is correct |
50 |
Correct |
68 ms |
10200 KB |
Output is correct |
51 |
Correct |
715 ms |
100140 KB |
Output is correct |
52 |
Correct |
860 ms |
100316 KB |
Output is correct |
53 |
Correct |
990 ms |
100196 KB |
Output is correct |
54 |
Correct |
65 ms |
10344 KB |
Output is correct |
55 |
Correct |
63 ms |
10188 KB |
Output is correct |
56 |
Correct |
124 ms |
12972 KB |
Output is correct |
57 |
Correct |
1 ms |
348 KB |
Output is correct |
58 |
Correct |
77 ms |
6576 KB |
Output is correct |
59 |
Correct |
80 ms |
6496 KB |
Output is correct |
60 |
Correct |
906 ms |
63048 KB |
Output is correct |
61 |
Correct |
78 ms |
6612 KB |
Output is correct |
62 |
Correct |
77 ms |
6872 KB |
Output is correct |
63 |
Correct |
77 ms |
6556 KB |
Output is correct |
64 |
Correct |
76 ms |
6576 KB |
Output is correct |
65 |
Correct |
72 ms |
6608 KB |
Output is correct |
66 |
Correct |
60 ms |
6452 KB |
Output is correct |
67 |
Correct |
72 ms |
6508 KB |
Output is correct |
68 |
Correct |
62 ms |
6448 KB |
Output is correct |
69 |
Correct |
74 ms |
6480 KB |
Output is correct |
70 |
Correct |
1105 ms |
63168 KB |
Output is correct |
71 |
Correct |
56 ms |
5972 KB |
Output is correct |
72 |
Correct |
718 ms |
100380 KB |
Output is correct |
73 |
Correct |
703 ms |
100376 KB |
Output is correct |
74 |
Correct |
0 ms |
348 KB |
Output is correct |
75 |
Correct |
0 ms |
348 KB |
Output is correct |
76 |
Correct |
0 ms |
448 KB |
Output is correct |
77 |
Correct |
0 ms |
348 KB |
Output is correct |
78 |
Correct |
0 ms |
348 KB |
Output is correct |
79 |
Correct |
6 ms |
1372 KB |
Output is correct |
80 |
Correct |
1077 ms |
99952 KB |
Output is correct |
81 |
Correct |
920 ms |
100452 KB |
Output is correct |
82 |
Correct |
1197 ms |
100108 KB |
Output is correct |
83 |
Correct |
1167 ms |
100128 KB |
Output is correct |
84 |
Correct |
1296 ms |
100440 KB |
Output is correct |
85 |
Correct |
978 ms |
99892 KB |
Output is correct |
86 |
Correct |
130 ms |
12880 KB |
Output is correct |
87 |
Correct |
0 ms |
348 KB |
Output is correct |