This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define ioss ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define int long long
#define pii pair<int, int>
#define fi first
#define se second
#define pb push_back
int n, a, b;
vector<pii> range;
bool comp(pii a, pii b) {
return (a.fi < b.fi || (a.fi == b.fi && a.se < b.se));
}
signed main() {
ioss;
// cari cycle (bisa manual, baru cari pola)
// --- a*b / fpb(a, b+1)
// hitung segment aktif
// line sweep
cin >> n >> a >> b;
__int128_t cycle = (__int128_t)a*b/(__gcd(a, (b+1)));
int ans = 0;
for(int i = 0; i < n; i++) {
int l, r; cin >> l >> r;
if((r-l)+1 >= cycle) ans = cycle;
l %= cycle, r %= cycle;
if(l <= r) range.pb({l, 1}), range.pb({r, 2});
else range.pb({l, 1}), range.pb({cycle-1, 2}), range.pb({0, 1}), range.pb({r, 2});
}
sort(range.begin(), range.end(), comp);
if(ans == cycle) {
cout << ans << endl;
return 0;
}
int l = -1, cnt = 0, r = 0;
for(auto [val, type] : range) {
if(type == 1) {
if(l == -1) l = val;
cnt++;
}
else r = val, cnt--;
if(cnt == 0) ans += (r-l)+1, l = -1;
// cout << " :: " << l << " " << r << " " << cnt << " " << ans << endl;
}
cout << ans << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |