#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
#define pb push_back
#define f first
#define s second
ll N, A, B;
int main() {
cin >> N >> A >> B;
ll gcd = __gcd(A, B + 1);
ll AP = A / gcd;
if (AP > (ll) 1e18 / B) {
// period is so big it won't matter
ll ans = 0;
for (int i = 0; i < N; i++) {
ll l, r;
cin >> l >> r;
ans += (r - l + 1);
}
cout << ans << endl;
return 0;
}
ll P = AP * B;
vector<pl> delts;
for (int i = 0; i < N; i++) {
ll l, r;
cin >> l >> r;
if (r - l + 1 >= P) {
cout << P << endl;
return 0;
}
l %= P;
r %= P;
if (l <= r) {
delts.pb({l, +1});
delts.pb({r + 1, -1});
} else {
delts.pb({l, +1});
delts.pb({P, -1});
delts.pb({0, +1});
delts.pb({r + 1, -1});
}
}
sort(delts.begin(), delts.end());
int cAct = 0;
ll ans = 0;
for (int i = 0; i < delts.size(); i++) {
cAct += delts[i].s;
if (i + 1 < delts.size()) {
ll dist = delts[i + 1].f - delts[i].f;
if (cAct > 0) {
ans += dist;
}
}
}
cout << ans << endl;
}
Compilation message
strange_device.cpp: In function 'int main()':
strange_device.cpp:67:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < delts.size(); i++) {
~~^~~~~~~~~~~~~~
strange_device.cpp:69:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (i + 1 < delts.size()) {
~~~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
29 ms |
1012 KB |
Output is correct |
3 |
Correct |
30 ms |
1012 KB |
Output is correct |
4 |
Correct |
2 ms |
128 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
2 ms |
256 KB |
Output is correct |
7 |
Correct |
2 ms |
256 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
2 ms |
256 KB |
Output is correct |
10 |
Correct |
2 ms |
256 KB |
Output is correct |
11 |
Correct |
2 ms |
256 KB |
Output is correct |
12 |
Correct |
2 ms |
256 KB |
Output is correct |
13 |
Correct |
2 ms |
252 KB |
Output is correct |
14 |
Correct |
2 ms |
256 KB |
Output is correct |
15 |
Correct |
2 ms |
256 KB |
Output is correct |
16 |
Correct |
29 ms |
988 KB |
Output is correct |
17 |
Correct |
289 ms |
4568 KB |
Output is correct |
18 |
Correct |
2 ms |
380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
4 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
1945 ms |
33468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2792 ms |
33272 KB |
Output is correct |
3 |
Correct |
2849 ms |
33296 KB |
Output is correct |
4 |
Correct |
2825 ms |
33272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2792 ms |
33272 KB |
Output is correct |
3 |
Correct |
2849 ms |
33296 KB |
Output is correct |
4 |
Correct |
2825 ms |
33272 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
2832 ms |
33324 KB |
Output is correct |
7 |
Correct |
2780 ms |
33344 KB |
Output is correct |
8 |
Correct |
2792 ms |
33252 KB |
Output is correct |
9 |
Correct |
2922 ms |
33224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2792 ms |
33272 KB |
Output is correct |
3 |
Correct |
2849 ms |
33296 KB |
Output is correct |
4 |
Correct |
2825 ms |
33272 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
275 ms |
4616 KB |
Output is correct |
7 |
Correct |
282 ms |
4712 KB |
Output is correct |
8 |
Correct |
285 ms |
4600 KB |
Output is correct |
9 |
Correct |
327 ms |
4544 KB |
Output is correct |
10 |
Correct |
273 ms |
4552 KB |
Output is correct |
11 |
Correct |
294 ms |
4556 KB |
Output is correct |
12 |
Correct |
277 ms |
4568 KB |
Output is correct |
13 |
Correct |
286 ms |
4584 KB |
Output is correct |
14 |
Correct |
275 ms |
4596 KB |
Output is correct |
15 |
Correct |
288 ms |
4572 KB |
Output is correct |
16 |
Correct |
286 ms |
4536 KB |
Output is correct |
17 |
Correct |
285 ms |
4628 KB |
Output is correct |
18 |
Correct |
2869 ms |
33244 KB |
Output is correct |
19 |
Correct |
2756 ms |
33292 KB |
Output is correct |
20 |
Correct |
2909 ms |
33416 KB |
Output is correct |
21 |
Correct |
290 ms |
4536 KB |
Output is correct |
22 |
Correct |
273 ms |
4572 KB |
Output is correct |
23 |
Correct |
905 ms |
16828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
291 ms |
4592 KB |
Output is correct |
3 |
Correct |
285 ms |
4580 KB |
Output is correct |
4 |
Correct |
2992 ms |
33260 KB |
Output is correct |
5 |
Correct |
286 ms |
4556 KB |
Output is correct |
6 |
Correct |
293 ms |
4572 KB |
Output is correct |
7 |
Correct |
287 ms |
4528 KB |
Output is correct |
8 |
Correct |
287 ms |
4544 KB |
Output is correct |
9 |
Correct |
282 ms |
4552 KB |
Output is correct |
10 |
Correct |
288 ms |
4564 KB |
Output is correct |
11 |
Correct |
284 ms |
4580 KB |
Output is correct |
12 |
Correct |
278 ms |
4588 KB |
Output is correct |
13 |
Correct |
289 ms |
4576 KB |
Output is correct |
14 |
Correct |
2906 ms |
33296 KB |
Output is correct |
15 |
Correct |
289 ms |
4548 KB |
Output is correct |
16 |
Correct |
2744 ms |
33360 KB |
Output is correct |
17 |
Correct |
2799 ms |
33284 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
29 ms |
1012 KB |
Output is correct |
3 |
Correct |
30 ms |
1012 KB |
Output is correct |
4 |
Correct |
2 ms |
128 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
2 ms |
256 KB |
Output is correct |
7 |
Correct |
2 ms |
256 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
2 ms |
256 KB |
Output is correct |
10 |
Correct |
2 ms |
256 KB |
Output is correct |
11 |
Correct |
2 ms |
256 KB |
Output is correct |
12 |
Correct |
2 ms |
256 KB |
Output is correct |
13 |
Correct |
2 ms |
252 KB |
Output is correct |
14 |
Correct |
2 ms |
256 KB |
Output is correct |
15 |
Correct |
2 ms |
256 KB |
Output is correct |
16 |
Correct |
29 ms |
988 KB |
Output is correct |
17 |
Correct |
289 ms |
4568 KB |
Output is correct |
18 |
Correct |
2 ms |
380 KB |
Output is correct |
19 |
Correct |
2 ms |
256 KB |
Output is correct |
20 |
Correct |
3 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
256 KB |
Output is correct |
23 |
Correct |
2 ms |
292 KB |
Output is correct |
24 |
Correct |
2 ms |
256 KB |
Output is correct |
25 |
Correct |
4 ms |
376 KB |
Output is correct |
26 |
Correct |
5 ms |
376 KB |
Output is correct |
27 |
Correct |
5 ms |
376 KB |
Output is correct |
28 |
Correct |
1945 ms |
33468 KB |
Output is correct |
29 |
Correct |
2 ms |
256 KB |
Output is correct |
30 |
Correct |
2792 ms |
33272 KB |
Output is correct |
31 |
Correct |
2849 ms |
33296 KB |
Output is correct |
32 |
Correct |
2825 ms |
33272 KB |
Output is correct |
33 |
Correct |
1 ms |
384 KB |
Output is correct |
34 |
Correct |
2832 ms |
33324 KB |
Output is correct |
35 |
Correct |
2780 ms |
33344 KB |
Output is correct |
36 |
Correct |
2792 ms |
33252 KB |
Output is correct |
37 |
Correct |
2922 ms |
33224 KB |
Output is correct |
38 |
Correct |
2 ms |
376 KB |
Output is correct |
39 |
Correct |
275 ms |
4616 KB |
Output is correct |
40 |
Correct |
282 ms |
4712 KB |
Output is correct |
41 |
Correct |
285 ms |
4600 KB |
Output is correct |
42 |
Correct |
327 ms |
4544 KB |
Output is correct |
43 |
Correct |
273 ms |
4552 KB |
Output is correct |
44 |
Correct |
294 ms |
4556 KB |
Output is correct |
45 |
Correct |
277 ms |
4568 KB |
Output is correct |
46 |
Correct |
286 ms |
4584 KB |
Output is correct |
47 |
Correct |
275 ms |
4596 KB |
Output is correct |
48 |
Correct |
288 ms |
4572 KB |
Output is correct |
49 |
Correct |
286 ms |
4536 KB |
Output is correct |
50 |
Correct |
285 ms |
4628 KB |
Output is correct |
51 |
Correct |
2869 ms |
33244 KB |
Output is correct |
52 |
Correct |
2756 ms |
33292 KB |
Output is correct |
53 |
Correct |
2909 ms |
33416 KB |
Output is correct |
54 |
Correct |
290 ms |
4536 KB |
Output is correct |
55 |
Correct |
273 ms |
4572 KB |
Output is correct |
56 |
Correct |
905 ms |
16828 KB |
Output is correct |
57 |
Correct |
2 ms |
376 KB |
Output is correct |
58 |
Correct |
291 ms |
4592 KB |
Output is correct |
59 |
Correct |
285 ms |
4580 KB |
Output is correct |
60 |
Correct |
2992 ms |
33260 KB |
Output is correct |
61 |
Correct |
286 ms |
4556 KB |
Output is correct |
62 |
Correct |
293 ms |
4572 KB |
Output is correct |
63 |
Correct |
287 ms |
4528 KB |
Output is correct |
64 |
Correct |
287 ms |
4544 KB |
Output is correct |
65 |
Correct |
282 ms |
4552 KB |
Output is correct |
66 |
Correct |
288 ms |
4564 KB |
Output is correct |
67 |
Correct |
284 ms |
4580 KB |
Output is correct |
68 |
Correct |
278 ms |
4588 KB |
Output is correct |
69 |
Correct |
289 ms |
4576 KB |
Output is correct |
70 |
Correct |
2906 ms |
33296 KB |
Output is correct |
71 |
Correct |
289 ms |
4548 KB |
Output is correct |
72 |
Correct |
2744 ms |
33360 KB |
Output is correct |
73 |
Correct |
2799 ms |
33284 KB |
Output is correct |
74 |
Correct |
2 ms |
376 KB |
Output is correct |
75 |
Correct |
2 ms |
252 KB |
Output is correct |
76 |
Correct |
2 ms |
256 KB |
Output is correct |
77 |
Correct |
2 ms |
256 KB |
Output is correct |
78 |
Correct |
2 ms |
504 KB |
Output is correct |
79 |
Correct |
30 ms |
1012 KB |
Output is correct |
80 |
Correct |
3077 ms |
33336 KB |
Output is correct |
81 |
Correct |
3028 ms |
33256 KB |
Output is correct |
82 |
Correct |
2882 ms |
33308 KB |
Output is correct |
83 |
Correct |
2898 ms |
33288 KB |
Output is correct |
84 |
Correct |
2904 ms |
33268 KB |
Output is correct |
85 |
Correct |
2901 ms |
33352 KB |
Output is correct |
86 |
Correct |
899 ms |
16876 KB |
Output is correct |
87 |
Correct |
2 ms |
256 KB |
Output is correct |