#include <bits/stdc++.h>
#define ll long long
#define lll __int128
#define endl "\n"
using namespace std;
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
ll n, a, b;
cin >> n >> a >> b;
// a or b has certain pattern
// ab -> 1 cycle, no duplikat
// next cycle: sama persis
// maintain cycles in terms of ab, merge all segments
// find modulo ab
// exist more than one cycle long -> all possible
// size ab berarti diff >= ab - 1
vector<pair<ll, int>> sweep;
ll ans = -1;
ll tot = a;
for(int i = 1; i <= n; ++i) {
ll l, r;
cin >> l >> r;
if(r - l >= tot - 1) {
ans = tot;
}
else {
if(tot <= 1e18) {
l = (l % tot);
r = (r % tot);
}
if(l <= r) {
sweep.push_back(make_pair(l, 0));
sweep.push_back(make_pair(r, 1));
}
else {
sweep.push_back(make_pair(l, 0));
sweep.push_back(make_pair(tot - 1, 1));
sweep.push_back(make_pair(0, 0));
sweep.push_back(make_pair(r, 1));
}
}
}
if(ans != -1)
cout << ans << endl, exit(0);
sort(sweep.begin(), sweep.end());
int cnt = 0;
ll st = -5;
ans = 0;
for(auto i : sweep) {
if(i.second) {
// end
--cnt;
if(cnt == 0)
ans += i.first - st;
}
else {
// start
// start itu + cnt
// set start kalo belum di set
if(cnt == 0)
st = i.first - 1;
++cnt;
}
}
cout << ans << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
5 ms |
1240 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
521 ms |
34012 KB |
Output is correct |
3 |
Incorrect |
492 ms |
34132 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
521 ms |
34012 KB |
Output is correct |
3 |
Incorrect |
492 ms |
34132 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
521 ms |
34012 KB |
Output is correct |
3 |
Incorrect |
492 ms |
34132 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
41 ms |
4960 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
5 ms |
1240 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |