#include <bits/stdc++.h>
using namespace std;
#define all(a) (a).begin(), (a).end()
#define add push_back
#define pll pair<long long, long long>
#define ll long long
#define fr first
#define sc second
const int N = 1e6 + 5;
ll n, a, b;
ll k, h;
pll rng[N];
vector<pll> v;
void add_range(ll x, ll y){
if(x <= y){
v.add({x, 0});
v.add({y, 1});
}
else {
v.add({x, 0});
v.add({h - 1, 1});
v.add({0, 0});
v.add({y, 1});
}
}
void solve(){
cin >> n >> a >> b;
ll cnt = 0;
for(int i = 0; i < n; i++){
cin >> rng[i].fr >> rng[i].sc;
cnt += rng[i].sc - rng[i].fr + 1;
}
k = a / __gcd(a, b + 1);
ll maxk = 2'000'000'000'000'000'000ll / b;
if(k >= maxk){
cout << cnt << "\n";
return;
}
h = k * b;
for(int i = 0; i < n; i++){
if((rng[i].sc - rng[i].fr + 1) / h > 0){
cout << h << "\n";
return;
}
add_range(rng[i].fr % h, rng[i].sc % h);
}
sort(all(v));
ll ans = 0, opnd = 0, last = -1;
for(auto& [tiv, id] : v){
if(opnd) ans += tiv - last;
else ans++;
last = tiv;
if(id) opnd--;
else opnd++;
}
cout << ans << "\n";
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
6 ms |
1484 KB |
Output is correct |
3 |
Correct |
6 ms |
1496 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 |
320 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 |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
328 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
5 ms |
1496 KB |
Output is correct |
17 |
Correct |
60 ms |
9824 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
344 ms |
51432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
519 ms |
54316 KB |
Output is correct |
3 |
Correct |
557 ms |
52056 KB |
Output is correct |
4 |
Correct |
507 ms |
63584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
519 ms |
54316 KB |
Output is correct |
3 |
Correct |
557 ms |
52056 KB |
Output is correct |
4 |
Correct |
507 ms |
63584 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
528 ms |
63672 KB |
Output is correct |
7 |
Correct |
517 ms |
63600 KB |
Output is correct |
8 |
Correct |
499 ms |
63672 KB |
Output is correct |
9 |
Correct |
585 ms |
63604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
519 ms |
54316 KB |
Output is correct |
3 |
Correct |
557 ms |
52056 KB |
Output is correct |
4 |
Correct |
507 ms |
63584 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
51 ms |
9796 KB |
Output is correct |
7 |
Correct |
52 ms |
9752 KB |
Output is correct |
8 |
Correct |
49 ms |
9796 KB |
Output is correct |
9 |
Correct |
51 ms |
9840 KB |
Output is correct |
10 |
Correct |
47 ms |
9796 KB |
Output is correct |
11 |
Correct |
51 ms |
9828 KB |
Output is correct |
12 |
Correct |
45 ms |
9864 KB |
Output is correct |
13 |
Correct |
58 ms |
9832 KB |
Output is correct |
14 |
Correct |
53 ms |
9824 KB |
Output is correct |
15 |
Correct |
58 ms |
9792 KB |
Output is correct |
16 |
Correct |
55 ms |
9836 KB |
Output is correct |
17 |
Correct |
48 ms |
9796 KB |
Output is correct |
18 |
Correct |
523 ms |
63636 KB |
Output is correct |
19 |
Correct |
484 ms |
63580 KB |
Output is correct |
20 |
Correct |
603 ms |
63584 KB |
Output is correct |
21 |
Correct |
71 ms |
9804 KB |
Output is correct |
22 |
Correct |
48 ms |
9812 KB |
Output is correct |
23 |
Correct |
142 ms |
34752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
53 ms |
9720 KB |
Output is correct |
3 |
Correct |
64 ms |
9924 KB |
Output is correct |
4 |
Correct |
643 ms |
61844 KB |
Output is correct |
5 |
Correct |
52 ms |
9796 KB |
Output is correct |
6 |
Correct |
54 ms |
9776 KB |
Output is correct |
7 |
Correct |
52 ms |
9876 KB |
Output is correct |
8 |
Correct |
63 ms |
9784 KB |
Output is correct |
9 |
Correct |
53 ms |
9784 KB |
Output is correct |
10 |
Correct |
52 ms |
9812 KB |
Output is correct |
11 |
Correct |
53 ms |
9800 KB |
Output is correct |
12 |
Correct |
48 ms |
9796 KB |
Output is correct |
13 |
Correct |
52 ms |
9812 KB |
Output is correct |
14 |
Correct |
604 ms |
61948 KB |
Output is correct |
15 |
Correct |
67 ms |
9728 KB |
Output is correct |
16 |
Correct |
531 ms |
63560 KB |
Output is correct |
17 |
Correct |
510 ms |
63548 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
6 ms |
1484 KB |
Output is correct |
3 |
Correct |
6 ms |
1496 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 |
320 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 |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
328 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
5 ms |
1496 KB |
Output is correct |
17 |
Correct |
60 ms |
9824 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
344 ms |
51432 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
519 ms |
54316 KB |
Output is correct |
31 |
Correct |
557 ms |
52056 KB |
Output is correct |
32 |
Correct |
507 ms |
63584 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
528 ms |
63672 KB |
Output is correct |
35 |
Correct |
517 ms |
63600 KB |
Output is correct |
36 |
Correct |
499 ms |
63672 KB |
Output is correct |
37 |
Correct |
585 ms |
63604 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
51 ms |
9796 KB |
Output is correct |
40 |
Correct |
52 ms |
9752 KB |
Output is correct |
41 |
Correct |
49 ms |
9796 KB |
Output is correct |
42 |
Correct |
51 ms |
9840 KB |
Output is correct |
43 |
Correct |
47 ms |
9796 KB |
Output is correct |
44 |
Correct |
51 ms |
9828 KB |
Output is correct |
45 |
Correct |
45 ms |
9864 KB |
Output is correct |
46 |
Correct |
58 ms |
9832 KB |
Output is correct |
47 |
Correct |
53 ms |
9824 KB |
Output is correct |
48 |
Correct |
58 ms |
9792 KB |
Output is correct |
49 |
Correct |
55 ms |
9836 KB |
Output is correct |
50 |
Correct |
48 ms |
9796 KB |
Output is correct |
51 |
Correct |
523 ms |
63636 KB |
Output is correct |
52 |
Correct |
484 ms |
63580 KB |
Output is correct |
53 |
Correct |
603 ms |
63584 KB |
Output is correct |
54 |
Correct |
71 ms |
9804 KB |
Output is correct |
55 |
Correct |
48 ms |
9812 KB |
Output is correct |
56 |
Correct |
142 ms |
34752 KB |
Output is correct |
57 |
Correct |
1 ms |
212 KB |
Output is correct |
58 |
Correct |
53 ms |
9720 KB |
Output is correct |
59 |
Correct |
64 ms |
9924 KB |
Output is correct |
60 |
Correct |
643 ms |
61844 KB |
Output is correct |
61 |
Correct |
52 ms |
9796 KB |
Output is correct |
62 |
Correct |
54 ms |
9776 KB |
Output is correct |
63 |
Correct |
52 ms |
9876 KB |
Output is correct |
64 |
Correct |
63 ms |
9784 KB |
Output is correct |
65 |
Correct |
53 ms |
9784 KB |
Output is correct |
66 |
Correct |
52 ms |
9812 KB |
Output is correct |
67 |
Correct |
53 ms |
9800 KB |
Output is correct |
68 |
Correct |
48 ms |
9796 KB |
Output is correct |
69 |
Correct |
52 ms |
9812 KB |
Output is correct |
70 |
Correct |
604 ms |
61948 KB |
Output is correct |
71 |
Correct |
67 ms |
9728 KB |
Output is correct |
72 |
Correct |
531 ms |
63560 KB |
Output is correct |
73 |
Correct |
510 ms |
63548 KB |
Output is correct |
74 |
Correct |
1 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 |
332 KB |
Output is correct |
79 |
Correct |
7 ms |
1496 KB |
Output is correct |
80 |
Correct |
611 ms |
63704 KB |
Output is correct |
81 |
Correct |
627 ms |
63800 KB |
Output is correct |
82 |
Correct |
593 ms |
63732 KB |
Output is correct |
83 |
Correct |
568 ms |
63808 KB |
Output is correct |
84 |
Correct |
620 ms |
63672 KB |
Output is correct |
85 |
Correct |
550 ms |
63660 KB |
Output is correct |
86 |
Correct |
137 ms |
34664 KB |
Output is correct |
87 |
Correct |
0 ms |
332 KB |
Output is correct |