#include <bits/stdc++.h>
using namespace std;
#define MASK(x) (1LL << (x))
#define BIT(x, i) (((x) >> (i)) & 1)
#define ALL(x) (x).begin(), (x).end()
#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 FORE(it, s) for (__typeof(s.begin()) it = (s).begin(); it != (s).end(); ++it)
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define file(TASK) \
if (fopen(TASK ".inp", "r")) { \
freopen(TASK ".inp", "r", stdin); \
freopen(TASK ".out", "w", stdout); \
}
template <class U, class V> bool maximize(U &A, const V &B) { return (A < B) ? (A = B, true) : false; }
template <class U, class V> bool minimize(U &A, const V &B) { return (A > B) ? (A = B, true) : false; }
int N;
#define int long long
const int INF = 2e18;
long long A, B;
void process(void) {
cin >> N >> A >> B;
int period = A / __gcd(A, B + 1);
if(INF / B >= period) period *= B;
else period = INF;
vector <pair <int, int>> events;
while(N--) {
int l, r; cin >> l >> r;
if(r - l + 1 >= period) return void(cout << period);
l %= period, r %= period;
if(l <= r) {
events.push_back(make_pair(l, 0));
events.push_back(make_pair(r, 1));
} else {
events.push_back(make_pair(0, 0));
events.push_back(make_pair(r, 1));
events.push_back(make_pair(l, 0));
events.push_back(make_pair(period - 1, 1));
}
}
sort(ALL(events));
int ans = 0, Leftmost = -1, cnt = 0;
for (auto [x, i] : events) {
if(i == 0) {
cnt++;
if(Leftmost == -1) Leftmost = x;
} else {
cnt--;
if(cnt == 0) {
ans += x - Leftmost + 1;
Leftmost = -1;
}
}
}
cout << ans;
}
signed main() {
file("TASK");
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int test = 1;
// cin >> test;
while(test--) {
process();
cout << '\n';
}
cerr << "Time elapsed: " << TIME << " s.\n";
return (0 ^ 0);
}
Compilation message
strange_device.cpp: In function 'int main()':
strange_device.cpp:17:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
17 | freopen(TASK ".inp", "r", stdin); \
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
strange_device.cpp:64:5: note: in expansion of macro 'file'
64 | file("TASK");
| ^~~~
strange_device.cpp:18:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
18 | freopen(TASK ".out", "w", stdout); \
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
strange_device.cpp:64:5: note: in expansion of macro 'file'
64 | file("TASK");
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
7 ms |
856 KB |
Output is correct |
3 |
Correct |
5 ms |
984 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
5 ms |
984 KB |
Output is correct |
17 |
Correct |
65 ms |
4516 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 |
1 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 |
322 ms |
33344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
479 ms |
33188 KB |
Output is correct |
3 |
Correct |
478 ms |
33244 KB |
Output is correct |
4 |
Correct |
455 ms |
33176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
479 ms |
33188 KB |
Output is correct |
3 |
Correct |
478 ms |
33244 KB |
Output is correct |
4 |
Correct |
455 ms |
33176 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
478 ms |
33272 KB |
Output is correct |
7 |
Correct |
460 ms |
33308 KB |
Output is correct |
8 |
Correct |
464 ms |
33176 KB |
Output is correct |
9 |
Correct |
552 ms |
33248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
479 ms |
33188 KB |
Output is correct |
3 |
Correct |
478 ms |
33244 KB |
Output is correct |
4 |
Correct |
455 ms |
33176 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
52 ms |
4552 KB |
Output is correct |
7 |
Correct |
50 ms |
4480 KB |
Output is correct |
8 |
Correct |
46 ms |
4576 KB |
Output is correct |
9 |
Correct |
49 ms |
4544 KB |
Output is correct |
10 |
Correct |
45 ms |
4580 KB |
Output is correct |
11 |
Correct |
48 ms |
4560 KB |
Output is correct |
12 |
Correct |
47 ms |
4452 KB |
Output is correct |
13 |
Correct |
57 ms |
4552 KB |
Output is correct |
14 |
Correct |
51 ms |
4496 KB |
Output is correct |
15 |
Correct |
53 ms |
4556 KB |
Output is correct |
16 |
Correct |
53 ms |
4516 KB |
Output is correct |
17 |
Correct |
46 ms |
4556 KB |
Output is correct |
18 |
Correct |
470 ms |
33480 KB |
Output is correct |
19 |
Correct |
449 ms |
33232 KB |
Output is correct |
20 |
Correct |
564 ms |
33216 KB |
Output is correct |
21 |
Correct |
51 ms |
4556 KB |
Output is correct |
22 |
Correct |
40 ms |
4556 KB |
Output is correct |
23 |
Correct |
134 ms |
16788 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
51 ms |
4460 KB |
Output is correct |
3 |
Correct |
52 ms |
4536 KB |
Output is correct |
4 |
Correct |
600 ms |
33288 KB |
Output is correct |
5 |
Correct |
51 ms |
4556 KB |
Output is correct |
6 |
Correct |
50 ms |
4556 KB |
Output is correct |
7 |
Correct |
53 ms |
4580 KB |
Output is correct |
8 |
Correct |
52 ms |
4556 KB |
Output is correct |
9 |
Correct |
50 ms |
4556 KB |
Output is correct |
10 |
Correct |
52 ms |
4476 KB |
Output is correct |
11 |
Correct |
53 ms |
4544 KB |
Output is correct |
12 |
Correct |
47 ms |
4568 KB |
Output is correct |
13 |
Correct |
52 ms |
4548 KB |
Output is correct |
14 |
Correct |
563 ms |
33200 KB |
Output is correct |
15 |
Correct |
54 ms |
4556 KB |
Output is correct |
16 |
Correct |
454 ms |
33172 KB |
Output is correct |
17 |
Correct |
482 ms |
33180 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
7 ms |
856 KB |
Output is correct |
3 |
Correct |
5 ms |
984 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
5 ms |
984 KB |
Output is correct |
17 |
Correct |
65 ms |
4516 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 |
1 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 |
322 ms |
33344 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
479 ms |
33188 KB |
Output is correct |
31 |
Correct |
478 ms |
33244 KB |
Output is correct |
32 |
Correct |
455 ms |
33176 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
478 ms |
33272 KB |
Output is correct |
35 |
Correct |
460 ms |
33308 KB |
Output is correct |
36 |
Correct |
464 ms |
33176 KB |
Output is correct |
37 |
Correct |
552 ms |
33248 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
52 ms |
4552 KB |
Output is correct |
40 |
Correct |
50 ms |
4480 KB |
Output is correct |
41 |
Correct |
46 ms |
4576 KB |
Output is correct |
42 |
Correct |
49 ms |
4544 KB |
Output is correct |
43 |
Correct |
45 ms |
4580 KB |
Output is correct |
44 |
Correct |
48 ms |
4560 KB |
Output is correct |
45 |
Correct |
47 ms |
4452 KB |
Output is correct |
46 |
Correct |
57 ms |
4552 KB |
Output is correct |
47 |
Correct |
51 ms |
4496 KB |
Output is correct |
48 |
Correct |
53 ms |
4556 KB |
Output is correct |
49 |
Correct |
53 ms |
4516 KB |
Output is correct |
50 |
Correct |
46 ms |
4556 KB |
Output is correct |
51 |
Correct |
470 ms |
33480 KB |
Output is correct |
52 |
Correct |
449 ms |
33232 KB |
Output is correct |
53 |
Correct |
564 ms |
33216 KB |
Output is correct |
54 |
Correct |
51 ms |
4556 KB |
Output is correct |
55 |
Correct |
40 ms |
4556 KB |
Output is correct |
56 |
Correct |
134 ms |
16788 KB |
Output is correct |
57 |
Correct |
0 ms |
212 KB |
Output is correct |
58 |
Correct |
51 ms |
4460 KB |
Output is correct |
59 |
Correct |
52 ms |
4536 KB |
Output is correct |
60 |
Correct |
600 ms |
33288 KB |
Output is correct |
61 |
Correct |
51 ms |
4556 KB |
Output is correct |
62 |
Correct |
50 ms |
4556 KB |
Output is correct |
63 |
Correct |
53 ms |
4580 KB |
Output is correct |
64 |
Correct |
52 ms |
4556 KB |
Output is correct |
65 |
Correct |
50 ms |
4556 KB |
Output is correct |
66 |
Correct |
52 ms |
4476 KB |
Output is correct |
67 |
Correct |
53 ms |
4544 KB |
Output is correct |
68 |
Correct |
47 ms |
4568 KB |
Output is correct |
69 |
Correct |
52 ms |
4548 KB |
Output is correct |
70 |
Correct |
563 ms |
33200 KB |
Output is correct |
71 |
Correct |
54 ms |
4556 KB |
Output is correct |
72 |
Correct |
454 ms |
33172 KB |
Output is correct |
73 |
Correct |
482 ms |
33180 KB |
Output is correct |
74 |
Correct |
1 ms |
212 KB |
Output is correct |
75 |
Correct |
1 ms |
316 KB |
Output is correct |
76 |
Correct |
1 ms |
320 KB |
Output is correct |
77 |
Correct |
1 ms |
212 KB |
Output is correct |
78 |
Correct |
1 ms |
212 KB |
Output is correct |
79 |
Correct |
5 ms |
1240 KB |
Output is correct |
80 |
Correct |
612 ms |
45436 KB |
Output is correct |
81 |
Correct |
607 ms |
45572 KB |
Output is correct |
82 |
Correct |
561 ms |
45440 KB |
Output is correct |
83 |
Correct |
572 ms |
45504 KB |
Output is correct |
84 |
Correct |
608 ms |
45392 KB |
Output is correct |
85 |
Correct |
545 ms |
45616 KB |
Output is correct |
86 |
Correct |
142 ms |
26668 KB |
Output is correct |
87 |
Correct |
1 ms |
256 KB |
Output is correct |