#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) {
if (y == 0)
{
return x;
}
return gcd(y, x % y);
}
void addSegment(ll sta, ll fin) {
auto startPos = segments.upper_bound({sta, per});
if (startPos == segments.begin())
{
for (auto i = segments.begin(); i != segments.end(); )
{
if (i->first <= fin)
{
fin = max(fin, i->second);
segments.erase(i++);
}
else
{
break;
}
}
segments.insert({sta, fin});
return;
}
startPos--;
if (startPos->second >= sta)
{
sta = min(sta, startPos->first);
fin = max(fin, startPos->second);
segments.erase(startPos++);
}
else
{
startPos++;
}
for (auto i = startPos; i != segments.end(); )
{
if (i->first <= fin)
{
fin = max(fin, i->second);
segments.erase(i++);
}
else
{
break;
}
}
segments.insert({sta, fin});
}
int main() {
cin >> n >> a >> b;
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});
}
else
{
segments.insert({0, r});
segments.insert({l, per-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";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
34 ms |
1400 KB |
Output is correct |
3 |
Correct |
33 ms |
1272 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
1 ms |
256 KB |
Output is correct |
8 |
Correct |
1 ms |
256 KB |
Output is correct |
9 |
Correct |
1 ms |
256 KB |
Output is correct |
10 |
Correct |
1 ms |
256 KB |
Output is correct |
11 |
Correct |
1 ms |
256 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
1 ms |
256 KB |
Output is correct |
14 |
Correct |
1 ms |
256 KB |
Output is correct |
15 |
Correct |
1 ms |
256 KB |
Output is correct |
16 |
Correct |
35 ms |
1272 KB |
Output is correct |
17 |
Correct |
357 ms |
8312 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
2164 ms |
416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
3454 ms |
64820 KB |
Output is correct |
3 |
Correct |
3453 ms |
64872 KB |
Output is correct |
4 |
Correct |
3471 ms |
64864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
3454 ms |
64820 KB |
Output is correct |
3 |
Correct |
3453 ms |
64872 KB |
Output is correct |
4 |
Correct |
3471 ms |
64864 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
3408 ms |
64940 KB |
Output is correct |
7 |
Correct |
3442 ms |
64868 KB |
Output is correct |
8 |
Correct |
3440 ms |
64264 KB |
Output is correct |
9 |
Correct |
3505 ms |
64388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
3454 ms |
64820 KB |
Output is correct |
3 |
Correct |
3453 ms |
64872 KB |
Output is correct |
4 |
Correct |
3471 ms |
64864 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
333 ms |
7800 KB |
Output is correct |
7 |
Correct |
327 ms |
7932 KB |
Output is correct |
8 |
Correct |
331 ms |
7928 KB |
Output is correct |
9 |
Correct |
348 ms |
7800 KB |
Output is correct |
10 |
Correct |
339 ms |
7952 KB |
Output is correct |
11 |
Correct |
329 ms |
7800 KB |
Output is correct |
12 |
Correct |
338 ms |
7800 KB |
Output is correct |
13 |
Correct |
339 ms |
7800 KB |
Output is correct |
14 |
Correct |
332 ms |
7848 KB |
Output is correct |
15 |
Correct |
349 ms |
7800 KB |
Output is correct |
16 |
Correct |
341 ms |
7928 KB |
Output is correct |
17 |
Correct |
334 ms |
7800 KB |
Output is correct |
18 |
Correct |
3475 ms |
64292 KB |
Output is correct |
19 |
Correct |
3382 ms |
64392 KB |
Output is correct |
20 |
Correct |
3483 ms |
64244 KB |
Output is correct |
21 |
Correct |
335 ms |
7928 KB |
Output is correct |
22 |
Correct |
338 ms |
7800 KB |
Output is correct |
23 |
Correct |
1022 ms |
1644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
337 ms |
7800 KB |
Output is correct |
3 |
Correct |
339 ms |
7800 KB |
Output is correct |
4 |
Correct |
3426 ms |
64396 KB |
Output is correct |
5 |
Correct |
339 ms |
7800 KB |
Output is correct |
6 |
Correct |
343 ms |
7940 KB |
Output is correct |
7 |
Correct |
338 ms |
7812 KB |
Output is correct |
8 |
Correct |
338 ms |
7800 KB |
Output is correct |
9 |
Correct |
335 ms |
7804 KB |
Output is correct |
10 |
Correct |
339 ms |
7800 KB |
Output is correct |
11 |
Correct |
347 ms |
7800 KB |
Output is correct |
12 |
Correct |
355 ms |
7928 KB |
Output is correct |
13 |
Correct |
346 ms |
7828 KB |
Output is correct |
14 |
Correct |
3475 ms |
64164 KB |
Output is correct |
15 |
Correct |
350 ms |
7160 KB |
Output is correct |
16 |
Correct |
3391 ms |
64024 KB |
Output is correct |
17 |
Correct |
3389 ms |
64020 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
34 ms |
1400 KB |
Output is correct |
3 |
Correct |
33 ms |
1272 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
1 ms |
256 KB |
Output is correct |
8 |
Correct |
1 ms |
256 KB |
Output is correct |
9 |
Correct |
1 ms |
256 KB |
Output is correct |
10 |
Correct |
1 ms |
256 KB |
Output is correct |
11 |
Correct |
1 ms |
256 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
1 ms |
256 KB |
Output is correct |
14 |
Correct |
1 ms |
256 KB |
Output is correct |
15 |
Correct |
1 ms |
256 KB |
Output is correct |
16 |
Correct |
35 ms |
1272 KB |
Output is correct |
17 |
Correct |
357 ms |
8312 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
19 |
Correct |
1 ms |
256 KB |
Output is correct |
20 |
Correct |
0 ms |
256 KB |
Output is correct |
21 |
Correct |
0 ms |
256 KB |
Output is correct |
22 |
Correct |
1 ms |
256 KB |
Output is correct |
23 |
Correct |
1 ms |
256 KB |
Output is correct |
24 |
Correct |
1 ms |
256 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
4 ms |
384 KB |
Output is correct |
27 |
Correct |
4 ms |
384 KB |
Output is correct |
28 |
Correct |
2164 ms |
416 KB |
Output is correct |
29 |
Correct |
1 ms |
256 KB |
Output is correct |
30 |
Correct |
3454 ms |
64820 KB |
Output is correct |
31 |
Correct |
3453 ms |
64872 KB |
Output is correct |
32 |
Correct |
3471 ms |
64864 KB |
Output is correct |
33 |
Correct |
0 ms |
256 KB |
Output is correct |
34 |
Correct |
3408 ms |
64940 KB |
Output is correct |
35 |
Correct |
3442 ms |
64868 KB |
Output is correct |
36 |
Correct |
3440 ms |
64264 KB |
Output is correct |
37 |
Correct |
3505 ms |
64388 KB |
Output is correct |
38 |
Correct |
1 ms |
256 KB |
Output is correct |
39 |
Correct |
333 ms |
7800 KB |
Output is correct |
40 |
Correct |
327 ms |
7932 KB |
Output is correct |
41 |
Correct |
331 ms |
7928 KB |
Output is correct |
42 |
Correct |
348 ms |
7800 KB |
Output is correct |
43 |
Correct |
339 ms |
7952 KB |
Output is correct |
44 |
Correct |
329 ms |
7800 KB |
Output is correct |
45 |
Correct |
338 ms |
7800 KB |
Output is correct |
46 |
Correct |
339 ms |
7800 KB |
Output is correct |
47 |
Correct |
332 ms |
7848 KB |
Output is correct |
48 |
Correct |
349 ms |
7800 KB |
Output is correct |
49 |
Correct |
341 ms |
7928 KB |
Output is correct |
50 |
Correct |
334 ms |
7800 KB |
Output is correct |
51 |
Correct |
3475 ms |
64292 KB |
Output is correct |
52 |
Correct |
3382 ms |
64392 KB |
Output is correct |
53 |
Correct |
3483 ms |
64244 KB |
Output is correct |
54 |
Correct |
335 ms |
7928 KB |
Output is correct |
55 |
Correct |
338 ms |
7800 KB |
Output is correct |
56 |
Correct |
1022 ms |
1644 KB |
Output is correct |
57 |
Correct |
0 ms |
256 KB |
Output is correct |
58 |
Correct |
337 ms |
7800 KB |
Output is correct |
59 |
Correct |
339 ms |
7800 KB |
Output is correct |
60 |
Correct |
3426 ms |
64396 KB |
Output is correct |
61 |
Correct |
339 ms |
7800 KB |
Output is correct |
62 |
Correct |
343 ms |
7940 KB |
Output is correct |
63 |
Correct |
338 ms |
7812 KB |
Output is correct |
64 |
Correct |
338 ms |
7800 KB |
Output is correct |
65 |
Correct |
335 ms |
7804 KB |
Output is correct |
66 |
Correct |
339 ms |
7800 KB |
Output is correct |
67 |
Correct |
347 ms |
7800 KB |
Output is correct |
68 |
Correct |
355 ms |
7928 KB |
Output is correct |
69 |
Correct |
346 ms |
7828 KB |
Output is correct |
70 |
Correct |
3475 ms |
64164 KB |
Output is correct |
71 |
Correct |
350 ms |
7160 KB |
Output is correct |
72 |
Correct |
3391 ms |
64024 KB |
Output is correct |
73 |
Correct |
3389 ms |
64020 KB |
Output is correct |
74 |
Correct |
1 ms |
256 KB |
Output is correct |
75 |
Correct |
1 ms |
256 KB |
Output is correct |
76 |
Correct |
0 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 |
33 ms |
1280 KB |
Output is correct |
80 |
Correct |
3464 ms |
64048 KB |
Output is correct |
81 |
Correct |
3412 ms |
63988 KB |
Output is correct |
82 |
Correct |
3546 ms |
63984 KB |
Output is correct |
83 |
Correct |
3633 ms |
63864 KB |
Output is correct |
84 |
Correct |
3551 ms |
63876 KB |
Output is correct |
85 |
Correct |
3436 ms |
63824 KB |
Output is correct |
86 |
Correct |
1025 ms |
1032 KB |
Output is correct |
87 |
Correct |
1 ms |
256 KB |
Output is correct |