#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> II;
const int N = 1e6 + 5, logN = 20;
const int MOD = 1e9 + 7;
const ll INF = 1e18;
int n;
ll A, B, cyc, sum = 0;
vector<II> vt;
II seg[N];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> A >> B;
for(int i = 1; i <= n; i ++) cin >> seg[i].first >> seg[i].second, sum += seg[i].second - seg[i].first + 1;
ll k = A / __gcd(A, B + 1);
if(k > INF / B + 1){
cout << sum << endl;
return 0;
}
cyc = k * B;
for(int i = 1; i <= n; i ++){
if(seg[i].second - seg[i].first + 1 >= cyc){
cout << cyc << endl;
return 0;
}
if(seg[i].first % cyc <= seg[i].second % cyc) vt.push_back(II(seg[i].first % cyc, seg[i].second % cyc));
else vt.push_back(II(0, seg[i].second % cyc)), vt.push_back(II(seg[i].first % cyc, cyc - 1));
}
sort(vt.begin(), vt.end());
ll Max = 0, ans = 0;
for(auto x : vt){
Max = max(Max, x.first);
ans += max(0ll, x.second - Max + 1);
Max = max(Max, x.second + 1);
}
cout << ans << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
5 ms |
1208 KB |
Output is correct |
3 |
Correct |
5 ms |
1228 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
212 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 |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
328 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
7 ms |
1208 KB |
Output is correct |
17 |
Correct |
50 ms |
7688 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
368 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
391 ms |
57648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
612 ms |
51228 KB |
Output is correct |
3 |
Correct |
498 ms |
51220 KB |
Output is correct |
4 |
Correct |
532 ms |
69536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
612 ms |
51228 KB |
Output is correct |
3 |
Correct |
498 ms |
51220 KB |
Output is correct |
4 |
Correct |
532 ms |
69536 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
487 ms |
69608 KB |
Output is correct |
7 |
Correct |
525 ms |
69636 KB |
Output is correct |
8 |
Correct |
561 ms |
69700 KB |
Output is correct |
9 |
Correct |
555 ms |
69612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
612 ms |
51228 KB |
Output is correct |
3 |
Correct |
498 ms |
51220 KB |
Output is correct |
4 |
Correct |
532 ms |
69536 KB |
Output is correct |
5 |
Correct |
2 ms |
212 KB |
Output is correct |
6 |
Correct |
50 ms |
7724 KB |
Output is correct |
7 |
Correct |
51 ms |
7752 KB |
Output is correct |
8 |
Correct |
48 ms |
7708 KB |
Output is correct |
9 |
Correct |
48 ms |
7752 KB |
Output is correct |
10 |
Correct |
56 ms |
7708 KB |
Output is correct |
11 |
Correct |
60 ms |
7764 KB |
Output is correct |
12 |
Correct |
46 ms |
7684 KB |
Output is correct |
13 |
Correct |
46 ms |
7736 KB |
Output is correct |
14 |
Correct |
51 ms |
7704 KB |
Output is correct |
15 |
Correct |
52 ms |
7748 KB |
Output is correct |
16 |
Correct |
55 ms |
7700 KB |
Output is correct |
17 |
Correct |
48 ms |
7688 KB |
Output is correct |
18 |
Correct |
546 ms |
69600 KB |
Output is correct |
19 |
Correct |
472 ms |
69688 KB |
Output is correct |
20 |
Correct |
484 ms |
69628 KB |
Output is correct |
21 |
Correct |
59 ms |
7792 KB |
Output is correct |
22 |
Correct |
42 ms |
7764 KB |
Output is correct |
23 |
Correct |
142 ms |
26524 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
48 ms |
7772 KB |
Output is correct |
3 |
Correct |
52 ms |
7768 KB |
Output is correct |
4 |
Correct |
535 ms |
52060 KB |
Output is correct |
5 |
Correct |
50 ms |
7756 KB |
Output is correct |
6 |
Correct |
53 ms |
7780 KB |
Output is correct |
7 |
Correct |
45 ms |
7804 KB |
Output is correct |
8 |
Correct |
52 ms |
7752 KB |
Output is correct |
9 |
Correct |
47 ms |
7680 KB |
Output is correct |
10 |
Correct |
48 ms |
7764 KB |
Output is correct |
11 |
Correct |
48 ms |
7720 KB |
Output is correct |
12 |
Correct |
42 ms |
7800 KB |
Output is correct |
13 |
Correct |
47 ms |
7712 KB |
Output is correct |
14 |
Correct |
506 ms |
69644 KB |
Output is correct |
15 |
Correct |
68 ms |
7760 KB |
Output is correct |
16 |
Correct |
464 ms |
69572 KB |
Output is correct |
17 |
Correct |
443 ms |
69528 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
5 ms |
1208 KB |
Output is correct |
3 |
Correct |
5 ms |
1228 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
212 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 |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
328 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
7 ms |
1208 KB |
Output is correct |
17 |
Correct |
50 ms |
7688 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
328 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
2 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
368 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
391 ms |
57648 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
612 ms |
51228 KB |
Output is correct |
31 |
Correct |
498 ms |
51220 KB |
Output is correct |
32 |
Correct |
532 ms |
69536 KB |
Output is correct |
33 |
Correct |
1 ms |
332 KB |
Output is correct |
34 |
Correct |
487 ms |
69608 KB |
Output is correct |
35 |
Correct |
525 ms |
69636 KB |
Output is correct |
36 |
Correct |
561 ms |
69700 KB |
Output is correct |
37 |
Correct |
555 ms |
69612 KB |
Output is correct |
38 |
Correct |
2 ms |
212 KB |
Output is correct |
39 |
Correct |
50 ms |
7724 KB |
Output is correct |
40 |
Correct |
51 ms |
7752 KB |
Output is correct |
41 |
Correct |
48 ms |
7708 KB |
Output is correct |
42 |
Correct |
48 ms |
7752 KB |
Output is correct |
43 |
Correct |
56 ms |
7708 KB |
Output is correct |
44 |
Correct |
60 ms |
7764 KB |
Output is correct |
45 |
Correct |
46 ms |
7684 KB |
Output is correct |
46 |
Correct |
46 ms |
7736 KB |
Output is correct |
47 |
Correct |
51 ms |
7704 KB |
Output is correct |
48 |
Correct |
52 ms |
7748 KB |
Output is correct |
49 |
Correct |
55 ms |
7700 KB |
Output is correct |
50 |
Correct |
48 ms |
7688 KB |
Output is correct |
51 |
Correct |
546 ms |
69600 KB |
Output is correct |
52 |
Correct |
472 ms |
69688 KB |
Output is correct |
53 |
Correct |
484 ms |
69628 KB |
Output is correct |
54 |
Correct |
59 ms |
7792 KB |
Output is correct |
55 |
Correct |
42 ms |
7764 KB |
Output is correct |
56 |
Correct |
142 ms |
26524 KB |
Output is correct |
57 |
Correct |
1 ms |
212 KB |
Output is correct |
58 |
Correct |
48 ms |
7772 KB |
Output is correct |
59 |
Correct |
52 ms |
7768 KB |
Output is correct |
60 |
Correct |
535 ms |
52060 KB |
Output is correct |
61 |
Correct |
50 ms |
7756 KB |
Output is correct |
62 |
Correct |
53 ms |
7780 KB |
Output is correct |
63 |
Correct |
45 ms |
7804 KB |
Output is correct |
64 |
Correct |
52 ms |
7752 KB |
Output is correct |
65 |
Correct |
47 ms |
7680 KB |
Output is correct |
66 |
Correct |
48 ms |
7764 KB |
Output is correct |
67 |
Correct |
48 ms |
7720 KB |
Output is correct |
68 |
Correct |
42 ms |
7800 KB |
Output is correct |
69 |
Correct |
47 ms |
7712 KB |
Output is correct |
70 |
Correct |
506 ms |
69644 KB |
Output is correct |
71 |
Correct |
68 ms |
7760 KB |
Output is correct |
72 |
Correct |
464 ms |
69572 KB |
Output is correct |
73 |
Correct |
443 ms |
69528 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 |
0 ms |
212 KB |
Output is correct |
79 |
Correct |
5 ms |
1260 KB |
Output is correct |
80 |
Correct |
495 ms |
69596 KB |
Output is correct |
81 |
Correct |
507 ms |
69612 KB |
Output is correct |
82 |
Correct |
473 ms |
69772 KB |
Output is correct |
83 |
Correct |
479 ms |
69628 KB |
Output is correct |
84 |
Correct |
465 ms |
69676 KB |
Output is correct |
85 |
Correct |
504 ms |
69580 KB |
Output is correct |
86 |
Correct |
136 ms |
26572 KB |
Output is correct |
87 |
Correct |
1 ms |
212 KB |
Output is correct |