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;
template<class T, class U>
void ckmin(T &a, U b)
{
if (a > b) a = b;
}
template<class T, class U>
void ckmax(T &a, U b)
{
if (a < b) a = b;
}
#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)
#define SZ(x) ((int) (x).size())
#define ALL(x) (x).begin(), (x).end()
typedef __int128_t ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
ll gcd(ll a, ll b)
{
return (b == 0 ? a : gcd(b, a % b));
}
void readi(ll &x)
{
long long res;
cin >> res;
x = res;
}
int Q;
ll A, B;
ll period, ans;
vpl events;
int32_t main()
{
ios_base::sync_with_stdio(false); cin.tie(0);
cout << fixed << setprecision(12);
cerr << fixed << setprecision(4);
cin >> Q;
readi(A); readi(B);
ll g = gcd(A, B + 1);
period = B * A / g;
while(Q--)
{
ll L, R;
readi(L); readi(R);
if (R - L + 1 >= period)
{
cout << (long long) period << '\n';
return 0;
}
L %= period;
R %= period;
if (L > R)
{
events.PB({L, 1});
events.PB({period, -1});
events.PB({0, 1});
events.PB({R + 1, -1});
}
else
{
events.PB({L, 1});
events.PB({R + 1, -1});
}
}
sort(ALL(events));
int sum = 0;
FOR(i, 0, SZ(events))
{
if (sum)
{
ans += (events[i].fi - events[i - 1].fi);
}
sum += events[i].se;
}
cout << (long long) ans << '\n';
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... |