This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
*/
// lethal option
#include<bits/stdc++.h>
using namespace std;
#define all(flg) flg.begin(), flg.end()
#define int long long
#define pb push_back
#define fi first
#define se second
#define endl "\n"
#define eb emplace_back
#define ii pair<int, int>
#define PI 3.141592653589793238462643383279502884
#define ll long long
#define for1(i, ff, gg) for(int i = ff; i <= gg; ++i)
#define for2(i, ff, gg) for(int i = ff; i >= gg; --i)
const ll mod = 1e9 + 7;
const int maxN = 1000005;
const ll oo = 2e18 + 7;
int n, a[maxN];
int x, y, z, k;
ii pr[maxN];
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> x >> y;
x = (x / __gcd(x, y + 1));
z = y * x;
if(x < y) swap(x, y);
if(oo / x <= y) z = oo;
assert(z >= 0);
set<ii> st;
for1(i, 1, n){
cin >> x >> y;
if(y - x + 1 >= z){
cout << z << endl;
exit(0);
}
pr[i].fi = x % z;
pr[i].se = y % z;
}
for1(i, 1, n){
if(pr[i].fi > pr[i].se){
++n;
pr[n].se = pr[i].se;
pr[n].fi = 0;
pr[i].se = z - 1;
}
}
int lmx = -oo;
sort(pr + 1, pr + n + 1);
int ans = 0;
for1(i, 1, n){
lmx = max(lmx, pr[i].fi - 1);
ans += max(0ll, pr[i].se - lmx);
lmx = max(lmx, pr[i].se);
}
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... |