#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define _size(x) (int)x.size()
#define BIT(i, x) ((x >> i) & 1)
#define MASK(n) ((1 << n) - 1)
#define REP(i, n) for (int i = 0, _n = (n); i < _n; i++)
#define FOR(i, a, b) for (int i = a, _b = (b); i <= _b; i++)
#define FORD(i, a, b) for (int i = a, _b = (b); i >= _b; i--)
#define FORB1(i, mask) for (int i = mask; i > 0; i ^= i & - i)
#define FORB0(i, n, mask) for (int i = ((1 << n) - 1) ^ mask; i > 0; i ^= i & - i)
#define FORALL(i, a) for (auto i: a)
#define fastio ios_base::sync_with_stdio(0); cin.tie(0);
const long long inf = 1e18;
int main() {
fastio;
int n;
long long a, b;
cin >> n >> a >> b;
long long k = a / __gcd(b + 1, a);
vector<pair<long long, int>> g;
long long p = 0;
if (inf / k < b) p = inf + 5;
else p = k * b;
REP(i, n) {
long long l, r;
cin >> l >> r;
if (r - l + 1 >= p) {
cout << p << '\n';
return 0;
}
long long lo = l % p, hi = r % p;
if (lo <= hi) g.push_back({lo, + 1}), g.push_back({hi, - 1});
else {
g.push_back({lo, + 1}), g.push_back({p - 1, - 1});
g.push_back({0, + 1}), g.push_back({hi, - 1});
}
}
sort(g.begin(), g.end(), [&] (const pair<long long, int> a, pair<long long, int> b) { return (a.fi != b.fi) ? (a.fi < b.fi) : (a.se > b.se); });
int i = 0, sum = 0;
long long ans = 0, cur;
while (i < _size(g)) {
int cur_sum = sum;
int j = i;
while (j < _size(g) && g[j].fi == g[i].fi && g[j].se == + 1) sum++, j++;
if (!cur_sum && sum) ans++;
else if (sum) ans += g[i].fi - cur;
while (j < _size(g) && g[j].fi == g[i].fi) sum--, j++;
cur = g[i].fi;
i = j;
}
cout << ans << '\n';
return 0;
}
Compilation message
strange_device.cpp: In function 'int main()':
strange_device.cpp:52:38: warning: 'cur' may be used uninitialized in this function [-Wmaybe-uninitialized]
52 | else if (sum) ans += g[i].fi - cur;
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
4 ms |
1232 KB |
Output is correct |
3 |
Correct |
5 ms |
1240 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
316 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
316 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
4 ms |
1300 KB |
Output is correct |
17 |
Correct |
47 ms |
7288 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
316 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
380 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
270 ms |
56844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
440 ms |
38832 KB |
Output is correct |
3 |
Correct |
498 ms |
38780 KB |
Output is correct |
4 |
Correct |
456 ms |
38876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
440 ms |
38832 KB |
Output is correct |
3 |
Correct |
498 ms |
38780 KB |
Output is correct |
4 |
Correct |
456 ms |
38876 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
475 ms |
38832 KB |
Output is correct |
7 |
Correct |
494 ms |
38936 KB |
Output is correct |
8 |
Correct |
446 ms |
39000 KB |
Output is correct |
9 |
Correct |
540 ms |
38840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
440 ms |
38832 KB |
Output is correct |
3 |
Correct |
498 ms |
38780 KB |
Output is correct |
4 |
Correct |
456 ms |
38876 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
41 ms |
7292 KB |
Output is correct |
7 |
Correct |
59 ms |
7224 KB |
Output is correct |
8 |
Correct |
39 ms |
7260 KB |
Output is correct |
9 |
Correct |
41 ms |
7248 KB |
Output is correct |
10 |
Correct |
36 ms |
7176 KB |
Output is correct |
11 |
Correct |
51 ms |
7256 KB |
Output is correct |
12 |
Correct |
38 ms |
7292 KB |
Output is correct |
13 |
Correct |
45 ms |
7228 KB |
Output is correct |
14 |
Correct |
38 ms |
7292 KB |
Output is correct |
15 |
Correct |
47 ms |
7176 KB |
Output is correct |
16 |
Correct |
45 ms |
7216 KB |
Output is correct |
17 |
Correct |
40 ms |
7232 KB |
Output is correct |
18 |
Correct |
461 ms |
68800 KB |
Output is correct |
19 |
Correct |
462 ms |
68964 KB |
Output is correct |
20 |
Correct |
584 ms |
68748 KB |
Output is correct |
21 |
Correct |
60 ms |
7272 KB |
Output is correct |
22 |
Correct |
41 ms |
7212 KB |
Output is correct |
23 |
Correct |
104 ms |
26672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
46 ms |
7244 KB |
Output is correct |
3 |
Correct |
44 ms |
7252 KB |
Output is correct |
4 |
Correct |
626 ms |
38860 KB |
Output is correct |
5 |
Correct |
46 ms |
7212 KB |
Output is correct |
6 |
Correct |
46 ms |
7228 KB |
Output is correct |
7 |
Correct |
61 ms |
7276 KB |
Output is correct |
8 |
Correct |
62 ms |
7196 KB |
Output is correct |
9 |
Correct |
45 ms |
7232 KB |
Output is correct |
10 |
Correct |
47 ms |
7276 KB |
Output is correct |
11 |
Correct |
47 ms |
7232 KB |
Output is correct |
12 |
Correct |
41 ms |
7288 KB |
Output is correct |
13 |
Correct |
46 ms |
7172 KB |
Output is correct |
14 |
Correct |
520 ms |
38904 KB |
Output is correct |
15 |
Correct |
47 ms |
7260 KB |
Output is correct |
16 |
Correct |
401 ms |
68748 KB |
Output is correct |
17 |
Correct |
446 ms |
68764 KB |
Output is correct |
18 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
4 ms |
1232 KB |
Output is correct |
3 |
Correct |
5 ms |
1240 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
316 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
316 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
4 ms |
1300 KB |
Output is correct |
17 |
Correct |
47 ms |
7288 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
316 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
380 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
270 ms |
56844 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
440 ms |
38832 KB |
Output is correct |
31 |
Correct |
498 ms |
38780 KB |
Output is correct |
32 |
Correct |
456 ms |
38876 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
475 ms |
38832 KB |
Output is correct |
35 |
Correct |
494 ms |
38936 KB |
Output is correct |
36 |
Correct |
446 ms |
39000 KB |
Output is correct |
37 |
Correct |
540 ms |
38840 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
41 ms |
7292 KB |
Output is correct |
40 |
Correct |
59 ms |
7224 KB |
Output is correct |
41 |
Correct |
39 ms |
7260 KB |
Output is correct |
42 |
Correct |
41 ms |
7248 KB |
Output is correct |
43 |
Correct |
36 ms |
7176 KB |
Output is correct |
44 |
Correct |
51 ms |
7256 KB |
Output is correct |
45 |
Correct |
38 ms |
7292 KB |
Output is correct |
46 |
Correct |
45 ms |
7228 KB |
Output is correct |
47 |
Correct |
38 ms |
7292 KB |
Output is correct |
48 |
Correct |
47 ms |
7176 KB |
Output is correct |
49 |
Correct |
45 ms |
7216 KB |
Output is correct |
50 |
Correct |
40 ms |
7232 KB |
Output is correct |
51 |
Correct |
461 ms |
68800 KB |
Output is correct |
52 |
Correct |
462 ms |
68964 KB |
Output is correct |
53 |
Correct |
584 ms |
68748 KB |
Output is correct |
54 |
Correct |
60 ms |
7272 KB |
Output is correct |
55 |
Correct |
41 ms |
7212 KB |
Output is correct |
56 |
Correct |
104 ms |
26672 KB |
Output is correct |
57 |
Correct |
1 ms |
212 KB |
Output is correct |
58 |
Correct |
46 ms |
7244 KB |
Output is correct |
59 |
Correct |
44 ms |
7252 KB |
Output is correct |
60 |
Correct |
626 ms |
38860 KB |
Output is correct |
61 |
Correct |
46 ms |
7212 KB |
Output is correct |
62 |
Correct |
46 ms |
7228 KB |
Output is correct |
63 |
Correct |
61 ms |
7276 KB |
Output is correct |
64 |
Correct |
62 ms |
7196 KB |
Output is correct |
65 |
Correct |
45 ms |
7232 KB |
Output is correct |
66 |
Correct |
47 ms |
7276 KB |
Output is correct |
67 |
Correct |
47 ms |
7232 KB |
Output is correct |
68 |
Correct |
41 ms |
7288 KB |
Output is correct |
69 |
Correct |
46 ms |
7172 KB |
Output is correct |
70 |
Correct |
520 ms |
38904 KB |
Output is correct |
71 |
Correct |
47 ms |
7260 KB |
Output is correct |
72 |
Correct |
401 ms |
68748 KB |
Output is correct |
73 |
Correct |
446 ms |
68764 KB |
Output is correct |
74 |
Correct |
1 ms |
320 KB |
Output is correct |
75 |
Correct |
0 ms |
324 KB |
Output is correct |
76 |
Correct |
1 ms |
324 KB |
Output is correct |
77 |
Correct |
1 ms |
212 KB |
Output is correct |
78 |
Correct |
0 ms |
316 KB |
Output is correct |
79 |
Correct |
6 ms |
1240 KB |
Output is correct |
80 |
Correct |
656 ms |
68748 KB |
Output is correct |
81 |
Correct |
611 ms |
68828 KB |
Output is correct |
82 |
Correct |
533 ms |
68768 KB |
Output is correct |
83 |
Correct |
543 ms |
68792 KB |
Output is correct |
84 |
Correct |
535 ms |
68856 KB |
Output is correct |
85 |
Correct |
530 ms |
68796 KB |
Output is correct |
86 |
Correct |
107 ms |
26660 KB |
Output is correct |
87 |
Correct |
1 ms |
324 KB |
Output is correct |