#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int Nmax = 1e6 + 5;
const ll inf = 1e18 + 10;
struct seg
{
ll l, r;
} a[Nmax];
ll A, B, per;
int n;
int main()
{
// freopen("input", "r", stdin);
cin.sync_with_stdio(false); cin.tie(0);
int i;
cin >> n >> A >> B;
for(i=1; i<=n; ++i) cin >> a[i].l >> a[i].r;
ll g = __gcd(A, B+1);
A /= g;
/// if(A*B > inf)
if(A > inf / B + 1) per = inf;
else per = min(inf, A * B);
for(i=1; i<=n; ++i)
if(a[i].r - a[i].l + 1 >= per)
{
cout << per << '\n';
return 0;
}
vector<pair<ll, ll>> v, v2;
for(i=1; i<=n; ++i)
{
a[i].l %= per;
a[i].r %= per;
if(a[i].l <= a[i].r)
v.push_back({a[i].l, a[i].r});
else
{
v.push_back({a[i].l, per-1});
v.push_back({0, a[i].r});
}
}
sort(v.begin(), v.end(),
[](pair<ll,ll> a, pair<ll,ll> b)
{ if(a.first == b.first) return a.second > b.second; return a < b; }
);
ll mx = -1;
for(auto it : v)
if(it.second > mx) v2.push_back(it), mx = it.second;
swap(v, v2);
ll ans = per;
for(i=0; i+1<v.size(); ++i)
if(v[i].second < v[i+1].first) ans -= v[i+1].first - v[i].second - 1;
ans -= v[0].first;
ans -= per - 1 - v.back().second;
cout << ans << '\n';
return 0;
}
Compilation message
strange_device.cpp: In function 'int main()':
strange_device.cpp:71:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0; i+1<v.size(); ++i)
~~~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
9 ms |
1016 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 |
372 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 |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 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 |
1020 KB |
Output is correct |
17 |
Correct |
73 ms |
6632 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 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 |
1 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
376 KB |
Output is correct |
5 |
Correct |
471 ms |
57728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
376 KB |
Output is correct |
2 |
Correct |
670 ms |
56388 KB |
Output is correct |
3 |
Correct |
658 ms |
93600 KB |
Output is correct |
4 |
Correct |
663 ms |
93524 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
376 KB |
Output is correct |
2 |
Correct |
670 ms |
56388 KB |
Output is correct |
3 |
Correct |
658 ms |
93600 KB |
Output is correct |
4 |
Correct |
663 ms |
93524 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
672 ms |
93524 KB |
Output is correct |
7 |
Correct |
692 ms |
93408 KB |
Output is correct |
8 |
Correct |
674 ms |
93628 KB |
Output is correct |
9 |
Correct |
731 ms |
93504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
376 KB |
Output is correct |
2 |
Correct |
670 ms |
56388 KB |
Output is correct |
3 |
Correct |
658 ms |
93600 KB |
Output is correct |
4 |
Correct |
663 ms |
93524 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
67 ms |
10300 KB |
Output is correct |
7 |
Correct |
72 ms |
10340 KB |
Output is correct |
8 |
Correct |
68 ms |
10216 KB |
Output is correct |
9 |
Correct |
69 ms |
10448 KB |
Output is correct |
10 |
Correct |
67 ms |
10316 KB |
Output is correct |
11 |
Correct |
70 ms |
10292 KB |
Output is correct |
12 |
Correct |
67 ms |
10312 KB |
Output is correct |
13 |
Correct |
72 ms |
10216 KB |
Output is correct |
14 |
Correct |
74 ms |
10216 KB |
Output is correct |
15 |
Correct |
74 ms |
10344 KB |
Output is correct |
16 |
Correct |
72 ms |
10316 KB |
Output is correct |
17 |
Correct |
69 ms |
10340 KB |
Output is correct |
18 |
Correct |
687 ms |
93572 KB |
Output is correct |
19 |
Correct |
667 ms |
93536 KB |
Output is correct |
20 |
Correct |
745 ms |
93528 KB |
Output is correct |
21 |
Correct |
73 ms |
10344 KB |
Output is correct |
22 |
Correct |
66 ms |
10420 KB |
Output is correct |
23 |
Correct |
197 ms |
26592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
72 ms |
10332 KB |
Output is correct |
3 |
Correct |
80 ms |
10320 KB |
Output is correct |
4 |
Correct |
751 ms |
93440 KB |
Output is correct |
5 |
Correct |
72 ms |
10216 KB |
Output is correct |
6 |
Correct |
71 ms |
10288 KB |
Output is correct |
7 |
Correct |
74 ms |
10216 KB |
Output is correct |
8 |
Correct |
74 ms |
10216 KB |
Output is correct |
9 |
Correct |
71 ms |
10344 KB |
Output is correct |
10 |
Correct |
74 ms |
10356 KB |
Output is correct |
11 |
Correct |
71 ms |
10348 KB |
Output is correct |
12 |
Correct |
65 ms |
10396 KB |
Output is correct |
13 |
Correct |
72 ms |
10344 KB |
Output is correct |
14 |
Correct |
723 ms |
93280 KB |
Output is correct |
15 |
Correct |
71 ms |
10224 KB |
Output is correct |
16 |
Correct |
668 ms |
93372 KB |
Output is correct |
17 |
Correct |
660 ms |
93288 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
9 ms |
1016 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 |
372 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 |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 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 |
1020 KB |
Output is correct |
17 |
Correct |
73 ms |
6632 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 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 |
1 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 |
376 KB |
Output is correct |
28 |
Correct |
471 ms |
57728 KB |
Output is correct |
29 |
Correct |
0 ms |
376 KB |
Output is correct |
30 |
Correct |
670 ms |
56388 KB |
Output is correct |
31 |
Correct |
658 ms |
93600 KB |
Output is correct |
32 |
Correct |
663 ms |
93524 KB |
Output is correct |
33 |
Correct |
2 ms |
376 KB |
Output is correct |
34 |
Correct |
672 ms |
93524 KB |
Output is correct |
35 |
Correct |
692 ms |
93408 KB |
Output is correct |
36 |
Correct |
674 ms |
93628 KB |
Output is correct |
37 |
Correct |
731 ms |
93504 KB |
Output is correct |
38 |
Correct |
2 ms |
376 KB |
Output is correct |
39 |
Correct |
67 ms |
10300 KB |
Output is correct |
40 |
Correct |
72 ms |
10340 KB |
Output is correct |
41 |
Correct |
68 ms |
10216 KB |
Output is correct |
42 |
Correct |
69 ms |
10448 KB |
Output is correct |
43 |
Correct |
67 ms |
10316 KB |
Output is correct |
44 |
Correct |
70 ms |
10292 KB |
Output is correct |
45 |
Correct |
67 ms |
10312 KB |
Output is correct |
46 |
Correct |
72 ms |
10216 KB |
Output is correct |
47 |
Correct |
74 ms |
10216 KB |
Output is correct |
48 |
Correct |
74 ms |
10344 KB |
Output is correct |
49 |
Correct |
72 ms |
10316 KB |
Output is correct |
50 |
Correct |
69 ms |
10340 KB |
Output is correct |
51 |
Correct |
687 ms |
93572 KB |
Output is correct |
52 |
Correct |
667 ms |
93536 KB |
Output is correct |
53 |
Correct |
745 ms |
93528 KB |
Output is correct |
54 |
Correct |
73 ms |
10344 KB |
Output is correct |
55 |
Correct |
66 ms |
10420 KB |
Output is correct |
56 |
Correct |
197 ms |
26592 KB |
Output is correct |
57 |
Correct |
2 ms |
376 KB |
Output is correct |
58 |
Correct |
72 ms |
10332 KB |
Output is correct |
59 |
Correct |
80 ms |
10320 KB |
Output is correct |
60 |
Correct |
751 ms |
93440 KB |
Output is correct |
61 |
Correct |
72 ms |
10216 KB |
Output is correct |
62 |
Correct |
71 ms |
10288 KB |
Output is correct |
63 |
Correct |
74 ms |
10216 KB |
Output is correct |
64 |
Correct |
74 ms |
10216 KB |
Output is correct |
65 |
Correct |
71 ms |
10344 KB |
Output is correct |
66 |
Correct |
74 ms |
10356 KB |
Output is correct |
67 |
Correct |
71 ms |
10348 KB |
Output is correct |
68 |
Correct |
65 ms |
10396 KB |
Output is correct |
69 |
Correct |
72 ms |
10344 KB |
Output is correct |
70 |
Correct |
723 ms |
93280 KB |
Output is correct |
71 |
Correct |
71 ms |
10224 KB |
Output is correct |
72 |
Correct |
668 ms |
93372 KB |
Output is correct |
73 |
Correct |
660 ms |
93288 KB |
Output is correct |
74 |
Correct |
2 ms |
376 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 |
9 ms |
1400 KB |
Output is correct |
80 |
Correct |
750 ms |
93456 KB |
Output is correct |
81 |
Correct |
750 ms |
93496 KB |
Output is correct |
82 |
Correct |
728 ms |
93560 KB |
Output is correct |
83 |
Correct |
721 ms |
93444 KB |
Output is correct |
84 |
Correct |
713 ms |
92436 KB |
Output is correct |
85 |
Correct |
728 ms |
93568 KB |
Output is correct |
86 |
Correct |
201 ms |
26504 KB |
Output is correct |
87 |
Correct |
2 ms |
376 KB |
Output is correct |