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;
typedef long long ll;
typedef long double ld;
ll n, a, b, l, r, per;
set<pair<ll, ll> > segments, newSegments;
ll gcd(ll x, ll y) {
//cout << "GCD of " << x << " " << y << "\n";
if (y == 0)
{
return x;
}
return gcd(y, x % y);
}
void addSegment(ll sta, ll fin) {
//newSegments = segments;
auto startPos = segments.upper_bound({sta, per});
vector<pair<ll, ll>> toErase;
if (startPos == segments.begin())
{
for (auto i = segments.begin(); i != segments.end(); i++)
{
if (i->first <= fin)
{
fin = max(fin, i->second);
toErase.push_back({i->first, i->second});
}
else
{
break;
}
}
for (int i = 0; i < toErase.size(); i++)
{
segments.erase({toErase[i].first, toErase[i].second});
}
segments.insert({sta, fin});
return;
}
startPos--;
if (startPos->second >= sta)
{
sta = min(sta, startPos->first);
fin = max(fin, startPos->second);
toErase.push_back({startPos->first, startPos->second});
}
startPos++;
for (auto i = startPos; i != segments.end(); i++)
{
if (i->first <= fin)
{
fin = max(fin, i->second);
toErase.push_back({i->first, i->second});
}
else
{
break;
}
}
for (int i = 0; i < toErase.size(); i++)
{
segments.erase({toErase[i].first, toErase[i].second});
}
segments.insert({sta, fin});
}
int main() {
cin >> n >> a >> b;
//segments.insert({0, 1});
//segments.insert({1000000000000000000, 0});
per = gcd(a, b+1);
ld A = a;
ld B = b;
ld t = A/(1000000000000000007) * B;
if (t > per)
{
per = 1000000000000000007;
}
else
{
per = round(A/per * B);
}
cin >> l >> r;
if (r-l+1 >= per)
{
cout << per << "\n";
return 0;
}
l = l%per;
r = r%per;
if(l <= r) {
segments.insert({l, r});
//segments.push_back({r, 1});
}
else
{
segments.insert({0, r});
//segments.push_back({r, 1});
segments.insert({l, per-1});
//segments.push_back({per-1, 1});
}
for (int i = 1; i < n; i++)
{
cin >> l >> r;
if (r-l+1 >= per)
{
cout << per << "\n";
return 0;
}
l = l%per;
r = r%per;
if (l <= r)
{
addSegment(l, r);
}
else
{
addSegment(0, r);
addSegment(l, per-1);
}
}
ll runningTotal = 0;
for (auto i : segments)
{
runningTotal += i.second - i.first + 1;
}
cout << runningTotal << "\n";
}
Compilation message (stderr)
strange_device.cpp: In function 'void addSegment(ll, ll)':
strange_device.cpp:36:27: 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]
36 | for (int i = 0; i < toErase.size(); i++)
| ~~^~~~~~~~~~~~~~~~
strange_device.cpp:63:23: 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]
63 | for (int i = 0; i < toErase.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... |