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>
// author: aykhn
using namespace std;
typedef long long ll;
#define TC int t; cin >> t; while (t--) _();
#define OPT ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define all(v) v.begin(), v.end()
#define pii pair<int, int>
#define mpr make_pair
#define eb emplace_back
#define new int32_t
#define pb push_back
#define ts to_string
#define fi first
#define se second
#define int ll
#define ins insert
#define inf 0x3F3F3F3F
#define infll 0x3F3F3F3F3F3F3F3FLL
#define bpc __builtin_popcount
int n, a, b;
/*int x(int i)
{
return (i + i/b) % a;
}
int y(int i)
{
return i % b;
}*/
new main()
{
cin >> n >> a >> b;
int x = a/__gcd(a, b + 1) * b;
set<pii> s;
for (int i = 0; i < n; i++)
{
int a, b;
cin >> a >> b;
if (b - a + 1 >= x)
{
s.ins(mpr(0, x - 1));
}
else
{
int l = a % x;
int r = b % x;
if (l <= r)
{
s.ins(mpr(l, r));
}
else
{
s.ins(mpr(l, x - 1));
s.ins(mpr(0, r));
}
}
}
set<pii> nw;
while (s.size() > 1)
{
auto it = s.begin();
pii a = *it;
it++;
pii b = *it;
if (max(a.fi, b.fi) <= min(a.se, b.se))
{
s.erase(s.begin());
s.erase(s.begin());
s.ins(mpr(min(a.fi, b.fi), max(a.se, b.se)));
}
else
{
s.erase(s.begin());
nw.ins(a);
}
}
nw.ins(*s.begin());
int res = 0;
for (pii a : nw)
{
res += a.se - a.fi + 1;
}
cout << res << endl;
return 0;
}
// 4, 5 -> 10;
// 3, 6 -> 18;
# | 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... |