#include <bits/stdc++.h>
using namespace std;
typedef 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-1});
ans.push_back({0,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();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
4 ms |
1368 KB |
Output is correct |
3 |
Correct |
5 ms |
1380 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 |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
320 KB |
Output is correct |
9 |
Correct |
0 ms |
316 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 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 |
0 ms |
320 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
4 ms |
1376 KB |
Output is correct |
17 |
Correct |
37 ms |
10176 KB |
Output is correct |
18 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
19 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
0 ms |
320 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
232 ms |
32740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
368 ms |
63416 KB |
Output is correct |
3 |
Correct |
353 ms |
63376 KB |
Output is correct |
4 |
Correct |
363 ms |
63412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
368 ms |
63416 KB |
Output is correct |
3 |
Correct |
353 ms |
63376 KB |
Output is correct |
4 |
Correct |
363 ms |
63412 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
348 ms |
63352 KB |
Output is correct |
7 |
Correct |
362 ms |
63484 KB |
Output is correct |
8 |
Correct |
348 ms |
63468 KB |
Output is correct |
9 |
Correct |
378 ms |
63444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
368 ms |
63416 KB |
Output is correct |
3 |
Correct |
353 ms |
63376 KB |
Output is correct |
4 |
Correct |
363 ms |
63412 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
33 ms |
10192 KB |
Output is correct |
7 |
Correct |
36 ms |
10188 KB |
Output is correct |
8 |
Correct |
33 ms |
10176 KB |
Output is correct |
9 |
Correct |
33 ms |
10176 KB |
Output is correct |
10 |
Correct |
38 ms |
10188 KB |
Output is correct |
11 |
Correct |
34 ms |
10200 KB |
Output is correct |
12 |
Correct |
32 ms |
10236 KB |
Output is correct |
13 |
Correct |
37 ms |
10176 KB |
Output is correct |
14 |
Correct |
33 ms |
10172 KB |
Output is correct |
15 |
Correct |
36 ms |
10192 KB |
Output is correct |
16 |
Correct |
35 ms |
10176 KB |
Output is correct |
17 |
Correct |
32 ms |
10168 KB |
Output is correct |
18 |
Correct |
331 ms |
56840 KB |
Output is correct |
19 |
Correct |
331 ms |
56700 KB |
Output is correct |
20 |
Correct |
362 ms |
56708 KB |
Output is correct |
21 |
Correct |
43 ms |
10200 KB |
Output is correct |
22 |
Correct |
31 ms |
10172 KB |
Output is correct |
23 |
Correct |
97 ms |
21616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
36 ms |
10172 KB |
Output is correct |
3 |
Correct |
35 ms |
10176 KB |
Output is correct |
4 |
Correct |
400 ms |
63476 KB |
Output is correct |
5 |
Correct |
35 ms |
10192 KB |
Output is correct |
6 |
Correct |
36 ms |
10192 KB |
Output is correct |
7 |
Correct |
36 ms |
10232 KB |
Output is correct |
8 |
Correct |
36 ms |
10244 KB |
Output is correct |
9 |
Correct |
34 ms |
10172 KB |
Output is correct |
10 |
Correct |
40 ms |
10188 KB |
Output is correct |
11 |
Correct |
35 ms |
10188 KB |
Output is correct |
12 |
Correct |
31 ms |
10176 KB |
Output is correct |
13 |
Correct |
35 ms |
10168 KB |
Output is correct |
14 |
Correct |
360 ms |
63524 KB |
Output is correct |
15 |
Correct |
40 ms |
10176 KB |
Output is correct |
16 |
Correct |
324 ms |
56708 KB |
Output is correct |
17 |
Correct |
325 ms |
56644 KB |
Output is correct |
18 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
19 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
4 ms |
1368 KB |
Output is correct |
3 |
Correct |
5 ms |
1380 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 |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
320 KB |
Output is correct |
9 |
Correct |
0 ms |
316 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 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 |
0 ms |
320 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
4 ms |
1376 KB |
Output is correct |
17 |
Correct |
37 ms |
10176 KB |
Output is correct |
18 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
19 |
Halted |
0 ms |
0 KB |
- |