#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define FileName "test"
#define ll long long
#define ld long double
#define ull unsigned long long
#define Debug(x) cerr << #x << "is " << x << '\n';
#define pb emplace_back
#define mp make_pair
#define x first
#define y second
//#define LuckyAurora
//#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
//#pragma GCC optimization ("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
using namespace std;
template<typename T> inline void Cin(T &x)
{
char c;
T sign = 1;
x = 0;
for (c = getchar(); c < '0' || c > '9'; c = getchar())
if (c == '-') sign = -1;
for (; c >= '0' && c <= '9'; c = getchar())
x = x * 10 + c - '0';
x *= sign;
}
template <typename T> inline void Out(T x) {if(x > 9) Out(x / 10); putchar(x % 10 + '0');}
template <typename T> inline void Cout(T x, char c) {if(x < 0) putchar('-'); x = abs(x); Out(x); putchar(c);}
template <typename T, typename... Args> inline void Cin(T& a, Args&... args) {Cin(a);Cin(args...);}
template <typename T, typename... Args> inline void Cout(T a, char c, Args... args) {Cout(a, c);Cout(args...);}
typedef pair<int, int> pii;
const int N = int(1e6) + 1;
struct TIrene {
ll l, r;
TIrene(ll l = 0, ll r = 0): l(l), r(r) {}
bool operator<(const TIrene& o)const& {
return l < o.l || (l == o.l && r < o.r);
}
};
vector<TIrene> v;
int n;
ll A, B, l, r, irene, Irene, low, high, res;
const ll inf = (ll)1e18;
// A / GCD(A, B + 1) * B
ll GCD(ll a, ll b)
{
ll r;
if(a < b) swap(a, b);
while(b) {
r = a % b;
a = b, b = r;
}
return a;
}
ll Pos(ll x)
{
if(inf >= Irene) return x % Irene;
return x;
}
void Enter()
{
cin >> n >> A >> B;
irene = A / GCD(A, B + 1);
Irene = LLONG_MAX;
if(inf / irene >= B) Irene = B * irene;
for(int i = 0; i < n; ++i) {
cin >> l >> r;
if(r - l + 1 >= Irene) {cout << Irene; exit(0);}
low = Pos(l), high = Pos(r);
//cout << low << ' ' << high << '\n';
if(high >= low) v.pb(low, high);
else v.pb(low, Irene - 1), v.pb(0, high);
}
sort(v.begin(), v.end());
}
void Solve()
{
res = 0, high = -2, low = -1;
for(int i = 0; i < int(v.size()); ++i) {
if(v[i].l > high) {
res += high - low + 1;
low = v[i].l, high = v[i].r;
} else high = max(high, v[i].r);
}
res += high - low + 1;
cout << res;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if(fopen(FileName".inp", "r")) {
freopen(FileName".inp", "r", stdin);
freopen(FileName".out", "w", stdout);
}
/*int nTest; cin >> nTest;
while(nTest --)*/ Enter(), Solve();
return 0;
}
Compilation message
strange_device.cpp:15:0: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
#pragma GCC optimization ("O3")
strange_device.cpp: In function 'int main()':
strange_device.cpp:106:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen(FileName".inp", "r", stdin);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
strange_device.cpp:107:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen(FileName".out", "w", stdout);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
8 ms |
1016 KB |
Output is correct |
3 |
Correct |
9 ms |
1016 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
380 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
256 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
256 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
8 ms |
1144 KB |
Output is correct |
17 |
Correct |
67 ms |
3896 KB |
Output is correct |
18 |
Correct |
2 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
404 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
296 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
420 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
376 KB |
Output is correct |
5 |
Correct |
458 ms |
18784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
605 ms |
17404 KB |
Output is correct |
3 |
Correct |
606 ms |
18864 KB |
Output is correct |
4 |
Correct |
607 ms |
19028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
605 ms |
17404 KB |
Output is correct |
3 |
Correct |
606 ms |
18864 KB |
Output is correct |
4 |
Correct |
607 ms |
19028 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
608 ms |
19060 KB |
Output is correct |
7 |
Correct |
618 ms |
19180 KB |
Output is correct |
8 |
Correct |
601 ms |
19116 KB |
Output is correct |
9 |
Correct |
662 ms |
19224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
605 ms |
17404 KB |
Output is correct |
3 |
Correct |
606 ms |
18864 KB |
Output is correct |
4 |
Correct |
607 ms |
19028 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
62 ms |
4848 KB |
Output is correct |
7 |
Correct |
67 ms |
4720 KB |
Output is correct |
8 |
Correct |
63 ms |
4756 KB |
Output is correct |
9 |
Correct |
64 ms |
4720 KB |
Output is correct |
10 |
Correct |
61 ms |
4512 KB |
Output is correct |
11 |
Correct |
65 ms |
4592 KB |
Output is correct |
12 |
Correct |
61 ms |
4592 KB |
Output is correct |
13 |
Correct |
67 ms |
4496 KB |
Output is correct |
14 |
Correct |
61 ms |
4616 KB |
Output is correct |
15 |
Correct |
67 ms |
4588 KB |
Output is correct |
16 |
Correct |
66 ms |
4564 KB |
Output is correct |
17 |
Correct |
63 ms |
4700 KB |
Output is correct |
18 |
Correct |
621 ms |
19000 KB |
Output is correct |
19 |
Correct |
597 ms |
19016 KB |
Output is correct |
20 |
Correct |
660 ms |
18772 KB |
Output is correct |
21 |
Correct |
68 ms |
4428 KB |
Output is correct |
22 |
Correct |
62 ms |
4828 KB |
Output is correct |
23 |
Correct |
195 ms |
10892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
82 ms |
4956 KB |
Output is correct |
3 |
Correct |
66 ms |
4964 KB |
Output is correct |
4 |
Correct |
685 ms |
19112 KB |
Output is correct |
5 |
Correct |
68 ms |
4976 KB |
Output is correct |
6 |
Correct |
67 ms |
4848 KB |
Output is correct |
7 |
Correct |
66 ms |
4976 KB |
Output is correct |
8 |
Correct |
67 ms |
4824 KB |
Output is correct |
9 |
Correct |
66 ms |
4848 KB |
Output is correct |
10 |
Correct |
67 ms |
4844 KB |
Output is correct |
11 |
Correct |
67 ms |
4892 KB |
Output is correct |
12 |
Correct |
60 ms |
4816 KB |
Output is correct |
13 |
Correct |
66 ms |
4844 KB |
Output is correct |
14 |
Correct |
641 ms |
18972 KB |
Output is correct |
15 |
Correct |
65 ms |
4692 KB |
Output is correct |
16 |
Correct |
608 ms |
18344 KB |
Output is correct |
17 |
Correct |
608 ms |
18464 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
8 ms |
1016 KB |
Output is correct |
3 |
Correct |
9 ms |
1016 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
380 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
256 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
256 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
8 ms |
1144 KB |
Output is correct |
17 |
Correct |
67 ms |
3896 KB |
Output is correct |
18 |
Correct |
2 ms |
256 KB |
Output is correct |
19 |
Correct |
9 ms |
404 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
296 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
420 KB |
Output is correct |
25 |
Correct |
3 ms |
376 KB |
Output is correct |
26 |
Correct |
3 ms |
376 KB |
Output is correct |
27 |
Correct |
3 ms |
376 KB |
Output is correct |
28 |
Correct |
458 ms |
18784 KB |
Output is correct |
29 |
Correct |
2 ms |
376 KB |
Output is correct |
30 |
Correct |
605 ms |
17404 KB |
Output is correct |
31 |
Correct |
606 ms |
18864 KB |
Output is correct |
32 |
Correct |
607 ms |
19028 KB |
Output is correct |
33 |
Correct |
2 ms |
376 KB |
Output is correct |
34 |
Correct |
608 ms |
19060 KB |
Output is correct |
35 |
Correct |
618 ms |
19180 KB |
Output is correct |
36 |
Correct |
601 ms |
19116 KB |
Output is correct |
37 |
Correct |
662 ms |
19224 KB |
Output is correct |
38 |
Correct |
2 ms |
376 KB |
Output is correct |
39 |
Correct |
62 ms |
4848 KB |
Output is correct |
40 |
Correct |
67 ms |
4720 KB |
Output is correct |
41 |
Correct |
63 ms |
4756 KB |
Output is correct |
42 |
Correct |
64 ms |
4720 KB |
Output is correct |
43 |
Correct |
61 ms |
4512 KB |
Output is correct |
44 |
Correct |
65 ms |
4592 KB |
Output is correct |
45 |
Correct |
61 ms |
4592 KB |
Output is correct |
46 |
Correct |
67 ms |
4496 KB |
Output is correct |
47 |
Correct |
61 ms |
4616 KB |
Output is correct |
48 |
Correct |
67 ms |
4588 KB |
Output is correct |
49 |
Correct |
66 ms |
4564 KB |
Output is correct |
50 |
Correct |
63 ms |
4700 KB |
Output is correct |
51 |
Correct |
621 ms |
19000 KB |
Output is correct |
52 |
Correct |
597 ms |
19016 KB |
Output is correct |
53 |
Correct |
660 ms |
18772 KB |
Output is correct |
54 |
Correct |
68 ms |
4428 KB |
Output is correct |
55 |
Correct |
62 ms |
4828 KB |
Output is correct |
56 |
Correct |
195 ms |
10892 KB |
Output is correct |
57 |
Correct |
2 ms |
376 KB |
Output is correct |
58 |
Correct |
82 ms |
4956 KB |
Output is correct |
59 |
Correct |
66 ms |
4964 KB |
Output is correct |
60 |
Correct |
685 ms |
19112 KB |
Output is correct |
61 |
Correct |
68 ms |
4976 KB |
Output is correct |
62 |
Correct |
67 ms |
4848 KB |
Output is correct |
63 |
Correct |
66 ms |
4976 KB |
Output is correct |
64 |
Correct |
67 ms |
4824 KB |
Output is correct |
65 |
Correct |
66 ms |
4848 KB |
Output is correct |
66 |
Correct |
67 ms |
4844 KB |
Output is correct |
67 |
Correct |
67 ms |
4892 KB |
Output is correct |
68 |
Correct |
60 ms |
4816 KB |
Output is correct |
69 |
Correct |
66 ms |
4844 KB |
Output is correct |
70 |
Correct |
641 ms |
18972 KB |
Output is correct |
71 |
Correct |
65 ms |
4692 KB |
Output is correct |
72 |
Correct |
608 ms |
18344 KB |
Output is correct |
73 |
Correct |
608 ms |
18464 KB |
Output is correct |
74 |
Correct |
2 ms |
376 KB |
Output is correct |
75 |
Correct |
2 ms |
376 KB |
Output is correct |
76 |
Correct |
2 ms |
376 KB |
Output is correct |
77 |
Correct |
3 ms |
376 KB |
Output is correct |
78 |
Correct |
2 ms |
376 KB |
Output is correct |
79 |
Correct |
8 ms |
1144 KB |
Output is correct |
80 |
Correct |
670 ms |
18760 KB |
Output is correct |
81 |
Correct |
677 ms |
18892 KB |
Output is correct |
82 |
Correct |
642 ms |
18816 KB |
Output is correct |
83 |
Correct |
645 ms |
18632 KB |
Output is correct |
84 |
Correct |
658 ms |
18612 KB |
Output is correct |
85 |
Correct |
712 ms |
18604 KB |
Output is correct |
86 |
Correct |
193 ms |
11872 KB |
Output is correct |
87 |
Correct |
2 ms |
376 KB |
Output is correct |