#include <bits/stdc++.h>
#define int long long
using namespace std;
int gcd(int a, int b) {
a = abs(a);
b = abs(b);
if(a < b) swap(a, b);
while(b) {
a %= b;
swap(a, b);
}
return a;
}
vector<int> B;
int getidx(int n) {
return lower_bound(B.begin(),B.end(),n) - B.begin();
}
struct SegTree {
vector<int> seg;
int MAX;
SegTree(int N) {
int i;
for(i=2;i<2*N;i*=2) {}
seg.resize(i);
MAX = i;
}
int sum(int s, int e, int n, int ns, int ne) {
if(e<=ns||ne<=s) return 0;
if(s<=ns&&ne<=e) return seg[n];
int mid = ns + ne >> 1;
return sum(s, e, 2*n, ns, mid) + sum(s, e, 2*n+1, mid, ne);
}
void update(int n) {
n += MAX/2;
n /= 2;
while(n) {
seg[n] = seg[2*n] + seg[2*n+1];
n /= 2;
}
}
void Plus(int n) {
seg[n+MAX/2]++;
update(n);
}
void Minus(int n) {
seg[n+MAX/2]--;
update(n);
}
int sum(int s, int e) {
return sum(s, e, 1, 0, MAX/2);
}
};
int L[1000005];
int R[1000005];
bool Check(int a, int b) {
int d = 1e9;
int a1 = a / d;
int a2 = a % d;
int b1 = b / d;
int b2 = b % d;
if(a1&&b1) return true;
//cout << a1 * b2 + a2 * b1 + (a2 * b2 / d) ;
if(a1*b2+a2*b1 + (a2*b2/d) >= d) return true;
return false;
}
const int INF = 1e18;
main() {
cin.sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int N, a, b;
cin >> N >> a >> b;
int d = gcd(a, b + 1);
int c = a / d;
assert(a % d == 0);
int k;
if(Check(b, c)) k = INF + 5;
else k = b * c;
//cout << k;
assert(k < INF + 10);
assert(k > 0);
bool isAll = false;
int i;
B.push_back(0);
B.push_back(k);
for(i=0;i<N;i++) {
cin >> L[i] >> R[i];
if(R[i] - L[i] >= k-1) isAll = true;
L[i] %= k;
R[i] %= k;
R[i]++;
B.push_back(L[i]);
B.push_back(R[i]);
}
sort(B.begin(),B.end());
B.erase(unique(B.begin(),B.end()),B.end());
SegTree tree(B.size() + 10);
for(i=0;i<N;i++) {
int l = getidx(L[i]);
int r = getidx(R[i]);
if(l < r) {
tree.Plus(l);
tree.Minus(r);
}
else {
tree.Plus(0);
tree.Minus(r);
tree.Plus(l);
tree.Minus(B.size());
}
}
int cnt = 0;
for(i=0;i<B.size()-1;i++) {
if(tree.sum(0,i+1)!=0) {
cnt += B[i+1]-B[i];
}
}
if(isAll) cnt = k;
cout << cnt;
}
Compilation message
strange_device.cpp: In member function 'long long int SegTree::sum(long long int, long long int, long long int, long long int, long long int)':
strange_device.cpp:30:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
30 | int mid = ns + ne >> 1;
| ~~~^~~~
strange_device.cpp: At global scope:
strange_device.cpp:67:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
67 | main() {
| ^~~~
strange_device.cpp: In function 'int main()':
strange_device.cpp:113:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
113 | for(i=0;i<B.size()-1;i++) {
| ~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
10 ms |
1044 KB |
Output is correct |
3 |
Correct |
9 ms |
1052 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
204 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 |
332 KB |
Output is correct |
16 |
Correct |
10 ms |
976 KB |
Output is correct |
17 |
Correct |
95 ms |
5560 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 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 |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
431 ms |
31644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
987 ms |
48368 KB |
Output is correct |
3 |
Correct |
986 ms |
48172 KB |
Output is correct |
4 |
Correct |
962 ms |
48228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
987 ms |
48368 KB |
Output is correct |
3 |
Correct |
986 ms |
48172 KB |
Output is correct |
4 |
Correct |
962 ms |
48228 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
955 ms |
48136 KB |
Output is correct |
7 |
Correct |
1004 ms |
48160 KB |
Output is correct |
8 |
Correct |
936 ms |
48136 KB |
Output is correct |
9 |
Correct |
1086 ms |
48056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
987 ms |
48368 KB |
Output is correct |
3 |
Correct |
986 ms |
48172 KB |
Output is correct |
4 |
Correct |
962 ms |
48228 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
87 ms |
5640 KB |
Output is correct |
7 |
Correct |
95 ms |
5568 KB |
Output is correct |
8 |
Correct |
81 ms |
5644 KB |
Output is correct |
9 |
Correct |
90 ms |
5560 KB |
Output is correct |
10 |
Correct |
91 ms |
5552 KB |
Output is correct |
11 |
Correct |
91 ms |
5608 KB |
Output is correct |
12 |
Correct |
86 ms |
5608 KB |
Output is correct |
13 |
Correct |
95 ms |
5644 KB |
Output is correct |
14 |
Correct |
86 ms |
5616 KB |
Output is correct |
15 |
Correct |
103 ms |
5688 KB |
Output is correct |
16 |
Correct |
95 ms |
5636 KB |
Output is correct |
17 |
Correct |
89 ms |
5640 KB |
Output is correct |
18 |
Correct |
933 ms |
48204 KB |
Output is correct |
19 |
Correct |
1076 ms |
48092 KB |
Output is correct |
20 |
Correct |
1053 ms |
48172 KB |
Output is correct |
21 |
Correct |
100 ms |
5648 KB |
Output is correct |
22 |
Correct |
86 ms |
5560 KB |
Output is correct |
23 |
Correct |
170 ms |
12740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
116 ms |
5520 KB |
Output is correct |
3 |
Correct |
112 ms |
5636 KB |
Output is correct |
4 |
Correct |
1053 ms |
48172 KB |
Output is correct |
5 |
Correct |
95 ms |
5560 KB |
Output is correct |
6 |
Correct |
96 ms |
5604 KB |
Output is correct |
7 |
Correct |
97 ms |
5596 KB |
Output is correct |
8 |
Correct |
92 ms |
5688 KB |
Output is correct |
9 |
Correct |
94 ms |
5736 KB |
Output is correct |
10 |
Correct |
94 ms |
5532 KB |
Output is correct |
11 |
Correct |
95 ms |
5616 KB |
Output is correct |
12 |
Correct |
86 ms |
5644 KB |
Output is correct |
13 |
Correct |
95 ms |
5640 KB |
Output is correct |
14 |
Correct |
1046 ms |
48248 KB |
Output is correct |
15 |
Correct |
94 ms |
5644 KB |
Output is correct |
16 |
Correct |
991 ms |
48164 KB |
Output is correct |
17 |
Correct |
976 ms |
48172 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 |
10 ms |
1044 KB |
Output is correct |
3 |
Correct |
9 ms |
1052 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
204 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 |
332 KB |
Output is correct |
16 |
Correct |
10 ms |
976 KB |
Output is correct |
17 |
Correct |
95 ms |
5560 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
1 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 |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
2 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
431 ms |
31644 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
987 ms |
48368 KB |
Output is correct |
31 |
Correct |
986 ms |
48172 KB |
Output is correct |
32 |
Correct |
962 ms |
48228 KB |
Output is correct |
33 |
Correct |
0 ms |
204 KB |
Output is correct |
34 |
Correct |
955 ms |
48136 KB |
Output is correct |
35 |
Correct |
1004 ms |
48160 KB |
Output is correct |
36 |
Correct |
936 ms |
48136 KB |
Output is correct |
37 |
Correct |
1086 ms |
48056 KB |
Output is correct |
38 |
Correct |
1 ms |
204 KB |
Output is correct |
39 |
Correct |
87 ms |
5640 KB |
Output is correct |
40 |
Correct |
95 ms |
5568 KB |
Output is correct |
41 |
Correct |
81 ms |
5644 KB |
Output is correct |
42 |
Correct |
90 ms |
5560 KB |
Output is correct |
43 |
Correct |
91 ms |
5552 KB |
Output is correct |
44 |
Correct |
91 ms |
5608 KB |
Output is correct |
45 |
Correct |
86 ms |
5608 KB |
Output is correct |
46 |
Correct |
95 ms |
5644 KB |
Output is correct |
47 |
Correct |
86 ms |
5616 KB |
Output is correct |
48 |
Correct |
103 ms |
5688 KB |
Output is correct |
49 |
Correct |
95 ms |
5636 KB |
Output is correct |
50 |
Correct |
89 ms |
5640 KB |
Output is correct |
51 |
Correct |
933 ms |
48204 KB |
Output is correct |
52 |
Correct |
1076 ms |
48092 KB |
Output is correct |
53 |
Correct |
1053 ms |
48172 KB |
Output is correct |
54 |
Correct |
100 ms |
5648 KB |
Output is correct |
55 |
Correct |
86 ms |
5560 KB |
Output is correct |
56 |
Correct |
170 ms |
12740 KB |
Output is correct |
57 |
Correct |
1 ms |
204 KB |
Output is correct |
58 |
Correct |
116 ms |
5520 KB |
Output is correct |
59 |
Correct |
112 ms |
5636 KB |
Output is correct |
60 |
Correct |
1053 ms |
48172 KB |
Output is correct |
61 |
Correct |
95 ms |
5560 KB |
Output is correct |
62 |
Correct |
96 ms |
5604 KB |
Output is correct |
63 |
Correct |
97 ms |
5596 KB |
Output is correct |
64 |
Correct |
92 ms |
5688 KB |
Output is correct |
65 |
Correct |
94 ms |
5736 KB |
Output is correct |
66 |
Correct |
94 ms |
5532 KB |
Output is correct |
67 |
Correct |
95 ms |
5616 KB |
Output is correct |
68 |
Correct |
86 ms |
5644 KB |
Output is correct |
69 |
Correct |
95 ms |
5640 KB |
Output is correct |
70 |
Correct |
1046 ms |
48248 KB |
Output is correct |
71 |
Correct |
94 ms |
5644 KB |
Output is correct |
72 |
Correct |
991 ms |
48164 KB |
Output is correct |
73 |
Correct |
976 ms |
48172 KB |
Output is correct |
74 |
Correct |
0 ms |
204 KB |
Output is correct |
75 |
Correct |
1 ms |
204 KB |
Output is correct |
76 |
Correct |
0 ms |
204 KB |
Output is correct |
77 |
Correct |
1 ms |
332 KB |
Output is correct |
78 |
Correct |
1 ms |
204 KB |
Output is correct |
79 |
Correct |
9 ms |
1360 KB |
Output is correct |
80 |
Correct |
1089 ms |
66380 KB |
Output is correct |
81 |
Correct |
1076 ms |
66184 KB |
Output is correct |
82 |
Correct |
1062 ms |
66716 KB |
Output is correct |
83 |
Correct |
1072 ms |
67636 KB |
Output is correct |
84 |
Correct |
1075 ms |
67240 KB |
Output is correct |
85 |
Correct |
1035 ms |
67940 KB |
Output is correct |
86 |
Correct |
177 ms |
23588 KB |
Output is correct |
87 |
Correct |
1 ms |
332 KB |
Output is correct |