#include<bits/stdc++.h>
using namespace std;
using ll = long long;
ll n, A, B;
vector<pair<ll, ll>> segs;
void solve2() {
assert(0);
ll ans = 0;
for(auto & [l, r] : segs) {
cin >> l >> r;
ans += r - l + 1;
}
cout << ans << endl;
}
//x*B st: x(B+1)%A == 0
main() {
cin >> n >> A >> B;
segs.resize(n);
ll cycle = 1e18;
ll C = A / (__gcd(B + 1, A));
if((A * C) / C == A) cycle = A * C;
else {
solve2();
return 0;
}
vector<pair<ll, bool>> evt;
for(auto & [l, r] : segs) {
cin >> l >> r;
r++;
if(l >= cycle) {
ll tmp = (l / cycle) * cycle;
l -= tmp;
r -= tmp;
}
if(r >= cycle*2) {
cout << cycle << endl;
return 0;
}
if(r >= cycle) {
evt.push_back({l, true});
evt.push_back({0, true});
evt.push_back({r - cycle, false});
} else {
evt.push_back({l, true});
evt.push_back({r, false});
}
}
sort(evt.begin(), evt.end());
ll prev = 0;
int cnt = 0;
ll ans = 0;
for(auto [t, b] : evt) {
ll delta = t - prev;
prev = t;
if(cnt > 0) ans += delta;
if(b) cnt++;
else cnt--;
}
if(cnt > 0) ans += cycle - prev;
cout << ans << endl;
}
Compilation message
strange_device.cpp:17:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
17 | main() {
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
14 ms |
1072 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
2 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1443 ms |
49004 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1443 ms |
49004 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1443 ms |
49004 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
142 ms |
6120 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
14 ms |
1072 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |