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 IOS ios_base::sync_with_stdio(false);cin.tie();cout.tie();
#define all(x) x.begin(), x.end()
#define int long long
#define pi pair<int, int >
#define pq priority_queue
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define eb emplace_back
#define mset multiset
#define F first
#define S second
using namespace std;
int n, A, B, range;
vector<pair<int, int>> v;
int gcd(int a, int b) {return (!a ? b : gcd(b % a, a));}
void build() {
int L = gcd(B+1, A); // (a/l * b)
range = (1e18 / B > A / L ? (A / L) * B : 1e18);
}
void calc(int l, int r) {
if (r - l > range) {v.pb({1, range}); return ;}
int s = l / range;
l %= range ;
r -= s * range;
if (r > range) r -= range;
v.pb({l, 1});
v.pb({r, 2});
}
int count() {
sort(all(v));
int ans = 0, se = 0, si = 0;
for (auto i: v) {
if (i.S & 1) {
if (i.F > 0 && i.F <= se) ans += i.F - si + 1;
}
else {
se = i.F;
ans += i.F - si + 1;
}
si = i.F;
}
return ans;
}
void solve ()
{
cin >> n >> A >> B;
build();
for (int i = 1; i <= n; i++) {
int l, r; cin >> l >> r;
calc(l, r);
}
cout << count();
}
signed main(){IOS solve(); return 0;}
# | 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... |