#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define all(x) (x).begin(),(x).end()
#define X first
#define Y second
#define sep ' '
#define endl '\n'
#define SZ(x) ll(x.size())
const ll MAXN = 1e6 + 10;
const ll LOG = 22;
const ll INF = 2e18;
const ll MOD = 1e9 + 7; //998244353; //1e9 + 9;
ll n , A , B , sum , ans , L[MAXN] , R[MAXN];
vector<pll> vec;
int main() {
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin >> n >> A >> B;
A /= __gcd(A , B + 1);
for(int i = 0 ; i < n ; i++){
cin >> L[i] >> R[i];
sum += R[i] - L[i] + 1;
}
if(INF / A <= B) return cout << sum << endl , 0;
A *= B; //cout << A << endl;
for(int i = 0 ; i < n ; i++){
if(R[i] - L[i] + 1 >= A) return cout << A << endl , 0;
L[i] %= A; R[i] %= A;
if(L[i] <= R[i]) vec.push_back({L[i] , R[i]});
else{
vec.push_back({0 , R[i]});
vec.push_back({L[i] , A - 1});
}
}
sort(all(vec));
ll l = 0 , r = -1;
for(pll i : vec){
if(r < i.X){
ans += r - l + 1;
l = INF , r = -1;
}
l = min(l , i.X);
r = max(r , i.Y);
}
cout << ans + r - l + 1 << endl;
return 0;
}
/*
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
6 ms |
1256 KB |
Output is correct |
3 |
Correct |
6 ms |
1228 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
328 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
5 ms |
1256 KB |
Output is correct |
17 |
Correct |
55 ms |
7748 KB |
Output is correct |
18 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
416 ms |
57652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
527 ms |
69668 KB |
Output is correct |
3 |
Correct |
522 ms |
69728 KB |
Output is correct |
4 |
Correct |
498 ms |
69616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
527 ms |
69668 KB |
Output is correct |
3 |
Correct |
522 ms |
69728 KB |
Output is correct |
4 |
Correct |
498 ms |
69616 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
519 ms |
69632 KB |
Output is correct |
7 |
Correct |
516 ms |
69688 KB |
Output is correct |
8 |
Correct |
533 ms |
69676 KB |
Output is correct |
9 |
Correct |
566 ms |
69632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
527 ms |
69668 KB |
Output is correct |
3 |
Correct |
522 ms |
69728 KB |
Output is correct |
4 |
Correct |
498 ms |
69616 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
49 ms |
7716 KB |
Output is correct |
7 |
Correct |
58 ms |
7744 KB |
Output is correct |
8 |
Correct |
50 ms |
7700 KB |
Output is correct |
9 |
Correct |
50 ms |
7724 KB |
Output is correct |
10 |
Correct |
52 ms |
7768 KB |
Output is correct |
11 |
Correct |
54 ms |
7680 KB |
Output is correct |
12 |
Correct |
52 ms |
7784 KB |
Output is correct |
13 |
Correct |
55 ms |
7844 KB |
Output is correct |
14 |
Correct |
49 ms |
7720 KB |
Output is correct |
15 |
Correct |
55 ms |
7800 KB |
Output is correct |
16 |
Correct |
53 ms |
7744 KB |
Output is correct |
17 |
Correct |
50 ms |
7712 KB |
Output is correct |
18 |
Correct |
666 ms |
69760 KB |
Output is correct |
19 |
Correct |
538 ms |
69684 KB |
Output is correct |
20 |
Correct |
582 ms |
69700 KB |
Output is correct |
21 |
Correct |
55 ms |
7764 KB |
Output is correct |
22 |
Correct |
47 ms |
7744 KB |
Output is correct |
23 |
Correct |
156 ms |
26576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
60 ms |
7732 KB |
Output is correct |
3 |
Correct |
56 ms |
7704 KB |
Output is correct |
4 |
Correct |
597 ms |
69572 KB |
Output is correct |
5 |
Correct |
56 ms |
7752 KB |
Output is correct |
6 |
Correct |
54 ms |
7712 KB |
Output is correct |
7 |
Correct |
55 ms |
7784 KB |
Output is correct |
8 |
Correct |
54 ms |
7748 KB |
Output is correct |
9 |
Correct |
53 ms |
7744 KB |
Output is correct |
10 |
Correct |
63 ms |
7860 KB |
Output is correct |
11 |
Correct |
57 ms |
7748 KB |
Output is correct |
12 |
Correct |
48 ms |
7716 KB |
Output is correct |
13 |
Correct |
56 ms |
7712 KB |
Output is correct |
14 |
Correct |
581 ms |
69660 KB |
Output is correct |
15 |
Correct |
68 ms |
7756 KB |
Output is correct |
16 |
Correct |
548 ms |
69568 KB |
Output is correct |
17 |
Correct |
536 ms |
69348 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
6 ms |
1256 KB |
Output is correct |
3 |
Correct |
6 ms |
1228 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
328 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
5 ms |
1256 KB |
Output is correct |
17 |
Correct |
55 ms |
7748 KB |
Output is correct |
18 |
Correct |
0 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
0 ms |
204 KB |
Output is correct |
24 |
Correct |
1 ms |
320 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
416 ms |
57652 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
527 ms |
69668 KB |
Output is correct |
31 |
Correct |
522 ms |
69728 KB |
Output is correct |
32 |
Correct |
498 ms |
69616 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
519 ms |
69632 KB |
Output is correct |
35 |
Correct |
516 ms |
69688 KB |
Output is correct |
36 |
Correct |
533 ms |
69676 KB |
Output is correct |
37 |
Correct |
566 ms |
69632 KB |
Output is correct |
38 |
Correct |
1 ms |
332 KB |
Output is correct |
39 |
Correct |
49 ms |
7716 KB |
Output is correct |
40 |
Correct |
58 ms |
7744 KB |
Output is correct |
41 |
Correct |
50 ms |
7700 KB |
Output is correct |
42 |
Correct |
50 ms |
7724 KB |
Output is correct |
43 |
Correct |
52 ms |
7768 KB |
Output is correct |
44 |
Correct |
54 ms |
7680 KB |
Output is correct |
45 |
Correct |
52 ms |
7784 KB |
Output is correct |
46 |
Correct |
55 ms |
7844 KB |
Output is correct |
47 |
Correct |
49 ms |
7720 KB |
Output is correct |
48 |
Correct |
55 ms |
7800 KB |
Output is correct |
49 |
Correct |
53 ms |
7744 KB |
Output is correct |
50 |
Correct |
50 ms |
7712 KB |
Output is correct |
51 |
Correct |
666 ms |
69760 KB |
Output is correct |
52 |
Correct |
538 ms |
69684 KB |
Output is correct |
53 |
Correct |
582 ms |
69700 KB |
Output is correct |
54 |
Correct |
55 ms |
7764 KB |
Output is correct |
55 |
Correct |
47 ms |
7744 KB |
Output is correct |
56 |
Correct |
156 ms |
26576 KB |
Output is correct |
57 |
Correct |
1 ms |
332 KB |
Output is correct |
58 |
Correct |
60 ms |
7732 KB |
Output is correct |
59 |
Correct |
56 ms |
7704 KB |
Output is correct |
60 |
Correct |
597 ms |
69572 KB |
Output is correct |
61 |
Correct |
56 ms |
7752 KB |
Output is correct |
62 |
Correct |
54 ms |
7712 KB |
Output is correct |
63 |
Correct |
55 ms |
7784 KB |
Output is correct |
64 |
Correct |
54 ms |
7748 KB |
Output is correct |
65 |
Correct |
53 ms |
7744 KB |
Output is correct |
66 |
Correct |
63 ms |
7860 KB |
Output is correct |
67 |
Correct |
57 ms |
7748 KB |
Output is correct |
68 |
Correct |
48 ms |
7716 KB |
Output is correct |
69 |
Correct |
56 ms |
7712 KB |
Output is correct |
70 |
Correct |
581 ms |
69660 KB |
Output is correct |
71 |
Correct |
68 ms |
7756 KB |
Output is correct |
72 |
Correct |
548 ms |
69568 KB |
Output is correct |
73 |
Correct |
536 ms |
69348 KB |
Output is correct |
74 |
Correct |
1 ms |
332 KB |
Output is correct |
75 |
Correct |
1 ms |
332 KB |
Output is correct |
76 |
Correct |
1 ms |
332 KB |
Output is correct |
77 |
Correct |
1 ms |
204 KB |
Output is correct |
78 |
Correct |
1 ms |
332 KB |
Output is correct |
79 |
Correct |
6 ms |
1228 KB |
Output is correct |
80 |
Correct |
626 ms |
68756 KB |
Output is correct |
81 |
Correct |
615 ms |
68504 KB |
Output is correct |
82 |
Correct |
599 ms |
68496 KB |
Output is correct |
83 |
Correct |
580 ms |
68808 KB |
Output is correct |
84 |
Correct |
583 ms |
69056 KB |
Output is correct |
85 |
Correct |
630 ms |
68360 KB |
Output is correct |
86 |
Correct |
179 ms |
26448 KB |
Output is correct |
87 |
Correct |
1 ms |
332 KB |
Output is correct |