#include<bits/stdc++.h>
#define int long long
#define pii pair<int, int>
using namespace std;
const int MAX=1e18;
signed main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n, a, b; cin >> n >> a >> b;
priority_queue<pii, vector<pii>, greater<pii>> pq;
int S=0;
bool cut=1;
while(n--) {
int l, r; cin >> l >> r;
pq.push({l, r});
S+=r-l+1;
if(S>(int)1e6) cut=0, S=0;
}
if(cut) {
set<pair<int, int>> ans;
while(!pq.empty()) {
int i=pq.top().first, j=pq.top().second;
pq.pop();
while(i<=j) {
while(!pq.empty() && pq.top().first==i) j=max(j, pq.top().second);
ans.insert({(i+i/b)%a, i%b});
i++;
}
}
cout << ans.size() << '\n';
} else {
priority_queue<pii, vector<pii>, greater<pii>> to_ans;
int Mod;
if(a>(MAX+b-1)/b) Mod=1e18+1;
else Mod=a*b;
while(!pq.empty()) {
int l=pq.top().first, r=pq.top().second;
pq.pop();
while(!pq.empty() && pq.top().first<=r) r=max(r, pq.top().second), pq.pop();
if((l+Mod-1)/Mod*Mod+Mod-1<=r) {cout << Mod << '\n'; return 0;}
if((l+Mod-1)/Mod*Mod<=r && l%Mod) to_ans.push({l%Mod, Mod-1}), to_ans.push({0, r%Mod});
else to_ans.push({l%Mod, r%Mod});
}
int ans=0;
while(!to_ans.empty()) {
int l=to_ans.top().first, r=to_ans.top().second;
to_ans.pop();
while(!to_ans.empty() && to_ans.top().first<=r) r=max(r, to_ans.top().second), to_ans.pop();
ans+=r-l+1;
}
cout << ans << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
36 ms |
12560 KB |
Output is correct |
3 |
Correct |
77 ms |
18156 KB |
Output is correct |
4 |
Correct |
2 ms |
852 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
6 ms |
1108 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
37 ms |
6968 KB |
Output is correct |
16 |
Correct |
27 ms |
6996 KB |
Output is correct |
17 |
Correct |
55 ms |
8600 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
316 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
122 ms |
32288 KB |
Output is correct |
3 |
Correct |
147 ms |
32092 KB |
Output is correct |
4 |
Correct |
116 ms |
30648 KB |
Output is correct |
5 |
Correct |
428 ms |
41076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
511 ms |
41160 KB |
Output is correct |
3 |
Incorrect |
452 ms |
40668 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
511 ms |
41160 KB |
Output is correct |
3 |
Incorrect |
452 ms |
40668 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
511 ms |
41160 KB |
Output is correct |
3 |
Incorrect |
452 ms |
40668 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
62 ms |
5268 KB |
Output is correct |
3 |
Correct |
47 ms |
5392 KB |
Output is correct |
4 |
Correct |
465 ms |
40836 KB |
Output is correct |
5 |
Correct |
46 ms |
5368 KB |
Output is correct |
6 |
Correct |
45 ms |
5456 KB |
Output is correct |
7 |
Correct |
61 ms |
5420 KB |
Output is correct |
8 |
Correct |
58 ms |
5316 KB |
Output is correct |
9 |
Correct |
46 ms |
5412 KB |
Output is correct |
10 |
Correct |
57 ms |
5296 KB |
Output is correct |
11 |
Correct |
45 ms |
5320 KB |
Output is correct |
12 |
Correct |
44 ms |
5280 KB |
Output is correct |
13 |
Correct |
50 ms |
5304 KB |
Output is correct |
14 |
Correct |
458 ms |
40688 KB |
Output is correct |
15 |
Incorrect |
48 ms |
5496 KB |
Output isn't correct |
16 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
36 ms |
12560 KB |
Output is correct |
3 |
Correct |
77 ms |
18156 KB |
Output is correct |
4 |
Correct |
2 ms |
852 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
6 ms |
1108 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
37 ms |
6968 KB |
Output is correct |
16 |
Correct |
27 ms |
6996 KB |
Output is correct |
17 |
Correct |
55 ms |
8600 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
316 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
316 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
122 ms |
32288 KB |
Output is correct |
26 |
Correct |
147 ms |
32092 KB |
Output is correct |
27 |
Correct |
116 ms |
30648 KB |
Output is correct |
28 |
Correct |
428 ms |
41076 KB |
Output is correct |
29 |
Correct |
1 ms |
256 KB |
Output is correct |
30 |
Correct |
511 ms |
41160 KB |
Output is correct |
31 |
Incorrect |
452 ms |
40668 KB |
Output isn't correct |
32 |
Halted |
0 ms |
0 KB |
- |