#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
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 |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
33 ms |
1272 KB |
Output is correct |
3 |
Correct |
35 ms |
1272 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
256 KB |
Output is correct |
9 |
Correct |
1 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
1 ms |
256 KB |
Output is correct |
13 |
Correct |
0 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
1 ms |
256 KB |
Output is correct |
16 |
Correct |
37 ms |
1272 KB |
Output is correct |
17 |
Correct |
348 ms |
10232 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
288 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
368 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
2227 ms |
25576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
3472 ms |
76644 KB |
Output is correct |
3 |
Correct |
3457 ms |
76524 KB |
Output is correct |
4 |
Correct |
3575 ms |
76476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
3472 ms |
76644 KB |
Output is correct |
3 |
Correct |
3457 ms |
76524 KB |
Output is correct |
4 |
Correct |
3575 ms |
76476 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
3458 ms |
76404 KB |
Output is correct |
7 |
Correct |
3445 ms |
76844 KB |
Output is correct |
8 |
Correct |
3566 ms |
76340 KB |
Output is correct |
9 |
Correct |
3495 ms |
76456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
3472 ms |
76644 KB |
Output is correct |
3 |
Correct |
3457 ms |
76524 KB |
Output is correct |
4 |
Correct |
3575 ms |
76476 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
398 ms |
10360 KB |
Output is correct |
7 |
Correct |
334 ms |
10232 KB |
Output is correct |
8 |
Correct |
331 ms |
10232 KB |
Output is correct |
9 |
Correct |
348 ms |
10232 KB |
Output is correct |
10 |
Correct |
344 ms |
10268 KB |
Output is correct |
11 |
Correct |
344 ms |
10284 KB |
Output is correct |
12 |
Correct |
344 ms |
10232 KB |
Output is correct |
13 |
Correct |
367 ms |
10360 KB |
Output is correct |
14 |
Correct |
336 ms |
10232 KB |
Output is correct |
15 |
Correct |
341 ms |
10360 KB |
Output is correct |
16 |
Correct |
341 ms |
10232 KB |
Output is correct |
17 |
Correct |
338 ms |
10232 KB |
Output is correct |
18 |
Correct |
3396 ms |
76556 KB |
Output is correct |
19 |
Correct |
3421 ms |
76408 KB |
Output is correct |
20 |
Correct |
3564 ms |
76676 KB |
Output is correct |
21 |
Correct |
351 ms |
10232 KB |
Output is correct |
22 |
Correct |
347 ms |
10232 KB |
Output is correct |
23 |
Correct |
1048 ms |
13048 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
355 ms |
10232 KB |
Output is correct |
3 |
Correct |
340 ms |
10360 KB |
Output is correct |
4 |
Correct |
3489 ms |
76320 KB |
Output is correct |
5 |
Correct |
362 ms |
10364 KB |
Output is correct |
6 |
Correct |
368 ms |
10488 KB |
Output is correct |
7 |
Correct |
351 ms |
10372 KB |
Output is correct |
8 |
Correct |
345 ms |
10424 KB |
Output is correct |
9 |
Correct |
367 ms |
10308 KB |
Output is correct |
10 |
Correct |
358 ms |
10360 KB |
Output is correct |
11 |
Correct |
339 ms |
10232 KB |
Output is correct |
12 |
Correct |
342 ms |
10232 KB |
Output is correct |
13 |
Correct |
343 ms |
10308 KB |
Output is correct |
14 |
Correct |
3895 ms |
76536 KB |
Output is correct |
15 |
Correct |
346 ms |
9592 KB |
Output is correct |
16 |
Correct |
3472 ms |
76408 KB |
Output is correct |
17 |
Correct |
3468 ms |
76432 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
33 ms |
1272 KB |
Output is correct |
3 |
Correct |
35 ms |
1272 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
256 KB |
Output is correct |
9 |
Correct |
1 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
1 ms |
256 KB |
Output is correct |
13 |
Correct |
0 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
1 ms |
256 KB |
Output is correct |
16 |
Correct |
37 ms |
1272 KB |
Output is correct |
17 |
Correct |
348 ms |
10232 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
19 |
Correct |
1 ms |
288 KB |
Output is correct |
20 |
Correct |
1 ms |
256 KB |
Output is correct |
21 |
Correct |
0 ms |
256 KB |
Output is correct |
22 |
Correct |
0 ms |
368 KB |
Output is correct |
23 |
Correct |
0 ms |
256 KB |
Output is correct |
24 |
Correct |
0 ms |
256 KB |
Output is correct |
25 |
Correct |
4 ms |
384 KB |
Output is correct |
26 |
Correct |
4 ms |
384 KB |
Output is correct |
27 |
Correct |
5 ms |
384 KB |
Output is correct |
28 |
Correct |
2227 ms |
25576 KB |
Output is correct |
29 |
Correct |
1 ms |
256 KB |
Output is correct |
30 |
Correct |
3472 ms |
76644 KB |
Output is correct |
31 |
Correct |
3457 ms |
76524 KB |
Output is correct |
32 |
Correct |
3575 ms |
76476 KB |
Output is correct |
33 |
Correct |
1 ms |
256 KB |
Output is correct |
34 |
Correct |
3458 ms |
76404 KB |
Output is correct |
35 |
Correct |
3445 ms |
76844 KB |
Output is correct |
36 |
Correct |
3566 ms |
76340 KB |
Output is correct |
37 |
Correct |
3495 ms |
76456 KB |
Output is correct |
38 |
Correct |
1 ms |
256 KB |
Output is correct |
39 |
Correct |
398 ms |
10360 KB |
Output is correct |
40 |
Correct |
334 ms |
10232 KB |
Output is correct |
41 |
Correct |
331 ms |
10232 KB |
Output is correct |
42 |
Correct |
348 ms |
10232 KB |
Output is correct |
43 |
Correct |
344 ms |
10268 KB |
Output is correct |
44 |
Correct |
344 ms |
10284 KB |
Output is correct |
45 |
Correct |
344 ms |
10232 KB |
Output is correct |
46 |
Correct |
367 ms |
10360 KB |
Output is correct |
47 |
Correct |
336 ms |
10232 KB |
Output is correct |
48 |
Correct |
341 ms |
10360 KB |
Output is correct |
49 |
Correct |
341 ms |
10232 KB |
Output is correct |
50 |
Correct |
338 ms |
10232 KB |
Output is correct |
51 |
Correct |
3396 ms |
76556 KB |
Output is correct |
52 |
Correct |
3421 ms |
76408 KB |
Output is correct |
53 |
Correct |
3564 ms |
76676 KB |
Output is correct |
54 |
Correct |
351 ms |
10232 KB |
Output is correct |
55 |
Correct |
347 ms |
10232 KB |
Output is correct |
56 |
Correct |
1048 ms |
13048 KB |
Output is correct |
57 |
Correct |
1 ms |
256 KB |
Output is correct |
58 |
Correct |
355 ms |
10232 KB |
Output is correct |
59 |
Correct |
340 ms |
10360 KB |
Output is correct |
60 |
Correct |
3489 ms |
76320 KB |
Output is correct |
61 |
Correct |
362 ms |
10364 KB |
Output is correct |
62 |
Correct |
368 ms |
10488 KB |
Output is correct |
63 |
Correct |
351 ms |
10372 KB |
Output is correct |
64 |
Correct |
345 ms |
10424 KB |
Output is correct |
65 |
Correct |
367 ms |
10308 KB |
Output is correct |
66 |
Correct |
358 ms |
10360 KB |
Output is correct |
67 |
Correct |
339 ms |
10232 KB |
Output is correct |
68 |
Correct |
342 ms |
10232 KB |
Output is correct |
69 |
Correct |
343 ms |
10308 KB |
Output is correct |
70 |
Correct |
3895 ms |
76536 KB |
Output is correct |
71 |
Correct |
346 ms |
9592 KB |
Output is correct |
72 |
Correct |
3472 ms |
76408 KB |
Output is correct |
73 |
Correct |
3468 ms |
76432 KB |
Output is correct |
74 |
Correct |
1 ms |
256 KB |
Output is correct |
75 |
Correct |
1 ms |
256 KB |
Output is correct |
76 |
Correct |
1 ms |
256 KB |
Output is correct |
77 |
Correct |
1 ms |
256 KB |
Output is correct |
78 |
Correct |
1 ms |
256 KB |
Output is correct |
79 |
Correct |
35 ms |
1272 KB |
Output is correct |
80 |
Correct |
3506 ms |
76424 KB |
Output is correct |
81 |
Correct |
3461 ms |
76596 KB |
Output is correct |
82 |
Correct |
3563 ms |
76452 KB |
Output is correct |
83 |
Correct |
3528 ms |
76632 KB |
Output is correct |
84 |
Correct |
3585 ms |
76472 KB |
Output is correct |
85 |
Correct |
3536 ms |
76332 KB |
Output is correct |
86 |
Correct |
1051 ms |
13036 KB |
Output is correct |
87 |
Correct |
1 ms |
256 KB |
Output is correct |