#include<bits/stdc++.h>
using namespace std;
long long gcd(long long x, long long y) {
if (y == 0) return x;
return gcd(y, x%y);
}
void PlayGround() {
long long n, a, b;
cin>>n>>a>>b;
vector<array<long long,2>>iner(n);
for(int i=0; i<n; ++i) {
cin>>iner[i][0]>>iner[i][1];
}
long long g = gcd(a, b+1);
long long interval = (a / gcd(a, b+1));
bool toobig = false;
if(log10(interval)+log10(b)>18) {
toobig = true;
} else {
interval *= b;
}
for(int i=0; i<n; ++i) {
long long len = iner[i][1] - iner[i][0] + 1;
if(!toobig && len>=interval) {
cout<<interval<<'\n';
return;
}
}
set<pair<long long, long long>>s;
for(int i=0; i<n; ++i) {
long long len = iner[i][1]-iner[i][0]+1;
long long l = iner[i][0]%interval;
long long r = iner[i][1]%interval;
if(!toobig) {
if(l>r) {
s.insert({l, interval-1});
s.insert({0, r});
} else {
s.insert({l, r});
}
} else {
s.insert({l, r});
}
}
long long ans = 0;
while(!s.empty()) {
auto it = s.begin();
s.erase(it);
long long l = it->first, r = it->second;
if(!s.empty()) {
auto nx = s.upper_bound(*it);
if(nx->second>=r) r = min(r, nx->first-1);
else {
s.erase(nx);
s.insert(*it);
continue;
}
}
ans += max(0LL, r-l+1);
}
cout<<ans<<'\n';
// cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
PlayGround();
return 0;
}
Compilation message
strange_device.cpp: In function 'void PlayGround()':
strange_device.cpp:38:15: warning: unused variable 'len' [-Wunused-variable]
38 | long long len = iner[i][1]-iner[i][0]+1;
| ^~~
strange_device.cpp:17:13: warning: unused variable 'g' [-Wunused-variable]
17 | long long g = gcd(a, b+1);
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
5 ms |
1108 KB |
Output is correct |
3 |
Correct |
5 ms |
1108 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
5 ms |
1108 KB |
Output is correct |
17 |
Correct |
55 ms |
8020 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 |
0 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 |
244 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 |
244 ms |
15956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
528 ms |
78504 KB |
Output is correct |
3 |
Correct |
574 ms |
78896 KB |
Output is correct |
4 |
Correct |
618 ms |
78580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
528 ms |
78504 KB |
Output is correct |
3 |
Correct |
574 ms |
78896 KB |
Output is correct |
4 |
Correct |
618 ms |
78580 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
560 ms |
78528 KB |
Output is correct |
7 |
Correct |
570 ms |
78584 KB |
Output is correct |
8 |
Correct |
570 ms |
78704 KB |
Output is correct |
9 |
Correct |
670 ms |
78564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
528 ms |
78504 KB |
Output is correct |
3 |
Correct |
574 ms |
78896 KB |
Output is correct |
4 |
Correct |
618 ms |
78580 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
48 ms |
8140 KB |
Output is correct |
7 |
Correct |
50 ms |
8104 KB |
Output is correct |
8 |
Correct |
51 ms |
8036 KB |
Output is correct |
9 |
Correct |
57 ms |
8080 KB |
Output is correct |
10 |
Correct |
49 ms |
8012 KB |
Output is correct |
11 |
Correct |
50 ms |
8036 KB |
Output is correct |
12 |
Correct |
51 ms |
8032 KB |
Output is correct |
13 |
Correct |
56 ms |
8020 KB |
Output is correct |
14 |
Correct |
51 ms |
8140 KB |
Output is correct |
15 |
Correct |
66 ms |
8024 KB |
Output is correct |
16 |
Correct |
60 ms |
11852 KB |
Output is correct |
17 |
Correct |
55 ms |
11844 KB |
Output is correct |
18 |
Correct |
586 ms |
115672 KB |
Output is correct |
19 |
Correct |
639 ms |
115608 KB |
Output is correct |
20 |
Correct |
713 ms |
115616 KB |
Output is correct |
21 |
Correct |
54 ms |
11856 KB |
Output is correct |
22 |
Correct |
56 ms |
11984 KB |
Output is correct |
23 |
Correct |
110 ms |
18252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
73 ms |
8028 KB |
Output is correct |
3 |
Correct |
59 ms |
8024 KB |
Output is correct |
4 |
Correct |
653 ms |
78508 KB |
Output is correct |
5 |
Correct |
60 ms |
8188 KB |
Output is correct |
6 |
Correct |
57 ms |
8156 KB |
Output is correct |
7 |
Correct |
59 ms |
8160 KB |
Output is correct |
8 |
Correct |
61 ms |
8268 KB |
Output is correct |
9 |
Correct |
58 ms |
8156 KB |
Output is correct |
10 |
Correct |
55 ms |
8152 KB |
Output is correct |
11 |
Correct |
55 ms |
8152 KB |
Output is correct |
12 |
Correct |
53 ms |
8164 KB |
Output is correct |
13 |
Correct |
58 ms |
8268 KB |
Output is correct |
14 |
Correct |
667 ms |
78524 KB |
Output is correct |
15 |
Correct |
57 ms |
11852 KB |
Output is correct |
16 |
Correct |
600 ms |
115664 KB |
Output is correct |
17 |
Correct |
596 ms |
115676 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
5 ms |
1108 KB |
Output is correct |
3 |
Correct |
5 ms |
1108 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
5 ms |
1108 KB |
Output is correct |
17 |
Correct |
55 ms |
8020 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 |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
244 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 |
244 ms |
15956 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
528 ms |
78504 KB |
Output is correct |
31 |
Correct |
574 ms |
78896 KB |
Output is correct |
32 |
Correct |
618 ms |
78580 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
560 ms |
78528 KB |
Output is correct |
35 |
Correct |
570 ms |
78584 KB |
Output is correct |
36 |
Correct |
570 ms |
78704 KB |
Output is correct |
37 |
Correct |
670 ms |
78564 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
48 ms |
8140 KB |
Output is correct |
40 |
Correct |
50 ms |
8104 KB |
Output is correct |
41 |
Correct |
51 ms |
8036 KB |
Output is correct |
42 |
Correct |
57 ms |
8080 KB |
Output is correct |
43 |
Correct |
49 ms |
8012 KB |
Output is correct |
44 |
Correct |
50 ms |
8036 KB |
Output is correct |
45 |
Correct |
51 ms |
8032 KB |
Output is correct |
46 |
Correct |
56 ms |
8020 KB |
Output is correct |
47 |
Correct |
51 ms |
8140 KB |
Output is correct |
48 |
Correct |
66 ms |
8024 KB |
Output is correct |
49 |
Correct |
60 ms |
11852 KB |
Output is correct |
50 |
Correct |
55 ms |
11844 KB |
Output is correct |
51 |
Correct |
586 ms |
115672 KB |
Output is correct |
52 |
Correct |
639 ms |
115608 KB |
Output is correct |
53 |
Correct |
713 ms |
115616 KB |
Output is correct |
54 |
Correct |
54 ms |
11856 KB |
Output is correct |
55 |
Correct |
56 ms |
11984 KB |
Output is correct |
56 |
Correct |
110 ms |
18252 KB |
Output is correct |
57 |
Correct |
0 ms |
212 KB |
Output is correct |
58 |
Correct |
73 ms |
8028 KB |
Output is correct |
59 |
Correct |
59 ms |
8024 KB |
Output is correct |
60 |
Correct |
653 ms |
78508 KB |
Output is correct |
61 |
Correct |
60 ms |
8188 KB |
Output is correct |
62 |
Correct |
57 ms |
8156 KB |
Output is correct |
63 |
Correct |
59 ms |
8160 KB |
Output is correct |
64 |
Correct |
61 ms |
8268 KB |
Output is correct |
65 |
Correct |
58 ms |
8156 KB |
Output is correct |
66 |
Correct |
55 ms |
8152 KB |
Output is correct |
67 |
Correct |
55 ms |
8152 KB |
Output is correct |
68 |
Correct |
53 ms |
8164 KB |
Output is correct |
69 |
Correct |
58 ms |
8268 KB |
Output is correct |
70 |
Correct |
667 ms |
78524 KB |
Output is correct |
71 |
Correct |
57 ms |
11852 KB |
Output is correct |
72 |
Correct |
600 ms |
115664 KB |
Output is correct |
73 |
Correct |
596 ms |
115676 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 |
320 KB |
Output is correct |
78 |
Correct |
1 ms |
212 KB |
Output is correct |
79 |
Correct |
6 ms |
1480 KB |
Output is correct |
80 |
Correct |
641 ms |
115796 KB |
Output is correct |
81 |
Correct |
625 ms |
115656 KB |
Output is correct |
82 |
Correct |
712 ms |
115864 KB |
Output is correct |
83 |
Correct |
700 ms |
115720 KB |
Output is correct |
84 |
Correct |
676 ms |
115872 KB |
Output is correct |
85 |
Correct |
658 ms |
115696 KB |
Output is correct |
86 |
Correct |
124 ms |
18184 KB |
Output is correct |
87 |
Correct |
1 ms |
212 KB |
Output is correct |