#include<bits/stdc++.h>
using namespace std;
#define int long long
int len;
bool merged[2000100];
struct range {
int l, r;
bool operator < (range against) {
return l < against.l;
}
};
vector<range> ranges;
int gcd(int a, int b) {
if(!b)
return a;
return gcd(b, a%b);
}
signed main() {
cin.sync_with_stdio(false);
cin.tie(nullptr);
int n, a, b;
cin >> n >> a >> b;
__int128_t temp = b;
temp*=a/gcd(a, b+1);
if(temp>1e18)
len = 2e18;
else
len = temp;
for(int i = 0; i < n; i++) {
int l, r;
cin >> l >> r;
if(r-l+1 >= len) {
cout << len << '\n';
return 0;
}
l%=len, r%=len;
if(l > r) {
ranges.push_back({l, len-1});
ranges.push_back({0, r});
} else {
ranges.push_back({l, r});
}
}
sort(ranges.begin(), ranges.end());
for(int i = 1; i < ranges.size(); i++) {
if(ranges[i].l <= ranges[i-1].r)
ranges[i].l = ranges[i-1].l, merged[i-1] = 1, ranges[i].r = max(ranges[i].r, ranges[i-1].r);
}
int ans = 0;
for(int i = 0; i < ranges.size(); i++)
if(!merged[i])
ans+=ranges[i].r-ranges[i].l+1;
cout << ans << '\n';
}
Compilation message
strange_device.cpp: In function 'int main()':
strange_device.cpp:45:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<range>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for(int i = 1; i < ranges.size(); i++) {
| ~~^~~~~~~~~~~~~~~
strange_device.cpp:50:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<range>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
50 | for(int i = 0; i < ranges.size(); i++)
| ~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
4 ms |
708 KB |
Output is correct |
3 |
Correct |
5 ms |
1068 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
288 KB |
Output is correct |
13 |
Correct |
1 ms |
328 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
4 ms |
984 KB |
Output is correct |
17 |
Correct |
47 ms |
3248 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 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 |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
283 ms |
17956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
386 ms |
16768 KB |
Output is correct |
3 |
Correct |
374 ms |
16936 KB |
Output is correct |
4 |
Correct |
368 ms |
16860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
386 ms |
16768 KB |
Output is correct |
3 |
Correct |
374 ms |
16936 KB |
Output is correct |
4 |
Correct |
368 ms |
16860 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
398 ms |
16888 KB |
Output is correct |
7 |
Correct |
372 ms |
16940 KB |
Output is correct |
8 |
Correct |
371 ms |
16768 KB |
Output is correct |
9 |
Correct |
402 ms |
16800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
386 ms |
16768 KB |
Output is correct |
3 |
Correct |
374 ms |
16936 KB |
Output is correct |
4 |
Correct |
368 ms |
16860 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
44 ms |
2476 KB |
Output is correct |
7 |
Correct |
41 ms |
2508 KB |
Output is correct |
8 |
Correct |
37 ms |
2440 KB |
Output is correct |
9 |
Correct |
38 ms |
2500 KB |
Output is correct |
10 |
Correct |
37 ms |
2452 KB |
Output is correct |
11 |
Correct |
38 ms |
2420 KB |
Output is correct |
12 |
Correct |
37 ms |
2408 KB |
Output is correct |
13 |
Correct |
40 ms |
2472 KB |
Output is correct |
14 |
Correct |
38 ms |
2536 KB |
Output is correct |
15 |
Correct |
41 ms |
2512 KB |
Output is correct |
16 |
Correct |
45 ms |
4300 KB |
Output is correct |
17 |
Correct |
42 ms |
3988 KB |
Output is correct |
18 |
Correct |
404 ms |
17924 KB |
Output is correct |
19 |
Correct |
380 ms |
17632 KB |
Output is correct |
20 |
Correct |
427 ms |
17768 KB |
Output is correct |
21 |
Correct |
48 ms |
4128 KB |
Output is correct |
22 |
Correct |
41 ms |
3932 KB |
Output is correct |
23 |
Correct |
119 ms |
10200 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
41 ms |
2464 KB |
Output is correct |
3 |
Correct |
41 ms |
2504 KB |
Output is correct |
4 |
Correct |
454 ms |
16836 KB |
Output is correct |
5 |
Correct |
43 ms |
2444 KB |
Output is correct |
6 |
Correct |
40 ms |
2456 KB |
Output is correct |
7 |
Correct |
40 ms |
2424 KB |
Output is correct |
8 |
Correct |
42 ms |
2512 KB |
Output is correct |
9 |
Correct |
40 ms |
2512 KB |
Output is correct |
10 |
Correct |
40 ms |
2512 KB |
Output is correct |
11 |
Correct |
42 ms |
2512 KB |
Output is correct |
12 |
Correct |
36 ms |
2424 KB |
Output is correct |
13 |
Correct |
39 ms |
2496 KB |
Output is correct |
14 |
Correct |
405 ms |
17064 KB |
Output is correct |
15 |
Correct |
49 ms |
3980 KB |
Output is correct |
16 |
Correct |
395 ms |
18640 KB |
Output is correct |
17 |
Correct |
389 ms |
18568 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
4 ms |
708 KB |
Output is correct |
3 |
Correct |
5 ms |
1068 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
288 KB |
Output is correct |
13 |
Correct |
1 ms |
328 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
4 ms |
984 KB |
Output is correct |
17 |
Correct |
47 ms |
3248 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
283 ms |
17956 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
386 ms |
16768 KB |
Output is correct |
31 |
Correct |
374 ms |
16936 KB |
Output is correct |
32 |
Correct |
368 ms |
16860 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
398 ms |
16888 KB |
Output is correct |
35 |
Correct |
372 ms |
16940 KB |
Output is correct |
36 |
Correct |
371 ms |
16768 KB |
Output is correct |
37 |
Correct |
402 ms |
16800 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
44 ms |
2476 KB |
Output is correct |
40 |
Correct |
41 ms |
2508 KB |
Output is correct |
41 |
Correct |
37 ms |
2440 KB |
Output is correct |
42 |
Correct |
38 ms |
2500 KB |
Output is correct |
43 |
Correct |
37 ms |
2452 KB |
Output is correct |
44 |
Correct |
38 ms |
2420 KB |
Output is correct |
45 |
Correct |
37 ms |
2408 KB |
Output is correct |
46 |
Correct |
40 ms |
2472 KB |
Output is correct |
47 |
Correct |
38 ms |
2536 KB |
Output is correct |
48 |
Correct |
41 ms |
2512 KB |
Output is correct |
49 |
Correct |
45 ms |
4300 KB |
Output is correct |
50 |
Correct |
42 ms |
3988 KB |
Output is correct |
51 |
Correct |
404 ms |
17924 KB |
Output is correct |
52 |
Correct |
380 ms |
17632 KB |
Output is correct |
53 |
Correct |
427 ms |
17768 KB |
Output is correct |
54 |
Correct |
48 ms |
4128 KB |
Output is correct |
55 |
Correct |
41 ms |
3932 KB |
Output is correct |
56 |
Correct |
119 ms |
10200 KB |
Output is correct |
57 |
Correct |
0 ms |
212 KB |
Output is correct |
58 |
Correct |
41 ms |
2464 KB |
Output is correct |
59 |
Correct |
41 ms |
2504 KB |
Output is correct |
60 |
Correct |
454 ms |
16836 KB |
Output is correct |
61 |
Correct |
43 ms |
2444 KB |
Output is correct |
62 |
Correct |
40 ms |
2456 KB |
Output is correct |
63 |
Correct |
40 ms |
2424 KB |
Output is correct |
64 |
Correct |
42 ms |
2512 KB |
Output is correct |
65 |
Correct |
40 ms |
2512 KB |
Output is correct |
66 |
Correct |
40 ms |
2512 KB |
Output is correct |
67 |
Correct |
42 ms |
2512 KB |
Output is correct |
68 |
Correct |
36 ms |
2424 KB |
Output is correct |
69 |
Correct |
39 ms |
2496 KB |
Output is correct |
70 |
Correct |
405 ms |
17064 KB |
Output is correct |
71 |
Correct |
49 ms |
3980 KB |
Output is correct |
72 |
Correct |
395 ms |
18640 KB |
Output is correct |
73 |
Correct |
389 ms |
18568 KB |
Output is correct |
74 |
Correct |
1 ms |
212 KB |
Output is correct |
75 |
Correct |
1 ms |
212 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 |
984 KB |
Output is correct |
80 |
Correct |
447 ms |
16928 KB |
Output is correct |
81 |
Correct |
471 ms |
16840 KB |
Output is correct |
82 |
Correct |
416 ms |
16940 KB |
Output is correct |
83 |
Correct |
416 ms |
16908 KB |
Output is correct |
84 |
Correct |
413 ms |
17024 KB |
Output is correct |
85 |
Correct |
411 ms |
16928 KB |
Output is correct |
86 |
Correct |
126 ms |
10036 KB |
Output is correct |
87 |
Correct |
1 ms |
340 KB |
Output is correct |