#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
const ll MAX = 1e18;
vector<pll> vec;
// {Left, Right}
ll n, a, b, length, ans = 0;
void opr(ll l, ll r) {
ll pushl = min(l, r);
ll pushr = max(l, r);
if (pushl == 0) {
// pushl = length;
}
if (pushr == 0) {
// pushr = length;
}
vec.push_back({pushl, pushr});
}
void answer() {
sort(vec.begin(), vec.end());
ll idx = -1;
for (int i = 0; i < vec.size(); ++i)
{
// cout << vec[i].first << " " << vec[i].second << "\n";
pll now = vec[i];
if (now.second < now.first)
{
continue ;
}
if (idx >= now.first)
{
if (now.second <= idx)
{
continue ;
} else {
idx++;
ans += now.second - idx + 1;
idx = now.second;
}
} else {
idx = now.first;
ans += now.second - idx + 1;
idx = now.second;
}
}
cout << ans << "\n";
}
ll fpb(ll a, ll b) {
if (a == 0)
{
return b;
}
return fpb(b%a, a);
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n >> a >> b;
ull lebek;
// if (a == 1) {
// lebek = b;
// } else {
// if (a % 2)
// {
// lebek = a * b;
// } else {
// lebek = (a/2) * b;
// }
// }
lebek = a / fpb(a, b+1) * b;
// length = lebek;
length = min(lebek, (ull)MAX);
for (int i = 0; i < n; ++i)
{
ll nowl, nowr;
cin >> nowl >> nowr;
if (nowr-nowl+1 >= length)
{
cout << length << "\n";
return 0;
}
if (nowl/length != nowr/length) {
opr(nowl % length + 1, length);
opr(1, nowr % length + 1);
} else {
opr(nowl % length + 1, nowr % length + 1);
}
}
answer();
return 0;
}
Compilation message
strange_device.cpp: In function 'void answer()':
strange_device.cpp:31:21: 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]
31 | for (int i = 0; i < vec.size(); ++i)
| ~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
4 ms |
600 KB |
Output is correct |
3 |
Correct |
4 ms |
600 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
280 KB |
Output is correct |
16 |
Correct |
4 ms |
600 KB |
Output is correct |
17 |
Correct |
42 ms |
2388 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
212 ms |
16772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
330 ms |
16816 KB |
Output is correct |
3 |
Correct |
326 ms |
16812 KB |
Output is correct |
4 |
Correct |
327 ms |
16848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
330 ms |
16816 KB |
Output is correct |
3 |
Correct |
326 ms |
16812 KB |
Output is correct |
4 |
Correct |
327 ms |
16848 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
321 ms |
16868 KB |
Output is correct |
7 |
Correct |
314 ms |
16788 KB |
Output is correct |
8 |
Correct |
350 ms |
16748 KB |
Output is correct |
9 |
Correct |
342 ms |
16780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
330 ms |
16816 KB |
Output is correct |
3 |
Correct |
326 ms |
16812 KB |
Output is correct |
4 |
Correct |
327 ms |
16848 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
29 ms |
2512 KB |
Output is correct |
7 |
Correct |
37 ms |
2476 KB |
Output is correct |
8 |
Correct |
29 ms |
2432 KB |
Output is correct |
9 |
Correct |
30 ms |
2432 KB |
Output is correct |
10 |
Correct |
29 ms |
2472 KB |
Output is correct |
11 |
Correct |
31 ms |
2472 KB |
Output is correct |
12 |
Correct |
29 ms |
2512 KB |
Output is correct |
13 |
Correct |
31 ms |
2504 KB |
Output is correct |
14 |
Correct |
29 ms |
2432 KB |
Output is correct |
15 |
Correct |
34 ms |
2488 KB |
Output is correct |
16 |
Correct |
32 ms |
2416 KB |
Output is correct |
17 |
Correct |
40 ms |
2416 KB |
Output is correct |
18 |
Correct |
331 ms |
16788 KB |
Output is correct |
19 |
Correct |
308 ms |
16808 KB |
Output is correct |
20 |
Correct |
343 ms |
16816 KB |
Output is correct |
21 |
Correct |
32 ms |
2512 KB |
Output is correct |
22 |
Correct |
33 ms |
2512 KB |
Output is correct |
23 |
Correct |
92 ms |
8612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
31 ms |
2512 KB |
Output is correct |
3 |
Correct |
31 ms |
2512 KB |
Output is correct |
4 |
Correct |
352 ms |
16840 KB |
Output is correct |
5 |
Correct |
34 ms |
2512 KB |
Output is correct |
6 |
Correct |
32 ms |
2516 KB |
Output is correct |
7 |
Correct |
31 ms |
2512 KB |
Output is correct |
8 |
Correct |
32 ms |
2512 KB |
Output is correct |
9 |
Correct |
31 ms |
2444 KB |
Output is correct |
10 |
Correct |
31 ms |
2432 KB |
Output is correct |
11 |
Correct |
32 ms |
2496 KB |
Output is correct |
12 |
Correct |
28 ms |
2512 KB |
Output is correct |
13 |
Correct |
32 ms |
2400 KB |
Output is correct |
14 |
Correct |
322 ms |
16800 KB |
Output is correct |
15 |
Correct |
32 ms |
2512 KB |
Output is correct |
16 |
Correct |
323 ms |
16836 KB |
Output is correct |
17 |
Correct |
308 ms |
16860 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
4 ms |
600 KB |
Output is correct |
3 |
Correct |
4 ms |
600 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
280 KB |
Output is correct |
16 |
Correct |
4 ms |
600 KB |
Output is correct |
17 |
Correct |
42 ms |
2388 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
212 ms |
16772 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
330 ms |
16816 KB |
Output is correct |
31 |
Correct |
326 ms |
16812 KB |
Output is correct |
32 |
Correct |
327 ms |
16848 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
321 ms |
16868 KB |
Output is correct |
35 |
Correct |
314 ms |
16788 KB |
Output is correct |
36 |
Correct |
350 ms |
16748 KB |
Output is correct |
37 |
Correct |
342 ms |
16780 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
29 ms |
2512 KB |
Output is correct |
40 |
Correct |
37 ms |
2476 KB |
Output is correct |
41 |
Correct |
29 ms |
2432 KB |
Output is correct |
42 |
Correct |
30 ms |
2432 KB |
Output is correct |
43 |
Correct |
29 ms |
2472 KB |
Output is correct |
44 |
Correct |
31 ms |
2472 KB |
Output is correct |
45 |
Correct |
29 ms |
2512 KB |
Output is correct |
46 |
Correct |
31 ms |
2504 KB |
Output is correct |
47 |
Correct |
29 ms |
2432 KB |
Output is correct |
48 |
Correct |
34 ms |
2488 KB |
Output is correct |
49 |
Correct |
32 ms |
2416 KB |
Output is correct |
50 |
Correct |
40 ms |
2416 KB |
Output is correct |
51 |
Correct |
331 ms |
16788 KB |
Output is correct |
52 |
Correct |
308 ms |
16808 KB |
Output is correct |
53 |
Correct |
343 ms |
16816 KB |
Output is correct |
54 |
Correct |
32 ms |
2512 KB |
Output is correct |
55 |
Correct |
33 ms |
2512 KB |
Output is correct |
56 |
Correct |
92 ms |
8612 KB |
Output is correct |
57 |
Correct |
0 ms |
212 KB |
Output is correct |
58 |
Correct |
31 ms |
2512 KB |
Output is correct |
59 |
Correct |
31 ms |
2512 KB |
Output is correct |
60 |
Correct |
352 ms |
16840 KB |
Output is correct |
61 |
Correct |
34 ms |
2512 KB |
Output is correct |
62 |
Correct |
32 ms |
2516 KB |
Output is correct |
63 |
Correct |
31 ms |
2512 KB |
Output is correct |
64 |
Correct |
32 ms |
2512 KB |
Output is correct |
65 |
Correct |
31 ms |
2444 KB |
Output is correct |
66 |
Correct |
31 ms |
2432 KB |
Output is correct |
67 |
Correct |
32 ms |
2496 KB |
Output is correct |
68 |
Correct |
28 ms |
2512 KB |
Output is correct |
69 |
Correct |
32 ms |
2400 KB |
Output is correct |
70 |
Correct |
322 ms |
16800 KB |
Output is correct |
71 |
Correct |
32 ms |
2512 KB |
Output is correct |
72 |
Correct |
323 ms |
16836 KB |
Output is correct |
73 |
Correct |
308 ms |
16860 KB |
Output is correct |
74 |
Correct |
0 ms |
212 KB |
Output is correct |
75 |
Correct |
1 ms |
320 KB |
Output is correct |
76 |
Correct |
1 ms |
212 KB |
Output is correct |
77 |
Correct |
1 ms |
212 KB |
Output is correct |
78 |
Correct |
0 ms |
212 KB |
Output is correct |
79 |
Correct |
4 ms |
1080 KB |
Output is correct |
80 |
Correct |
387 ms |
18824 KB |
Output is correct |
81 |
Correct |
375 ms |
18412 KB |
Output is correct |
82 |
Correct |
349 ms |
18092 KB |
Output is correct |
83 |
Correct |
340 ms |
18048 KB |
Output is correct |
84 |
Correct |
373 ms |
18932 KB |
Output is correct |
85 |
Correct |
340 ms |
18860 KB |
Output is correct |
86 |
Correct |
99 ms |
10580 KB |
Output is correct |
87 |
Correct |
0 ms |
320 KB |
Output is correct |