#include<bits/stdc++.h>
#define overload(a,b,c,d,...) d
#define rep1(a) for(ll _=0;_<(ll)a;++_)
#define rep2(i,a) for(ll i=0;i<(ll)a;++i)
#define rep3(i,a,b) for(ll i=a;i<(ll)b;++i)
#define rep(...) overload(__VA_ARGS__,rep3,rep2,rep1)(__VA_ARGS__)
#define rrep1(i,a) for(ll i=(ll)a-1;i>=0;--i)
#define rrep2(i,a,b) for(ll i=(ll)b-1;i>=(ll)a;--i)
#define rrep(...) overload(__VA_ARGS__,rrep2,rrep1)(__VA_ARGS__)
#define all(a) a.begin(),a.end()
#define lb(v,x) lower_bound(all(v),x);
#define ub(v,x) upper_bound(all(v),x);
using namespace std;
using ll=long long;
using ull=unsigned long long;
using ld=long double;
template<typename T,typename U>
inline bool chmin(T&a,const U&b){return (a>b?a=b,true:false);}
template<typename T,typename U>
inline bool chmax(T&a,const U&b){return (a<b?a=b,true:false);}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;cin>>n;
ll a,b;cin>>a>>b;
ll g=gcd(a,b+1);
if((ld)(1e18)/(ld)b<a/g){
ll ans=0;
rep(i,n){
ll l,r;cin>>l>>r;
ans+=r-l+1;
}
cout<<ans<<endl;return 0;
}
vector<pair<ll,ll>>v;
ll md=a/g*b;
rep(i,n){
ll l,r;cin>>l>>r;
if(r-l+1>=md){
cout<<md<<endl;return 0;
}
l%=md,r%=md;
if(l>r){
v.emplace_back(l,1);
v.emplace_back(md,-1);
v.emplace_back(0,1);
v.emplace_back(r+1,-1);
}
else{
v.emplace_back(l,1);
v.emplace_back(r+1,-1);
}
}
sort(all(v));
ll ans=0,cnt=1;
rep(i,1,v.size()){
if(cnt>0){
ans+=v[i].first-v[i-1].first;
}
cnt+=v[i].second;
}
cout<<ans<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
6 ms |
984 KB |
Output is correct |
3 |
Correct |
5 ms |
856 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 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 |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
320 KB |
Output is correct |
15 |
Correct |
1 ms |
320 KB |
Output is correct |
16 |
Correct |
6 ms |
1240 KB |
Output is correct |
17 |
Correct |
54 ms |
7288 KB |
Output is correct |
18 |
Correct |
1 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 |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
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 |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
336 ms |
33360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
475 ms |
33256 KB |
Output is correct |
3 |
Correct |
570 ms |
68788 KB |
Output is correct |
4 |
Correct |
488 ms |
68988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
475 ms |
33256 KB |
Output is correct |
3 |
Correct |
570 ms |
68788 KB |
Output is correct |
4 |
Correct |
488 ms |
68988 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
514 ms |
68900 KB |
Output is correct |
7 |
Correct |
528 ms |
68992 KB |
Output is correct |
8 |
Correct |
493 ms |
68880 KB |
Output is correct |
9 |
Correct |
602 ms |
68868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
475 ms |
33256 KB |
Output is correct |
3 |
Correct |
570 ms |
68788 KB |
Output is correct |
4 |
Correct |
488 ms |
68988 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
50 ms |
7184 KB |
Output is correct |
7 |
Correct |
52 ms |
7272 KB |
Output is correct |
8 |
Correct |
47 ms |
7296 KB |
Output is correct |
9 |
Correct |
48 ms |
7252 KB |
Output is correct |
10 |
Correct |
47 ms |
7188 KB |
Output is correct |
11 |
Correct |
49 ms |
7224 KB |
Output is correct |
12 |
Correct |
57 ms |
7344 KB |
Output is correct |
13 |
Correct |
57 ms |
7296 KB |
Output is correct |
14 |
Correct |
47 ms |
7228 KB |
Output is correct |
15 |
Correct |
52 ms |
7236 KB |
Output is correct |
16 |
Correct |
54 ms |
7212 KB |
Output is correct |
17 |
Correct |
47 ms |
7188 KB |
Output is correct |
18 |
Correct |
513 ms |
68856 KB |
Output is correct |
19 |
Correct |
460 ms |
68884 KB |
Output is correct |
20 |
Correct |
608 ms |
68868 KB |
Output is correct |
21 |
Correct |
68 ms |
7288 KB |
Output is correct |
22 |
Correct |
45 ms |
7276 KB |
Output is correct |
23 |
Correct |
138 ms |
26636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
48 ms |
4488 KB |
Output is correct |
3 |
Correct |
51 ms |
4556 KB |
Output is correct |
4 |
Correct |
590 ms |
33196 KB |
Output is correct |
5 |
Correct |
48 ms |
4544 KB |
Output is correct |
6 |
Correct |
53 ms |
4460 KB |
Output is correct |
7 |
Correct |
60 ms |
4548 KB |
Output is correct |
8 |
Correct |
55 ms |
4468 KB |
Output is correct |
9 |
Correct |
48 ms |
4556 KB |
Output is correct |
10 |
Correct |
53 ms |
4556 KB |
Output is correct |
11 |
Correct |
51 ms |
4448 KB |
Output is correct |
12 |
Correct |
44 ms |
4476 KB |
Output is correct |
13 |
Correct |
58 ms |
4520 KB |
Output is correct |
14 |
Correct |
559 ms |
33264 KB |
Output is correct |
15 |
Correct |
61 ms |
4488 KB |
Output is correct |
16 |
Correct |
464 ms |
33272 KB |
Output is correct |
17 |
Correct |
496 ms |
33176 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 |
6 ms |
984 KB |
Output is correct |
3 |
Correct |
5 ms |
856 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 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 |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
320 KB |
Output is correct |
15 |
Correct |
1 ms |
320 KB |
Output is correct |
16 |
Correct |
6 ms |
1240 KB |
Output is correct |
17 |
Correct |
54 ms |
7288 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
2 ms |
340 KB |
Output is correct |
28 |
Correct |
336 ms |
33360 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
475 ms |
33256 KB |
Output is correct |
31 |
Correct |
570 ms |
68788 KB |
Output is correct |
32 |
Correct |
488 ms |
68988 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
514 ms |
68900 KB |
Output is correct |
35 |
Correct |
528 ms |
68992 KB |
Output is correct |
36 |
Correct |
493 ms |
68880 KB |
Output is correct |
37 |
Correct |
602 ms |
68868 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
50 ms |
7184 KB |
Output is correct |
40 |
Correct |
52 ms |
7272 KB |
Output is correct |
41 |
Correct |
47 ms |
7296 KB |
Output is correct |
42 |
Correct |
48 ms |
7252 KB |
Output is correct |
43 |
Correct |
47 ms |
7188 KB |
Output is correct |
44 |
Correct |
49 ms |
7224 KB |
Output is correct |
45 |
Correct |
57 ms |
7344 KB |
Output is correct |
46 |
Correct |
57 ms |
7296 KB |
Output is correct |
47 |
Correct |
47 ms |
7228 KB |
Output is correct |
48 |
Correct |
52 ms |
7236 KB |
Output is correct |
49 |
Correct |
54 ms |
7212 KB |
Output is correct |
50 |
Correct |
47 ms |
7188 KB |
Output is correct |
51 |
Correct |
513 ms |
68856 KB |
Output is correct |
52 |
Correct |
460 ms |
68884 KB |
Output is correct |
53 |
Correct |
608 ms |
68868 KB |
Output is correct |
54 |
Correct |
68 ms |
7288 KB |
Output is correct |
55 |
Correct |
45 ms |
7276 KB |
Output is correct |
56 |
Correct |
138 ms |
26636 KB |
Output is correct |
57 |
Correct |
0 ms |
212 KB |
Output is correct |
58 |
Correct |
48 ms |
4488 KB |
Output is correct |
59 |
Correct |
51 ms |
4556 KB |
Output is correct |
60 |
Correct |
590 ms |
33196 KB |
Output is correct |
61 |
Correct |
48 ms |
4544 KB |
Output is correct |
62 |
Correct |
53 ms |
4460 KB |
Output is correct |
63 |
Correct |
60 ms |
4548 KB |
Output is correct |
64 |
Correct |
55 ms |
4468 KB |
Output is correct |
65 |
Correct |
48 ms |
4556 KB |
Output is correct |
66 |
Correct |
53 ms |
4556 KB |
Output is correct |
67 |
Correct |
51 ms |
4448 KB |
Output is correct |
68 |
Correct |
44 ms |
4476 KB |
Output is correct |
69 |
Correct |
58 ms |
4520 KB |
Output is correct |
70 |
Correct |
559 ms |
33264 KB |
Output is correct |
71 |
Correct |
61 ms |
4488 KB |
Output is correct |
72 |
Correct |
464 ms |
33272 KB |
Output is correct |
73 |
Correct |
496 ms |
33176 KB |
Output is correct |
74 |
Correct |
0 ms |
212 KB |
Output is correct |
75 |
Correct |
1 ms |
320 KB |
Output is correct |
76 |
Correct |
1 ms |
212 KB |
Output is correct |
77 |
Correct |
1 ms |
212 KB |
Output is correct |
78 |
Correct |
1 ms |
212 KB |
Output is correct |
79 |
Correct |
5 ms |
1316 KB |
Output is correct |
80 |
Correct |
670 ms |
68900 KB |
Output is correct |
81 |
Correct |
621 ms |
69004 KB |
Output is correct |
82 |
Correct |
574 ms |
68880 KB |
Output is correct |
83 |
Correct |
606 ms |
69004 KB |
Output is correct |
84 |
Correct |
593 ms |
69004 KB |
Output is correct |
85 |
Correct |
569 ms |
68768 KB |
Output is correct |
86 |
Correct |
144 ms |
26712 KB |
Output is correct |
87 |
Correct |
1 ms |
212 KB |
Output is correct |