This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/* made by amunduzbaev */
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define ub upper_bound
#define lb lower_bound
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(),x.rend()
#define NeedForSpeed ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define vv vector
#define tut(x) array<int, x>
#define int long long
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vii;
typedef vector<pii> vpii;
template<class T> bool umin(T& a, const T& b) { return a > b? a = b, true:false; }
template<class T> bool umax(T& a, const T& b) { return a < b? a = b, true:false; }
//void usaco(string s) { freopen((s+".in").c_str(),"r",stdin);
//freopen((s+".out").c_str(),"w",stdout); }
//template<class T> tree<T,
//less<T>, null_type, rb_tree_tag,
//tree_order_statistics_node_update> ordered_set;
const int N = 1e6+5;
const int mod = 1e9+7;
const ll inf = 1e18;
const ld Pi = acos(-1);
#define MULTI 0
int n, m, K, s, ans, q, res, a;
int b, l[N], r[N];
bool cmp(pii a, pii b){
if(a.ff != b.ff) return a.ff < b.ff;
else return a.ss > b.ss;
}
void solve(int t_case){
cin>>n>>a>>b;
int cyc = a / __gcd(a, b+1);
if(cyc > inf / b) cyc = inf;
else cyc = cyc * b;
vpii tt;
for(int i=0;i<n;i++){
cin>>l[i]>>r[i];
if(r[i] - l[i] + 1 >= cyc){
cout<<cyc<<"\n";
return;
}
int lx = l[i] % cyc, rx = r[i] % cyc;
if(lx <= rx) tt.pb({lx, rx});
else tt.pb({lx, cyc - 1}), tt.pb({0, rx});
}
sort(all(tt));
int lx = -1, rx = -1;
for(auto x : tt){
if(x.ff > rx){
if(~lx && ~rx) res += (rx - lx + 1);
lx = x.ff, rx = x.ss;
} else umax(rx, x.ss);
}
if(~lx && ~rx) res += (rx - lx + 1);
cout<<res<<"\n";
}
signed main(){
NeedForSpeed
if(!MULTI) {
solve(1);
} else {
int t; cin>>t;
for(int t_case = 1; t_case <= t; t_case++) solve(t_case);
} 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... |