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>
#define ll long long
#define ull unsigned long long
#define ss second
#define ff first
#define pb push_back
#define pii pair<int, int>
#define INF INT_MAX
#define debug(n) cout << #n << " = " << n << "\n";
#define FAST ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const ll MOD = 1e9 + 7;
int main() {
ll n, A, B, T, K;
cin >> n >> A >> B;
K = A / __gcd(B + 1, A);
T = K * B;
map<ll, ll> m;
for(int i = 0; i < n; i++) {
ll l, r;
cin >> l >> r;
if(r - l + 1 >= T) {
cout << T << "\n";
return 0;
}
if(r % T < l % T) {
m[0]++;
m[r % T + 1]--;
m[l]++;
m[T]--;
} else {
m[l % T]++;
m[r % T + 1]--;
}
}
ll prev = 0;
vector<pair<ll, ll> > segments;
for(auto el : m) {
prev += el.ss;
segments.pb({el.ff, prev});
}
ll ans = 0;
for(int i = 0; i < int(segments.size() - 1); i++) {
if(segments[i].ss > 0){
ans += segments[i + 1].ff - segments[i].ff;
}
}
cout << ans << "\n";
return 0;
}
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
*/
# | 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... |