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 pb push_back
#define mp make_pair
#define f1 first
#define s2 second
#define fastio ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define debug(x...) cerr << "[" << #x << "]: " << x << "\n";
using ll = long long;
using ld = long double;
using ii = pair<int, int>;
using pl = pair<ll, ll>;
ld const PI = 4*atan((ld)1);
int main()
{
fastio;
ll n, a, b;
cin >> n >> a >> b;
vector<pl> v(n);
for (pl &x : v)
cin >> x.f1 >> x.s2;
ll gcd = __gcd(a, b+1);
if (v.back().s2/a < b / gcd)
{
ll res = 0;
for (pl x : v)
res += (x.s2 - x.f1 + 1);
cout << res << '\n';
return 0;
}
ll mod = (a/gcd) * b;
vector<pl> range; //first: duration, second: state
for (pl x : v)
{
if (x.s2 - x.f1 + 1 >= mod)
{
cout << mod << '\n';
return 0;
}
ll l = x.f1 % mod;
ll r = x.s2 % mod;
debug(l);
debug(r);
if (l <= r)
{
range.pb({l, 1});
range.pb({r+1, -1});
}
else
{
range.pb({l, 1});
range.pb({a*b, -1});
range.pb({0, 1});
range.pb({r+1, -1});
}
}
sort(range.begin(), range.end());
//for (auto x : range)
//{
// cout << "-> " << x.f1 << " " << x.s2 << '\n';
//}
ll ctr = 0;
ll res = 0;
for (int i = 0; i+1 < range.size(); ++i)
{
ctr += range[i].s2;
if (ctr > 0) res += (range[i+1].f1 - range[i].f1);
}
cout << res << '\n';
return 0;
}
Compilation message (stderr)
strange_device.cpp: In function 'int main()':
strange_device.cpp:78:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
78 | for (int i = 0; i+1 < range.size(); ++i)
| ~~~~^~~~~~~~~~~~~~
# | 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... |