#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define pb push_back
#define fi first
#define se second
#define debug(x) cout << #x << " => " << x << "\n";
const long long mod = 1e9 + 7;
const long long MAX = 1e18;
int n, a, b, L[1000005], R[1000005], s, m;
vector <pair<int,int>> ls;
int cnt, prv, ans;
void solve () {
cin >> n >> a >> b;
for(int i = 1; i <= n; i++) {
cin >> L[i] >> R[i];
s += R[i] - L[i] + 1;
}
if(MAX / a < b) {
return;
}
m = a * b / __gcd(a, b + 1);
for(int i = 1; i <= n; i++) {
if(R[i] - L[i] + 1 >= m) {
cout << m << "\n";
return;
}
}
for(int i = 1; i <= n; i++) {
L[i] %= m;
R[i] %= m;
if(R[i] < L[i]) {
ls.pb({L[i], 0});
ls.pb({m, 1});
ls.pb({0, 0});
ls.pb({R[i] + 1, 1});
}
else {
ls.pb({L[i], 0});
ls.pb({R[i] + 1, 1});
}
}
sort(ls.begin(), ls.end());
cnt = 0, prv = -1;
// for(auto i : ls) cout << i.fi << " ";
// cout << "\n";
for(int i = 0; i < ls.size(); i++) {
if(ls[i].se == 0) cnt++;
else cnt--;
if(i < ls.size() - 1 && ls[i].fi == ls[i + 1].fi) continue;
if(cnt > 0 && prv == -1) {prv = ls[i].fi;}
if(cnt == 0 && prv != -1) {ans += ls[i].fi - prv; prv = -1;}
}
if(prv != -1) ans += m - prv;
cout << ans << "\n";
}
signed main () {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
}
Compilation message
strange_device.cpp: In function 'void solve()':
strange_device.cpp:61:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | for(int i = 0; i < ls.size(); i++) {
| ~~^~~~~~~~~~~
strange_device.cpp:65:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | if(i < ls.size() - 1 && ls[i].fi == ls[i + 1].fi) continue;
| ~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
6 ms |
1240 KB |
Output is correct |
3 |
Correct |
8 ms |
1236 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
328 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
328 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
6 ms |
1280 KB |
Output is correct |
17 |
Correct |
68 ms |
6220 KB |
Output is correct |
18 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
19 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
394 ms |
49324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
593 ms |
49476 KB |
Output is correct |
3 |
Correct |
579 ms |
49456 KB |
Output is correct |
4 |
Correct |
538 ms |
55988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
593 ms |
49476 KB |
Output is correct |
3 |
Correct |
579 ms |
49456 KB |
Output is correct |
4 |
Correct |
538 ms |
55988 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
561 ms |
54764 KB |
Output is correct |
7 |
Correct |
619 ms |
55076 KB |
Output is correct |
8 |
Correct |
578 ms |
54552 KB |
Output is correct |
9 |
Correct |
654 ms |
55532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
593 ms |
49476 KB |
Output is correct |
3 |
Correct |
579 ms |
49456 KB |
Output is correct |
4 |
Correct |
538 ms |
55988 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
51 ms |
6496 KB |
Output is correct |
7 |
Correct |
58 ms |
6512 KB |
Output is correct |
8 |
Correct |
52 ms |
6468 KB |
Output is correct |
9 |
Correct |
65 ms |
6516 KB |
Output is correct |
10 |
Correct |
99 ms |
6528 KB |
Output is correct |
11 |
Correct |
52 ms |
6496 KB |
Output is correct |
12 |
Correct |
54 ms |
6480 KB |
Output is correct |
13 |
Correct |
60 ms |
6440 KB |
Output is correct |
14 |
Correct |
50 ms |
6448 KB |
Output is correct |
15 |
Correct |
65 ms |
6436 KB |
Output is correct |
16 |
Correct |
64 ms |
6500 KB |
Output is correct |
17 |
Correct |
51 ms |
6500 KB |
Output is correct |
18 |
Correct |
629 ms |
54540 KB |
Output is correct |
19 |
Correct |
549 ms |
56108 KB |
Output is correct |
20 |
Correct |
664 ms |
55252 KB |
Output is correct |
21 |
Correct |
74 ms |
6464 KB |
Output is correct |
22 |
Correct |
61 ms |
6496 KB |
Output is correct |
23 |
Correct |
154 ms |
22508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
62 ms |
6552 KB |
Output is correct |
3 |
Correct |
58 ms |
6636 KB |
Output is correct |
4 |
Correct |
644 ms |
49820 KB |
Output is correct |
5 |
Correct |
99 ms |
6596 KB |
Output is correct |
6 |
Correct |
63 ms |
6652 KB |
Output is correct |
7 |
Correct |
59 ms |
6628 KB |
Output is correct |
8 |
Correct |
62 ms |
6576 KB |
Output is correct |
9 |
Correct |
57 ms |
6588 KB |
Output is correct |
10 |
Correct |
65 ms |
6548 KB |
Output is correct |
11 |
Correct |
78 ms |
6616 KB |
Output is correct |
12 |
Correct |
62 ms |
6596 KB |
Output is correct |
13 |
Correct |
65 ms |
6652 KB |
Output is correct |
14 |
Correct |
663 ms |
50104 KB |
Output is correct |
15 |
Correct |
72 ms |
6732 KB |
Output is correct |
16 |
Correct |
485 ms |
49672 KB |
Output is correct |
17 |
Correct |
564 ms |
50652 KB |
Output is correct |
18 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
19 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
6 ms |
1240 KB |
Output is correct |
3 |
Correct |
8 ms |
1236 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
328 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
328 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
6 ms |
1280 KB |
Output is correct |
17 |
Correct |
68 ms |
6220 KB |
Output is correct |
18 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
19 |
Halted |
0 ms |
0 KB |
- |