#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
void solve(){
ll n,a,b; cin >> n >> a >> b;
vector<pair<ll,ll>> arr(n);
ll curr=0;
for(int i=0; i < n;i++){
cin >> arr[i].first >> arr[i].second;
curr+=arr[i].second - arr[i].first +1;
}
ll x=a/(__gcd(b+1,a));
ll maxi=x*b;
if((arr[n-1].second-1)< maxi){
cout << curr << endl;
return;
}
vector<pair<ll,ll>> ans;
for(int i=0; i < n;i++){
if(arr[i].second - arr[i].first +1>=maxi){
cout << maxi << endl;
return;
}
arr[i].second%=maxi;
arr[i].first%=maxi;
if(arr[i].first > arr[i].second){
ans.push_back({arr[i].first,maxi-1ll});
ans.push_back({0ll,arr[i].second});
} else{
ans.push_back({arr[i].first, arr[i].second});
}
}
sort(ans.begin(),ans.end());
vector<pair<ll,ll>> add;
curr=-1;
for(auto c: ans){
if(curr==-1){
curr=0;
add.push_back(c);
} else if(c.first > add[curr].second){
add.push_back(c);
curr++;
} else{
add[curr].second=max(add[curr].second,c.second);
}
}
ll out=0;
for(auto c : add) out += c.second - c.first +1;
cout << out << endl;
}
int main(){
ios::sync_with_stdio(false);cout.tie();cin.tie();
int t=1;
// cin >> t;
while(t--){
solve();
}
}
Compilation message
strange_device.cpp: In function 'void solve()':
strange_device.cpp:9:20: warning: comparison of integer expressions of different signedness: 'int' and 'll' {aka 'long long unsigned int'} [-Wsign-compare]
9 | for(int i=0; i < n;i++){
| ~~^~~
strange_device.cpp:20:20: warning: comparison of integer expressions of different signedness: 'int' and 'll' {aka 'long long unsigned int'} [-Wsign-compare]
20 | for(int i=0; i < n;i++){
| ~~^~~
strange_device.cpp:38:16: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long unsigned int'} and 'int' [-Wsign-compare]
38 | if(curr==-1){
| ~~~~^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
4 ms |
1140 KB |
Output is correct |
3 |
Correct |
4 ms |
1112 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
8 |
Correct |
0 ms |
316 KB |
Output is correct |
9 |
Correct |
0 ms |
232 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
5 ms |
1156 KB |
Output is correct |
17 |
Correct |
36 ms |
6584 KB |
Output is correct |
18 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
324 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
0 ms |
320 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
213 ms |
32648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
329 ms |
56316 KB |
Output is correct |
3 |
Correct |
313 ms |
56348 KB |
Output is correct |
4 |
Correct |
318 ms |
56348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
329 ms |
56316 KB |
Output is correct |
3 |
Correct |
313 ms |
56348 KB |
Output is correct |
4 |
Correct |
318 ms |
56348 KB |
Output is correct |
5 |
Correct |
0 ms |
320 KB |
Output is correct |
6 |
Correct |
322 ms |
56372 KB |
Output is correct |
7 |
Correct |
325 ms |
56384 KB |
Output is correct |
8 |
Correct |
341 ms |
56356 KB |
Output is correct |
9 |
Correct |
340 ms |
56404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
329 ms |
56316 KB |
Output is correct |
3 |
Correct |
313 ms |
56348 KB |
Output is correct |
4 |
Correct |
318 ms |
56348 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
31 ms |
6536 KB |
Output is correct |
7 |
Correct |
38 ms |
6660 KB |
Output is correct |
8 |
Correct |
34 ms |
6604 KB |
Output is correct |
9 |
Correct |
31 ms |
6536 KB |
Output is correct |
10 |
Correct |
31 ms |
6640 KB |
Output is correct |
11 |
Correct |
31 ms |
6596 KB |
Output is correct |
12 |
Correct |
30 ms |
6624 KB |
Output is correct |
13 |
Correct |
33 ms |
6600 KB |
Output is correct |
14 |
Correct |
32 ms |
6724 KB |
Output is correct |
15 |
Correct |
34 ms |
6616 KB |
Output is correct |
16 |
Correct |
38 ms |
6616 KB |
Output is correct |
17 |
Correct |
31 ms |
6560 KB |
Output is correct |
18 |
Correct |
315 ms |
56380 KB |
Output is correct |
19 |
Correct |
318 ms |
56452 KB |
Output is correct |
20 |
Correct |
362 ms |
56436 KB |
Output is correct |
21 |
Correct |
33 ms |
6616 KB |
Output is correct |
22 |
Correct |
32 ms |
6708 KB |
Output is correct |
23 |
Correct |
89 ms |
14228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
33 ms |
6584 KB |
Output is correct |
3 |
Correct |
33 ms |
6616 KB |
Output is correct |
4 |
Correct |
405 ms |
56348 KB |
Output is correct |
5 |
Correct |
42 ms |
6656 KB |
Output is correct |
6 |
Correct |
39 ms |
6656 KB |
Output is correct |
7 |
Correct |
34 ms |
6624 KB |
Output is correct |
8 |
Correct |
34 ms |
6616 KB |
Output is correct |
9 |
Correct |
32 ms |
6616 KB |
Output is correct |
10 |
Correct |
32 ms |
6608 KB |
Output is correct |
11 |
Correct |
33 ms |
6620 KB |
Output is correct |
12 |
Correct |
43 ms |
6620 KB |
Output is correct |
13 |
Correct |
49 ms |
6596 KB |
Output is correct |
14 |
Correct |
349 ms |
56352 KB |
Output is correct |
15 |
Correct |
49 ms |
6724 KB |
Output is correct |
16 |
Correct |
322 ms |
56568 KB |
Output is correct |
17 |
Correct |
438 ms |
56516 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
4 ms |
1140 KB |
Output is correct |
3 |
Correct |
4 ms |
1112 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
8 |
Correct |
0 ms |
316 KB |
Output is correct |
9 |
Correct |
0 ms |
232 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
5 ms |
1156 KB |
Output is correct |
17 |
Correct |
36 ms |
6584 KB |
Output is correct |
18 |
Correct |
1 ms |
320 KB |
Output is correct |
19 |
Correct |
0 ms |
324 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
320 KB |
Output is correct |
22 |
Correct |
0 ms |
320 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
328 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
213 ms |
32648 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
329 ms |
56316 KB |
Output is correct |
31 |
Correct |
313 ms |
56348 KB |
Output is correct |
32 |
Correct |
318 ms |
56348 KB |
Output is correct |
33 |
Correct |
0 ms |
320 KB |
Output is correct |
34 |
Correct |
322 ms |
56372 KB |
Output is correct |
35 |
Correct |
325 ms |
56384 KB |
Output is correct |
36 |
Correct |
341 ms |
56356 KB |
Output is correct |
37 |
Correct |
340 ms |
56404 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
31 ms |
6536 KB |
Output is correct |
40 |
Correct |
38 ms |
6660 KB |
Output is correct |
41 |
Correct |
34 ms |
6604 KB |
Output is correct |
42 |
Correct |
31 ms |
6536 KB |
Output is correct |
43 |
Correct |
31 ms |
6640 KB |
Output is correct |
44 |
Correct |
31 ms |
6596 KB |
Output is correct |
45 |
Correct |
30 ms |
6624 KB |
Output is correct |
46 |
Correct |
33 ms |
6600 KB |
Output is correct |
47 |
Correct |
32 ms |
6724 KB |
Output is correct |
48 |
Correct |
34 ms |
6616 KB |
Output is correct |
49 |
Correct |
38 ms |
6616 KB |
Output is correct |
50 |
Correct |
31 ms |
6560 KB |
Output is correct |
51 |
Correct |
315 ms |
56380 KB |
Output is correct |
52 |
Correct |
318 ms |
56452 KB |
Output is correct |
53 |
Correct |
362 ms |
56436 KB |
Output is correct |
54 |
Correct |
33 ms |
6616 KB |
Output is correct |
55 |
Correct |
32 ms |
6708 KB |
Output is correct |
56 |
Correct |
89 ms |
14228 KB |
Output is correct |
57 |
Correct |
1 ms |
212 KB |
Output is correct |
58 |
Correct |
33 ms |
6584 KB |
Output is correct |
59 |
Correct |
33 ms |
6616 KB |
Output is correct |
60 |
Correct |
405 ms |
56348 KB |
Output is correct |
61 |
Correct |
42 ms |
6656 KB |
Output is correct |
62 |
Correct |
39 ms |
6656 KB |
Output is correct |
63 |
Correct |
34 ms |
6624 KB |
Output is correct |
64 |
Correct |
34 ms |
6616 KB |
Output is correct |
65 |
Correct |
32 ms |
6616 KB |
Output is correct |
66 |
Correct |
32 ms |
6608 KB |
Output is correct |
67 |
Correct |
33 ms |
6620 KB |
Output is correct |
68 |
Correct |
43 ms |
6620 KB |
Output is correct |
69 |
Correct |
49 ms |
6596 KB |
Output is correct |
70 |
Correct |
349 ms |
56352 KB |
Output is correct |
71 |
Correct |
49 ms |
6724 KB |
Output is correct |
72 |
Correct |
322 ms |
56568 KB |
Output is correct |
73 |
Correct |
438 ms |
56516 KB |
Output is correct |
74 |
Correct |
0 ms |
212 KB |
Output is correct |
75 |
Correct |
0 ms |
212 KB |
Output is correct |
76 |
Correct |
1 ms |
212 KB |
Output is correct |
77 |
Correct |
0 ms |
212 KB |
Output is correct |
78 |
Correct |
1 ms |
212 KB |
Output is correct |
79 |
Correct |
4 ms |
1392 KB |
Output is correct |
80 |
Correct |
417 ms |
56684 KB |
Output is correct |
81 |
Correct |
407 ms |
56632 KB |
Output is correct |
82 |
Correct |
372 ms |
56652 KB |
Output is correct |
83 |
Correct |
342 ms |
56620 KB |
Output is correct |
84 |
Correct |
357 ms |
56604 KB |
Output is correct |
85 |
Correct |
342 ms |
56592 KB |
Output is correct |
86 |
Correct |
94 ms |
19060 KB |
Output is correct |
87 |
Correct |
1 ms |
212 KB |
Output is correct |