#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pl;
const int N = 1'000'000;
ll n, a, b, l[N + 10], r[N + 10];
set<pair<pl, pl>> s;
vector<pair<pl, pl>> vec;
ll gcd(ll a, ll b) {
return b? gcd(b, a % b): a;
}
void readInput() {
cin >> n >> a >> b;
for (int i = 1; i <= n; i++)
cin >> l[i] >> r[i];
a /= gcd(a, b + 1);
}
void addSeg(pl l, pl r) {
//cout << l.first << ' ' << l.second << " | " << r.first << ' ' << r.second << endl;
vec.push_back({l, r});
}
void addS(pl l, pl r) {
while (true) {
auto au = s.lower_bound({l, {0ll, 0ll}});
if (au != s.end() && au -> first <= r) {
r = max(r, au -> second);
s.erase(au);
}
else {
s.insert({l, r});
break;
}
}
}
void addAll() {
sort(vec.begin(), vec.end());
reverse(vec.begin(), vec.end());
for (auto [l, r]: vec)
addS(l, r);
}
pl getP(ll x) {
ll q = x / b;
ll r = x % b;
return {q % a, r};
}
ll getLen(pl x, pl y) {
ll lenX = y.first - x.first;
return lenX * b - x.second + y.second + 1;
}
void calcS() {
pl mxP = make_pair(a - 1, b - 1);
for (int i = 1; i <= n; i++) {
ll len = r[i] - l[i] + 1;
if (len / b >= a) {
s.insert({{0ll, 0ll}, mxP});
return;
}
}
for (int i = 1; i <= n; i++) {
pl f = getP(l[i]);
pl g = getP(r[i]);
if (g < f) {
addSeg({0ll, 0ll}, g);
addSeg(f, mxP);
}
else
addSeg(f, g);
}
addAll();
}
void calcAnswer() {
ll ans = 0;
while (s.size()) {
ans += getLen(s.begin() -> first, s.begin() -> second);
s.erase(s.begin());
}
cout << ans << flush;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
readInput();
calcS();
calcAnswer();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
5 ms |
4044 KB |
Output is correct |
3 |
Correct |
5 ms |
3940 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2524 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
5 ms |
3932 KB |
Output is correct |
17 |
Correct |
57 ms |
21452 KB |
Output is correct |
18 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2564 KB |
Output is correct |
3 |
Correct |
1 ms |
2560 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
241 ms |
69240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
668 ms |
155256 KB |
Output is correct |
3 |
Correct |
530 ms |
153672 KB |
Output is correct |
4 |
Correct |
585 ms |
154468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
668 ms |
155256 KB |
Output is correct |
3 |
Correct |
530 ms |
153672 KB |
Output is correct |
4 |
Correct |
585 ms |
154468 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
547 ms |
154128 KB |
Output is correct |
7 |
Correct |
536 ms |
153652 KB |
Output is correct |
8 |
Correct |
526 ms |
155012 KB |
Output is correct |
9 |
Correct |
548 ms |
154788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
668 ms |
155256 KB |
Output is correct |
3 |
Correct |
530 ms |
153672 KB |
Output is correct |
4 |
Correct |
585 ms |
154468 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
50 ms |
21256 KB |
Output is correct |
7 |
Correct |
47 ms |
20936 KB |
Output is correct |
8 |
Correct |
54 ms |
20692 KB |
Output is correct |
9 |
Correct |
55 ms |
20884 KB |
Output is correct |
10 |
Correct |
73 ms |
20672 KB |
Output is correct |
11 |
Correct |
47 ms |
20680 KB |
Output is correct |
12 |
Correct |
71 ms |
21036 KB |
Output is correct |
13 |
Correct |
48 ms |
20668 KB |
Output is correct |
14 |
Correct |
47 ms |
21188 KB |
Output is correct |
15 |
Correct |
49 ms |
21132 KB |
Output is correct |
16 |
Correct |
75 ms |
21160 KB |
Output is correct |
17 |
Correct |
54 ms |
20948 KB |
Output is correct |
18 |
Correct |
552 ms |
153696 KB |
Output is correct |
19 |
Correct |
502 ms |
154672 KB |
Output is correct |
20 |
Correct |
564 ms |
154424 KB |
Output is correct |
21 |
Correct |
49 ms |
21084 KB |
Output is correct |
22 |
Correct |
70 ms |
20740 KB |
Output is correct |
23 |
Correct |
93 ms |
38436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
53 ms |
20596 KB |
Output is correct |
3 |
Correct |
73 ms |
21212 KB |
Output is correct |
4 |
Correct |
607 ms |
155132 KB |
Output is correct |
5 |
Correct |
54 ms |
21352 KB |
Output is correct |
6 |
Correct |
60 ms |
20928 KB |
Output is correct |
7 |
Correct |
56 ms |
20716 KB |
Output is correct |
8 |
Correct |
59 ms |
21688 KB |
Output is correct |
9 |
Correct |
64 ms |
20652 KB |
Output is correct |
10 |
Correct |
53 ms |
20928 KB |
Output is correct |
11 |
Correct |
66 ms |
21264 KB |
Output is correct |
12 |
Correct |
50 ms |
21308 KB |
Output is correct |
13 |
Correct |
59 ms |
20656 KB |
Output is correct |
14 |
Correct |
685 ms |
154648 KB |
Output is correct |
15 |
Correct |
59 ms |
19860 KB |
Output is correct |
16 |
Correct |
555 ms |
155284 KB |
Output is correct |
17 |
Correct |
541 ms |
153944 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
5 ms |
4044 KB |
Output is correct |
3 |
Correct |
5 ms |
3940 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2524 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
5 ms |
3932 KB |
Output is correct |
17 |
Correct |
57 ms |
21452 KB |
Output is correct |
18 |
Correct |
1 ms |
2392 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
0 ms |
2396 KB |
Output is correct |
22 |
Correct |
0 ms |
2396 KB |
Output is correct |
23 |
Correct |
0 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2392 KB |
Output is correct |
25 |
Correct |
1 ms |
2564 KB |
Output is correct |
26 |
Correct |
1 ms |
2560 KB |
Output is correct |
27 |
Correct |
1 ms |
2652 KB |
Output is correct |
28 |
Correct |
241 ms |
69240 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
668 ms |
155256 KB |
Output is correct |
31 |
Correct |
530 ms |
153672 KB |
Output is correct |
32 |
Correct |
585 ms |
154468 KB |
Output is correct |
33 |
Correct |
1 ms |
2396 KB |
Output is correct |
34 |
Correct |
547 ms |
154128 KB |
Output is correct |
35 |
Correct |
536 ms |
153652 KB |
Output is correct |
36 |
Correct |
526 ms |
155012 KB |
Output is correct |
37 |
Correct |
548 ms |
154788 KB |
Output is correct |
38 |
Correct |
0 ms |
2396 KB |
Output is correct |
39 |
Correct |
50 ms |
21256 KB |
Output is correct |
40 |
Correct |
47 ms |
20936 KB |
Output is correct |
41 |
Correct |
54 ms |
20692 KB |
Output is correct |
42 |
Correct |
55 ms |
20884 KB |
Output is correct |
43 |
Correct |
73 ms |
20672 KB |
Output is correct |
44 |
Correct |
47 ms |
20680 KB |
Output is correct |
45 |
Correct |
71 ms |
21036 KB |
Output is correct |
46 |
Correct |
48 ms |
20668 KB |
Output is correct |
47 |
Correct |
47 ms |
21188 KB |
Output is correct |
48 |
Correct |
49 ms |
21132 KB |
Output is correct |
49 |
Correct |
75 ms |
21160 KB |
Output is correct |
50 |
Correct |
54 ms |
20948 KB |
Output is correct |
51 |
Correct |
552 ms |
153696 KB |
Output is correct |
52 |
Correct |
502 ms |
154672 KB |
Output is correct |
53 |
Correct |
564 ms |
154424 KB |
Output is correct |
54 |
Correct |
49 ms |
21084 KB |
Output is correct |
55 |
Correct |
70 ms |
20740 KB |
Output is correct |
56 |
Correct |
93 ms |
38436 KB |
Output is correct |
57 |
Correct |
0 ms |
2396 KB |
Output is correct |
58 |
Correct |
53 ms |
20596 KB |
Output is correct |
59 |
Correct |
73 ms |
21212 KB |
Output is correct |
60 |
Correct |
607 ms |
155132 KB |
Output is correct |
61 |
Correct |
54 ms |
21352 KB |
Output is correct |
62 |
Correct |
60 ms |
20928 KB |
Output is correct |
63 |
Correct |
56 ms |
20716 KB |
Output is correct |
64 |
Correct |
59 ms |
21688 KB |
Output is correct |
65 |
Correct |
64 ms |
20652 KB |
Output is correct |
66 |
Correct |
53 ms |
20928 KB |
Output is correct |
67 |
Correct |
66 ms |
21264 KB |
Output is correct |
68 |
Correct |
50 ms |
21308 KB |
Output is correct |
69 |
Correct |
59 ms |
20656 KB |
Output is correct |
70 |
Correct |
685 ms |
154648 KB |
Output is correct |
71 |
Correct |
59 ms |
19860 KB |
Output is correct |
72 |
Correct |
555 ms |
155284 KB |
Output is correct |
73 |
Correct |
541 ms |
153944 KB |
Output is correct |
74 |
Correct |
1 ms |
2396 KB |
Output is correct |
75 |
Correct |
0 ms |
2396 KB |
Output is correct |
76 |
Correct |
1 ms |
2396 KB |
Output is correct |
77 |
Correct |
0 ms |
2396 KB |
Output is correct |
78 |
Correct |
1 ms |
2396 KB |
Output is correct |
79 |
Correct |
6 ms |
4060 KB |
Output is correct |
80 |
Correct |
631 ms |
162940 KB |
Output is correct |
81 |
Correct |
597 ms |
163528 KB |
Output is correct |
82 |
Correct |
536 ms |
163612 KB |
Output is correct |
83 |
Correct |
580 ms |
164112 KB |
Output is correct |
84 |
Correct |
577 ms |
162976 KB |
Output is correct |
85 |
Correct |
591 ms |
163488 KB |
Output is correct |
86 |
Correct |
94 ms |
40132 KB |
Output is correct |
87 |
Correct |
1 ms |
2396 KB |
Output is correct |