#pragma GCC optimize("unroll-loops,Ofast,O3")
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define spc << " " <<
#define endl "\n"
#define all(x) x.begin(), x.end()
#define int long long
#define ii pair<long long,int>
#define vi vector<int>
#define vii vector<ii>
#define st first
#define nd second
#define inf 1e15
#define MOD 1000000007
#define MX 200005
using namespace std;
void solve(){
int n,A,B; cin >> n >> A >> B;
vii seg;
A = A/__gcd(B+1, A);
if(sqrt(A)*sqrt(B)>=1e9){
int ans=0;
for(int i=1; i<=n; i++){
int l,r; cin >> l >> r;
ans+=r-l+1;
}
cout << ans << endl;
return;
}
int wow = A*B;
//cerr << wow << endl;
for(int i=1; i<=n; i++){
int l,r; cin >> l >> r;
if(r-l+1>=wow){
cout << wow << endl;
return;
}
l%=wow; r%=wow;
if(r>=l) seg.pb({l,r});
else{
seg.pb({0,r});
seg.pb({l, wow-1});
}
}
sort(all(seg));
int ans=0, cur=-1;
for(auto p:seg){
//cerr << p.st spc p.nd << endl;
ans += max((int)0, min(p.nd-p.st+1, p.nd-cur));
cur = max(cur, p.nd);
}
cout << ans << endl;
}
signed main(){
ios_base::sync_with_stdio(false);cin.tie(0);
#ifdef Local
freopen("in","r",stdin);
freopen("out","w",stdout);
#endif
/*freopen("nondec.in","r",stdin);
freopen("nondec.out","w",stdout);*/
int t=1;
//cin >> t;
while(t--) solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
736 KB |
Output is correct |
3 |
Correct |
3 ms |
736 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
736 KB |
Output is correct |
17 |
Correct |
25 ms |
2520 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
163 ms |
17392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
208 ms |
17384 KB |
Output is correct |
3 |
Correct |
209 ms |
36280 KB |
Output is correct |
4 |
Correct |
212 ms |
37560 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
208 ms |
17384 KB |
Output is correct |
3 |
Correct |
209 ms |
36280 KB |
Output is correct |
4 |
Correct |
212 ms |
37560 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
210 ms |
36244 KB |
Output is correct |
7 |
Correct |
213 ms |
37264 KB |
Output is correct |
8 |
Correct |
215 ms |
37300 KB |
Output is correct |
9 |
Correct |
236 ms |
36788 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
208 ms |
17384 KB |
Output is correct |
3 |
Correct |
209 ms |
36280 KB |
Output is correct |
4 |
Correct |
212 ms |
37560 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
21 ms |
4568 KB |
Output is correct |
7 |
Correct |
24 ms |
4524 KB |
Output is correct |
8 |
Correct |
23 ms |
4568 KB |
Output is correct |
9 |
Correct |
24 ms |
4476 KB |
Output is correct |
10 |
Correct |
21 ms |
4460 KB |
Output is correct |
11 |
Correct |
25 ms |
4568 KB |
Output is correct |
12 |
Correct |
21 ms |
4556 KB |
Output is correct |
13 |
Correct |
24 ms |
4564 KB |
Output is correct |
14 |
Correct |
22 ms |
4568 KB |
Output is correct |
15 |
Correct |
25 ms |
4568 KB |
Output is correct |
16 |
Correct |
24 ms |
4568 KB |
Output is correct |
17 |
Correct |
23 ms |
4568 KB |
Output is correct |
18 |
Correct |
217 ms |
35788 KB |
Output is correct |
19 |
Correct |
211 ms |
37556 KB |
Output is correct |
20 |
Correct |
232 ms |
35700 KB |
Output is correct |
21 |
Correct |
24 ms |
4568 KB |
Output is correct |
22 |
Correct |
20 ms |
4692 KB |
Output is correct |
23 |
Correct |
64 ms |
15296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
29 ms |
2520 KB |
Output is correct |
3 |
Correct |
23 ms |
2520 KB |
Output is correct |
4 |
Correct |
257 ms |
36036 KB |
Output is correct |
5 |
Correct |
31 ms |
4568 KB |
Output is correct |
6 |
Correct |
25 ms |
4664 KB |
Output is correct |
7 |
Correct |
23 ms |
4568 KB |
Output is correct |
8 |
Correct |
25 ms |
4564 KB |
Output is correct |
9 |
Correct |
23 ms |
4568 KB |
Output is correct |
10 |
Correct |
24 ms |
4568 KB |
Output is correct |
11 |
Correct |
23 ms |
4568 KB |
Output is correct |
12 |
Correct |
21 ms |
4552 KB |
Output is correct |
13 |
Correct |
23 ms |
4568 KB |
Output is correct |
14 |
Correct |
242 ms |
37044 KB |
Output is correct |
15 |
Correct |
23 ms |
4564 KB |
Output is correct |
16 |
Correct |
212 ms |
36564 KB |
Output is correct |
17 |
Correct |
212 ms |
36112 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
736 KB |
Output is correct |
3 |
Correct |
3 ms |
736 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
736 KB |
Output is correct |
17 |
Correct |
25 ms |
2520 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
163 ms |
17392 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
208 ms |
17384 KB |
Output is correct |
31 |
Correct |
209 ms |
36280 KB |
Output is correct |
32 |
Correct |
212 ms |
37560 KB |
Output is correct |
33 |
Correct |
0 ms |
344 KB |
Output is correct |
34 |
Correct |
210 ms |
36244 KB |
Output is correct |
35 |
Correct |
213 ms |
37264 KB |
Output is correct |
36 |
Correct |
215 ms |
37300 KB |
Output is correct |
37 |
Correct |
236 ms |
36788 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
21 ms |
4568 KB |
Output is correct |
40 |
Correct |
24 ms |
4524 KB |
Output is correct |
41 |
Correct |
23 ms |
4568 KB |
Output is correct |
42 |
Correct |
24 ms |
4476 KB |
Output is correct |
43 |
Correct |
21 ms |
4460 KB |
Output is correct |
44 |
Correct |
25 ms |
4568 KB |
Output is correct |
45 |
Correct |
21 ms |
4556 KB |
Output is correct |
46 |
Correct |
24 ms |
4564 KB |
Output is correct |
47 |
Correct |
22 ms |
4568 KB |
Output is correct |
48 |
Correct |
25 ms |
4568 KB |
Output is correct |
49 |
Correct |
24 ms |
4568 KB |
Output is correct |
50 |
Correct |
23 ms |
4568 KB |
Output is correct |
51 |
Correct |
217 ms |
35788 KB |
Output is correct |
52 |
Correct |
211 ms |
37556 KB |
Output is correct |
53 |
Correct |
232 ms |
35700 KB |
Output is correct |
54 |
Correct |
24 ms |
4568 KB |
Output is correct |
55 |
Correct |
20 ms |
4692 KB |
Output is correct |
56 |
Correct |
64 ms |
15296 KB |
Output is correct |
57 |
Correct |
1 ms |
348 KB |
Output is correct |
58 |
Correct |
29 ms |
2520 KB |
Output is correct |
59 |
Correct |
23 ms |
2520 KB |
Output is correct |
60 |
Correct |
257 ms |
36036 KB |
Output is correct |
61 |
Correct |
31 ms |
4568 KB |
Output is correct |
62 |
Correct |
25 ms |
4664 KB |
Output is correct |
63 |
Correct |
23 ms |
4568 KB |
Output is correct |
64 |
Correct |
25 ms |
4564 KB |
Output is correct |
65 |
Correct |
23 ms |
4568 KB |
Output is correct |
66 |
Correct |
24 ms |
4568 KB |
Output is correct |
67 |
Correct |
23 ms |
4568 KB |
Output is correct |
68 |
Correct |
21 ms |
4552 KB |
Output is correct |
69 |
Correct |
23 ms |
4568 KB |
Output is correct |
70 |
Correct |
242 ms |
37044 KB |
Output is correct |
71 |
Correct |
23 ms |
4564 KB |
Output is correct |
72 |
Correct |
212 ms |
36564 KB |
Output is correct |
73 |
Correct |
212 ms |
36112 KB |
Output is correct |
74 |
Correct |
1 ms |
344 KB |
Output is correct |
75 |
Correct |
0 ms |
344 KB |
Output is correct |
76 |
Correct |
0 ms |
348 KB |
Output is correct |
77 |
Correct |
0 ms |
348 KB |
Output is correct |
78 |
Correct |
0 ms |
348 KB |
Output is correct |
79 |
Correct |
3 ms |
1192 KB |
Output is correct |
80 |
Correct |
240 ms |
36024 KB |
Output is correct |
81 |
Correct |
248 ms |
36608 KB |
Output is correct |
82 |
Correct |
224 ms |
17336 KB |
Output is correct |
83 |
Correct |
226 ms |
17860 KB |
Output is correct |
84 |
Correct |
235 ms |
18100 KB |
Output is correct |
85 |
Correct |
225 ms |
17588 KB |
Output is correct |
86 |
Correct |
63 ms |
8728 KB |
Output is correct |
87 |
Correct |
0 ms |
348 KB |
Output is correct |