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 OPTM ios_base::sync_with_stdio(0); cin.tie(0);
#define INF int(1e9+7)
#define ln '\n'
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define us unsigned short
#define FOR(i,s,n) for (int i = s; i < n; i++)
#define FORR(i,n,s) for (int i = n; i > s; i--)
#define FORX(u, arr) for (auto u : arr)
#define PB push_back
#define in(v,x) (v.find(x) != v.end())
#define F first
#define S second
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define UM unordered_map
#define US unordered_set
#define PQ priority_queue
#define ALL(v) v.begin(), v.end()
const ll LLINF = 1e18+1;
ll n,a,b;
vector<PLL> v;
ll gcd(ll a, ll b) {
return (b == 0) ? a : gcd(b,a%b);
}
int main() {
//freopen("","r",stdin);
//freopen("","w",stdout);
OPTM;
cin >> n >> a >> b;
ll p = a*b/gcd(a,b+1);
FOR(i,0,n) {
ll l,r; cin >> l >> r;
ll c = l%p, d = r%p;
if (r-l+1 >= p) return p;
if (d >= c) v.PB({c,d});
else {
v.PB({c,p-1});
v.PB({0,d});
}
}
ll res = 0;
sort(ALL(v));
ll vn = v.size();
ll s = v[0].F, e = v[0].S;
FOR(i,1,vn) {
if (v[i].F <= e) e = max(e,v[i].S);
else {
res += e-s+1;
s = v[i].F;
e = v[i].S;
}
}
res += e-s+1;
cout << res;
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... |