#include <cmath>
#include <cassert>
#include <iostream>
#include <set>
#include <climits>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <type_traits>
#include <string>
#include <queue>
#include <map>
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("Ofast")
#pragma GCC optimization ("unroll-loops")
using namespace std;
class Intervals {
public:
set<pair<int64_t,int64_t>> intervals;
void print () {
for (auto& p: intervals) {
cout << p.first << " " << p.second << '\n';
}
cout << '\n';
}
pair<int64_t,int64_t> leftIntersect (pair<int64_t,int64_t> p) {
auto it = intervals.upper_bound({p.first + 1, -1});
if (it == intervals.begin()) {
return {-1, -1};
}
it--;
pair<int64_t,int64_t> q = *it;
if (q.second < p.first) {
return {-1, -1};
}
return q;
}
pair<int64_t,int64_t> rightIntersect (pair<int64_t,int64_t> p) {
auto it = intervals.upper_bound({p.second + 1, -1});
if (it == intervals.begin()) {
return {-1, -1};
}
it--;
pair<int64_t,int64_t> q = *it;
if (q.first > p.second) {
return {-1, -1};
}
if (q.first < p.first) {
return {-1, -1};
}
return q;
}
bool willChange (pair<int64_t,int64_t> p) {
pair<int64_t,int64_t> q = leftIntersect(p);
if (q.first < 0 && q.second < 0) {
return true;
}
if (q.first <= p.first && q.second >= p.second) {
return false;
}
return true;
}
pair<int64_t,int64_t> update_left (pair<int64_t,int64_t> p) {
pair<int64_t,int64_t> q = leftIntersect(p);
if (q.first == -1 && q.second == -1) {
return p;
}
if (q.second < p.first) {
return p;
}
if (intervals.count(q)) intervals.erase(q);
return {q.first, p.second};
}
pair<int64_t,int64_t> update_right (pair<int64_t,int64_t> p) {
pair<int64_t,int64_t> q = rightIntersect(p);
if (q.first == -1 && q.second == -1) {
return p;
}
if (q.first > p.second) {
return p;
}
if (intervals.count(q)) intervals.erase(q);
return {p.first, q.second};
}
void erase_in_between (pair<int64_t,int64_t> p) {
auto it = intervals.lower_bound({p.first, -1});
set<pair<int64_t,int64_t>> to_erase;
while (it != intervals.end()) {
if ((*it).second < p.second) {
to_erase.insert(*it);
} else {
break;
}
it++;
}
for (auto& q: to_erase) {
intervals.erase(q);
}
}
void add_interval (pair<int64_t,int64_t> p) {
if (!willChange(p)) {
return;
}
erase_in_between(p);
pair<int64_t,int64_t> new_p = update_left(p);
pair<int64_t,int64_t> new_p1 = update_right(new_p);
intervals.insert(new_p1);
}
};
int64_t gcd (int64_t a, int64_t b) {
if (!a || !b) return max(a,b);
return gcd(max(a,b) % min(a,b), min(a,b));
}
int main () {
int N;
int64_t A, B;
cin >> N >> A >> B;
A /= gcd(A, B + 1);
int64_t prod;
if (A < 1e18/B) {
prod = A * B;
} else {
prod = 1e18;
}
Intervals myInterval;
for (int i = 0; i < N; i++) {
int64_t x, y;
cin >> x >> y;
if (abs(y - x) >= prod) {
myInterval.add_interval({0, prod - 1});
continue;
}
x %= (prod), y %= (prod);
if (x <= y) {
//cout << "T1\n";
myInterval.add_interval({x, y});
} else {
//cout << "T2\n";
swap(x, y);
myInterval.add_interval({0, x});
myInterval.add_interval({y, prod - 1});
}
}
// myInterval.print();
int64_t ans = 0;
for (auto& p: myInterval.intervals) {
ans += p.second - p.first + 1;
}
cout << ans << '\n';
}
Compilation message
strange_device.cpp:15: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
15 | #pragma GCC optimization ("O3")
|
strange_device.cpp:16: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
16 | #pragma GCC optimization ("Ofast")
|
strange_device.cpp:17: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
17 | #pragma GCC optimization ("unroll-loops")
|
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
16 ms |
1172 KB |
Output is correct |
3 |
Correct |
16 ms |
1228 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
288 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
288 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
16 ms |
1196 KB |
Output is correct |
17 |
Correct |
176 ms |
7120 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
861 ms |
1048 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1788 ms |
63580 KB |
Output is correct |
3 |
Correct |
1841 ms |
63520 KB |
Output is correct |
4 |
Correct |
2013 ms |
63536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1788 ms |
63580 KB |
Output is correct |
3 |
Correct |
1841 ms |
63520 KB |
Output is correct |
4 |
Correct |
2013 ms |
63536 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1796 ms |
63460 KB |
Output is correct |
7 |
Correct |
1893 ms |
63540 KB |
Output is correct |
8 |
Correct |
1817 ms |
63736 KB |
Output is correct |
9 |
Correct |
2129 ms |
63556 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1788 ms |
63580 KB |
Output is correct |
3 |
Correct |
1841 ms |
63520 KB |
Output is correct |
4 |
Correct |
2013 ms |
63536 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
167 ms |
7132 KB |
Output is correct |
7 |
Correct |
165 ms |
7028 KB |
Output is correct |
8 |
Correct |
165 ms |
7132 KB |
Output is correct |
9 |
Correct |
174 ms |
7132 KB |
Output is correct |
10 |
Correct |
170 ms |
7016 KB |
Output is correct |
11 |
Correct |
159 ms |
7044 KB |
Output is correct |
12 |
Correct |
167 ms |
7112 KB |
Output is correct |
13 |
Correct |
180 ms |
7084 KB |
Output is correct |
14 |
Correct |
161 ms |
7088 KB |
Output is correct |
15 |
Correct |
200 ms |
7076 KB |
Output is correct |
16 |
Correct |
190 ms |
7120 KB |
Output is correct |
17 |
Correct |
185 ms |
7212 KB |
Output is correct |
18 |
Correct |
1789 ms |
63388 KB |
Output is correct |
19 |
Correct |
1958 ms |
63364 KB |
Output is correct |
20 |
Correct |
2074 ms |
63192 KB |
Output is correct |
21 |
Correct |
166 ms |
6856 KB |
Output is correct |
22 |
Correct |
175 ms |
6916 KB |
Output is correct |
23 |
Correct |
401 ms |
764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
180 ms |
6872 KB |
Output is correct |
3 |
Correct |
196 ms |
6980 KB |
Output is correct |
4 |
Correct |
1930 ms |
63776 KB |
Output is correct |
5 |
Correct |
190 ms |
7068 KB |
Output is correct |
6 |
Correct |
190 ms |
7044 KB |
Output is correct |
7 |
Correct |
229 ms |
7044 KB |
Output is correct |
8 |
Correct |
184 ms |
6940 KB |
Output is correct |
9 |
Correct |
176 ms |
6980 KB |
Output is correct |
10 |
Correct |
168 ms |
6932 KB |
Output is correct |
11 |
Correct |
171 ms |
6980 KB |
Output is correct |
12 |
Correct |
171 ms |
6912 KB |
Output is correct |
13 |
Correct |
190 ms |
6976 KB |
Output is correct |
14 |
Correct |
2064 ms |
63712 KB |
Output is correct |
15 |
Correct |
170 ms |
6276 KB |
Output is correct |
16 |
Correct |
1779 ms |
63052 KB |
Output is correct |
17 |
Correct |
1754 ms |
63412 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
16 ms |
1172 KB |
Output is correct |
3 |
Correct |
16 ms |
1228 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
288 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
288 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
16 ms |
1196 KB |
Output is correct |
17 |
Correct |
176 ms |
7120 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
21 |
Correct |
0 ms |
204 KB |
Output is correct |
22 |
Correct |
0 ms |
204 KB |
Output is correct |
23 |
Correct |
0 ms |
204 KB |
Output is correct |
24 |
Correct |
0 ms |
204 KB |
Output is correct |
25 |
Correct |
2 ms |
332 KB |
Output is correct |
26 |
Correct |
2 ms |
332 KB |
Output is correct |
27 |
Correct |
2 ms |
332 KB |
Output is correct |
28 |
Correct |
861 ms |
1048 KB |
Output is correct |
29 |
Correct |
0 ms |
204 KB |
Output is correct |
30 |
Correct |
1788 ms |
63580 KB |
Output is correct |
31 |
Correct |
1841 ms |
63520 KB |
Output is correct |
32 |
Correct |
2013 ms |
63536 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
1796 ms |
63460 KB |
Output is correct |
35 |
Correct |
1893 ms |
63540 KB |
Output is correct |
36 |
Correct |
1817 ms |
63736 KB |
Output is correct |
37 |
Correct |
2129 ms |
63556 KB |
Output is correct |
38 |
Correct |
0 ms |
204 KB |
Output is correct |
39 |
Correct |
167 ms |
7132 KB |
Output is correct |
40 |
Correct |
165 ms |
7028 KB |
Output is correct |
41 |
Correct |
165 ms |
7132 KB |
Output is correct |
42 |
Correct |
174 ms |
7132 KB |
Output is correct |
43 |
Correct |
170 ms |
7016 KB |
Output is correct |
44 |
Correct |
159 ms |
7044 KB |
Output is correct |
45 |
Correct |
167 ms |
7112 KB |
Output is correct |
46 |
Correct |
180 ms |
7084 KB |
Output is correct |
47 |
Correct |
161 ms |
7088 KB |
Output is correct |
48 |
Correct |
200 ms |
7076 KB |
Output is correct |
49 |
Correct |
190 ms |
7120 KB |
Output is correct |
50 |
Correct |
185 ms |
7212 KB |
Output is correct |
51 |
Correct |
1789 ms |
63388 KB |
Output is correct |
52 |
Correct |
1958 ms |
63364 KB |
Output is correct |
53 |
Correct |
2074 ms |
63192 KB |
Output is correct |
54 |
Correct |
166 ms |
6856 KB |
Output is correct |
55 |
Correct |
175 ms |
6916 KB |
Output is correct |
56 |
Correct |
401 ms |
764 KB |
Output is correct |
57 |
Correct |
1 ms |
204 KB |
Output is correct |
58 |
Correct |
180 ms |
6872 KB |
Output is correct |
59 |
Correct |
196 ms |
6980 KB |
Output is correct |
60 |
Correct |
1930 ms |
63776 KB |
Output is correct |
61 |
Correct |
190 ms |
7068 KB |
Output is correct |
62 |
Correct |
190 ms |
7044 KB |
Output is correct |
63 |
Correct |
229 ms |
7044 KB |
Output is correct |
64 |
Correct |
184 ms |
6940 KB |
Output is correct |
65 |
Correct |
176 ms |
6980 KB |
Output is correct |
66 |
Correct |
168 ms |
6932 KB |
Output is correct |
67 |
Correct |
171 ms |
6980 KB |
Output is correct |
68 |
Correct |
171 ms |
6912 KB |
Output is correct |
69 |
Correct |
190 ms |
6976 KB |
Output is correct |
70 |
Correct |
2064 ms |
63712 KB |
Output is correct |
71 |
Correct |
170 ms |
6276 KB |
Output is correct |
72 |
Correct |
1779 ms |
63052 KB |
Output is correct |
73 |
Correct |
1754 ms |
63412 KB |
Output is correct |
74 |
Correct |
0 ms |
204 KB |
Output is correct |
75 |
Correct |
0 ms |
296 KB |
Output is correct |
76 |
Correct |
1 ms |
204 KB |
Output is correct |
77 |
Correct |
0 ms |
292 KB |
Output is correct |
78 |
Correct |
0 ms |
296 KB |
Output is correct |
79 |
Correct |
16 ms |
1228 KB |
Output is correct |
80 |
Correct |
2039 ms |
63172 KB |
Output is correct |
81 |
Correct |
1963 ms |
63156 KB |
Output is correct |
82 |
Correct |
2111 ms |
63088 KB |
Output is correct |
83 |
Correct |
2059 ms |
63096 KB |
Output is correct |
84 |
Correct |
2235 ms |
63040 KB |
Output is correct |
85 |
Correct |
2001 ms |
63084 KB |
Output is correct |
86 |
Correct |
406 ms |
460 KB |
Output is correct |
87 |
Correct |
0 ms |
204 KB |
Output is correct |