#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define jizz ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define pb push_back
#define MP make_pair
#define F first
#define S second
#define ET cout << "\n"
#define MEM(i,j) memset(i,j,sizeof i)
#define ALL(v) v.begin(),v.end()
#define DB(a,s,e) {for(int i=s;i<e;++i) cerr << a[i] << " ";ET;}
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
vector<pll> v;
int main()
{jizz
ll n,a,b,l,r,ans=0,g;
__int128 cycle;
cin >> n >> a >> b,g=a/__gcd(b+1,a),cycle=(__int128)g*b;
for(int i=1;i<=n;++i)
{
cin >> l >> r;
if((__int128)(r-l+1)>=cycle)
return cout << (ll)cycle << "\n",0;
if(l%cycle<=r%cycle)
v.pb(MP((ll)l%cycle,(ll)r%cycle));
else
v.pb(MP((ll)l%cycle,(ll)cycle-1)),v.pb(MP(0LL,(ll)r%cycle));
}
sort(ALL(v)),r=-1;
for(auto i:v)
{
if(i.F<=r) ans+=max(i.S-r,0LL),r=max(i.S,r);
else ans+=i.S-i.F+1,r=i.S;
}
cout << ans << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
8 ms |
1144 KB |
Output is correct |
3 |
Correct |
9 ms |
1016 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
3 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
256 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
8 ms |
908 KB |
Output is correct |
17 |
Correct |
69 ms |
3196 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
380 KB |
Output is correct |
5 |
Correct |
459 ms |
17872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
504 KB |
Output is correct |
2 |
Correct |
623 ms |
17796 KB |
Output is correct |
3 |
Correct |
609 ms |
17900 KB |
Output is correct |
4 |
Correct |
610 ms |
17692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
504 KB |
Output is correct |
2 |
Correct |
623 ms |
17796 KB |
Output is correct |
3 |
Correct |
609 ms |
17900 KB |
Output is correct |
4 |
Correct |
610 ms |
17692 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
613 ms |
17868 KB |
Output is correct |
7 |
Correct |
615 ms |
17660 KB |
Output is correct |
8 |
Correct |
619 ms |
17536 KB |
Output is correct |
9 |
Correct |
675 ms |
17892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
504 KB |
Output is correct |
2 |
Correct |
623 ms |
17796 KB |
Output is correct |
3 |
Correct |
609 ms |
17900 KB |
Output is correct |
4 |
Correct |
610 ms |
17692 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
67 ms |
3392 KB |
Output is correct |
7 |
Correct |
67 ms |
3440 KB |
Output is correct |
8 |
Correct |
63 ms |
3440 KB |
Output is correct |
9 |
Correct |
68 ms |
3564 KB |
Output is correct |
10 |
Correct |
64 ms |
3608 KB |
Output is correct |
11 |
Correct |
63 ms |
3464 KB |
Output is correct |
12 |
Correct |
63 ms |
3444 KB |
Output is correct |
13 |
Correct |
69 ms |
3452 KB |
Output is correct |
14 |
Correct |
63 ms |
3568 KB |
Output is correct |
15 |
Correct |
70 ms |
3540 KB |
Output is correct |
16 |
Correct |
68 ms |
3564 KB |
Output is correct |
17 |
Correct |
67 ms |
3540 KB |
Output is correct |
18 |
Correct |
644 ms |
17740 KB |
Output is correct |
19 |
Correct |
603 ms |
17364 KB |
Output is correct |
20 |
Correct |
670 ms |
17364 KB |
Output is correct |
21 |
Correct |
67 ms |
2928 KB |
Output is correct |
22 |
Correct |
61 ms |
3156 KB |
Output is correct |
23 |
Correct |
196 ms |
9508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
296 KB |
Output is correct |
2 |
Correct |
67 ms |
2772 KB |
Output is correct |
3 |
Correct |
69 ms |
2668 KB |
Output is correct |
4 |
Correct |
698 ms |
17004 KB |
Output is correct |
5 |
Correct |
67 ms |
2924 KB |
Output is correct |
6 |
Correct |
67 ms |
2928 KB |
Output is correct |
7 |
Correct |
67 ms |
2800 KB |
Output is correct |
8 |
Correct |
68 ms |
2672 KB |
Output is correct |
9 |
Correct |
66 ms |
2612 KB |
Output is correct |
10 |
Correct |
68 ms |
2692 KB |
Output is correct |
11 |
Correct |
68 ms |
2672 KB |
Output is correct |
12 |
Correct |
60 ms |
2672 KB |
Output is correct |
13 |
Correct |
67 ms |
2688 KB |
Output is correct |
14 |
Correct |
681 ms |
17088 KB |
Output is correct |
15 |
Correct |
67 ms |
2928 KB |
Output is correct |
16 |
Correct |
624 ms |
17344 KB |
Output is correct |
17 |
Correct |
623 ms |
17236 KB |
Output is correct |
18 |
Correct |
2 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
8 ms |
1144 KB |
Output is correct |
3 |
Correct |
9 ms |
1016 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
3 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
256 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
8 ms |
908 KB |
Output is correct |
17 |
Correct |
69 ms |
3196 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
256 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
3 ms |
376 KB |
Output is correct |
26 |
Correct |
3 ms |
376 KB |
Output is correct |
27 |
Correct |
3 ms |
380 KB |
Output is correct |
28 |
Correct |
459 ms |
17872 KB |
Output is correct |
29 |
Correct |
3 ms |
504 KB |
Output is correct |
30 |
Correct |
623 ms |
17796 KB |
Output is correct |
31 |
Correct |
609 ms |
17900 KB |
Output is correct |
32 |
Correct |
610 ms |
17692 KB |
Output is correct |
33 |
Correct |
2 ms |
376 KB |
Output is correct |
34 |
Correct |
613 ms |
17868 KB |
Output is correct |
35 |
Correct |
615 ms |
17660 KB |
Output is correct |
36 |
Correct |
619 ms |
17536 KB |
Output is correct |
37 |
Correct |
675 ms |
17892 KB |
Output is correct |
38 |
Correct |
2 ms |
376 KB |
Output is correct |
39 |
Correct |
67 ms |
3392 KB |
Output is correct |
40 |
Correct |
67 ms |
3440 KB |
Output is correct |
41 |
Correct |
63 ms |
3440 KB |
Output is correct |
42 |
Correct |
68 ms |
3564 KB |
Output is correct |
43 |
Correct |
64 ms |
3608 KB |
Output is correct |
44 |
Correct |
63 ms |
3464 KB |
Output is correct |
45 |
Correct |
63 ms |
3444 KB |
Output is correct |
46 |
Correct |
69 ms |
3452 KB |
Output is correct |
47 |
Correct |
63 ms |
3568 KB |
Output is correct |
48 |
Correct |
70 ms |
3540 KB |
Output is correct |
49 |
Correct |
68 ms |
3564 KB |
Output is correct |
50 |
Correct |
67 ms |
3540 KB |
Output is correct |
51 |
Correct |
644 ms |
17740 KB |
Output is correct |
52 |
Correct |
603 ms |
17364 KB |
Output is correct |
53 |
Correct |
670 ms |
17364 KB |
Output is correct |
54 |
Correct |
67 ms |
2928 KB |
Output is correct |
55 |
Correct |
61 ms |
3156 KB |
Output is correct |
56 |
Correct |
196 ms |
9508 KB |
Output is correct |
57 |
Correct |
2 ms |
296 KB |
Output is correct |
58 |
Correct |
67 ms |
2772 KB |
Output is correct |
59 |
Correct |
69 ms |
2668 KB |
Output is correct |
60 |
Correct |
698 ms |
17004 KB |
Output is correct |
61 |
Correct |
67 ms |
2924 KB |
Output is correct |
62 |
Correct |
67 ms |
2928 KB |
Output is correct |
63 |
Correct |
67 ms |
2800 KB |
Output is correct |
64 |
Correct |
68 ms |
2672 KB |
Output is correct |
65 |
Correct |
66 ms |
2612 KB |
Output is correct |
66 |
Correct |
68 ms |
2692 KB |
Output is correct |
67 |
Correct |
68 ms |
2672 KB |
Output is correct |
68 |
Correct |
60 ms |
2672 KB |
Output is correct |
69 |
Correct |
67 ms |
2688 KB |
Output is correct |
70 |
Correct |
681 ms |
17088 KB |
Output is correct |
71 |
Correct |
67 ms |
2928 KB |
Output is correct |
72 |
Correct |
624 ms |
17344 KB |
Output is correct |
73 |
Correct |
623 ms |
17236 KB |
Output is correct |
74 |
Correct |
2 ms |
256 KB |
Output is correct |
75 |
Correct |
2 ms |
376 KB |
Output is correct |
76 |
Correct |
2 ms |
376 KB |
Output is correct |
77 |
Correct |
2 ms |
376 KB |
Output is correct |
78 |
Correct |
2 ms |
376 KB |
Output is correct |
79 |
Correct |
8 ms |
1016 KB |
Output is correct |
80 |
Correct |
690 ms |
17508 KB |
Output is correct |
81 |
Correct |
699 ms |
17352 KB |
Output is correct |
82 |
Correct |
667 ms |
17224 KB |
Output is correct |
83 |
Correct |
747 ms |
17168 KB |
Output is correct |
84 |
Correct |
667 ms |
17284 KB |
Output is correct |
85 |
Correct |
664 ms |
17160 KB |
Output is correct |
86 |
Correct |
194 ms |
9104 KB |
Output is correct |
87 |
Correct |
2 ms |
380 KB |
Output is correct |