#include <bits/stdc++.h>
#define long long long
using namespace std;
long a, b;
set<pair<long,long> > blockmp;
map<long, set<pair<long,long> > > segmp;
long ans;
void addSegment(long bckt, long l, long r){
auto bit = blockmp.lower_bound({bckt+1,0});
if(bit != blockmp.begin()){
bit--;
if(bit->second >= bckt) return;
}
auto cit = segmp[bckt].lower_bound({l+1, 0});
if(cit != segmp[bckt].begin()){
cit--;
if(cit->second >= r) return;
}
auto it = segmp[bckt].lower_bound({l, 0});
while(it != segmp[bckt].end() && it->first <= r){
if(it->second <= r){
ans -= it->second - it->first + 1;
it = segmp[bckt].erase(it);
}else{
ans -= it->second - it->first + 1;
long tmpr = it->second;
segmp[bckt].erase(it);
it = segmp[bckt].insert({r+1, tmpr}).first;
ans += tmpr - r;
}
}
it = segmp[bckt].lower_bound({r+1, 0});
while(it != segmp[bckt].begin()){
--it;
if(it->second < l) break;
if(it->first >= l){
ans -= it->second - it->first + 1;
it = segmp[bckt].erase(it);
}else{
ans -= it->second - it->first + 1;
long tmpl = it->first;
segmp[bckt].erase(it);
it = segmp[bckt].insert({tmpl, l-1}).first;
ans += l - tmpl;
}
}
segmp[bckt].emplace(l, r);
ans += r - l + 1;
}
void addBlock(long l, long r){
auto bit = blockmp.lower_bound({l+1,0});
if(bit != blockmp.begin()){
bit--;
if(bit->second >= r) return;
}
auto st = segmp.lower_bound(l);
auto ft = segmp.upper_bound(r);
vector<long> v;
for(auto it = st; it != ft; it++){
for(auto p : it->second) ans -= p.second - p.first + 1;
it->second.clear();
v.push_back(it->first);
}
for(long x : v) segmp.erase(x);
auto it = blockmp.lower_bound({l, 0});
while(it != blockmp.end() && it->first <= r){
if(it->second <= r){
ans -= (it->second - it->first + 1) * b;
it = blockmp.erase(it);
}else{
ans -= it->second - it->first + 1;
long tmpr = it->second;
blockmp.erase(it);
it = blockmp.insert({r+1, tmpr}).first;
ans += (tmpr - r) * b;
}
}
it = blockmp.lower_bound({r+1, 0});
while(it != blockmp.begin()){
--it;
if(it->second < l) break;
if(it->first >= l){
ans -= (it->second - it->first + 1) * b;
it = blockmp.erase(it);
}else{
ans -= (it->second - it->first + 1) * b;
long tmpl = it->first;
blockmp.erase(it);
it = blockmp.insert({tmpl, l-1}).first;
ans += (l - tmpl) * b;
}
}
ans += (r-l+1) * b;
blockmp.emplace(l, r);
}
int main(){
int n;
scanf("%d%lld%lld",&n,&a,&b);
long g = __gcd(a, b+1ll);
long crit = a / g;
for(int i = 1; i <= n; i++){
long l, r;
scanf("%lld%lld",&l,&r);
if(l/b == r/b){
addSegment((l/b) % crit, l % b, r % b);
continue;
}
long sa = l / b;
addSegment(sa % crit, l % b, b-1);
long fa = r / b;
addSegment(fa % crit, 0, r % b);
long res = (sa+1) % crit;
if((fa-1) - (sa+1) >= crit){
// answer = crit * b
printf("%lld\n", crit * b);
return 0;
}
if(res + (fa-1) - (sa+1) >= crit){
// res to crit-1
addBlock(res, crit-1);
// 0 to (fa-1) % crit
addBlock(0, (fa-1) % crit);
}else if(sa+1 <= fa-1){
addBlock(res, (fa-1) % crit);
}
}
printf("%lld\n", ans);
return 0;
}
Compilation message
strange_device.cpp: In function 'int main()':
strange_device.cpp:98:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
98 | scanf("%d%lld%lld",&n,&a,&b);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
strange_device.cpp:103:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
103 | scanf("%lld%lld",&l,&r);
| ~~~~~^~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
10 ms |
1280 KB |
Output is correct |
3 |
Correct |
9 ms |
1280 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
1 ms |
256 KB |
Output is correct |
8 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
368 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Incorrect |
3 ms |
512 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
1515 ms |
157248 KB |
Output is correct |
3 |
Correct |
4428 ms |
377180 KB |
Output is correct |
4 |
Correct |
4840 ms |
376368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
1515 ms |
157248 KB |
Output is correct |
3 |
Correct |
4428 ms |
377180 KB |
Output is correct |
4 |
Correct |
4840 ms |
376368 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Incorrect |
3798 ms |
282296 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
1515 ms |
157248 KB |
Output is correct |
3 |
Correct |
4428 ms |
377180 KB |
Output is correct |
4 |
Correct |
4840 ms |
376368 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
172 ms |
23488 KB |
Output is correct |
7 |
Incorrect |
251 ms |
29688 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
92 ms |
7672 KB |
Output is correct |
3 |
Correct |
88 ms |
8056 KB |
Output is correct |
4 |
Correct |
1301 ms |
126768 KB |
Output is correct |
5 |
Correct |
93 ms |
8952 KB |
Output is correct |
6 |
Correct |
93 ms |
8952 KB |
Output is correct |
7 |
Correct |
93 ms |
8952 KB |
Output is correct |
8 |
Correct |
103 ms |
11000 KB |
Output is correct |
9 |
Correct |
104 ms |
11128 KB |
Output is correct |
10 |
Correct |
83 ms |
7800 KB |
Output is correct |
11 |
Correct |
100 ms |
7672 KB |
Output is correct |
12 |
Correct |
88 ms |
7672 KB |
Output is correct |
13 |
Correct |
94 ms |
9464 KB |
Output is correct |
14 |
Correct |
912 ms |
63736 KB |
Output is correct |
15 |
Correct |
80 ms |
7156 KB |
Output is correct |
16 |
Correct |
953 ms |
63456 KB |
Output is correct |
17 |
Correct |
1308 ms |
122392 KB |
Output is correct |
18 |
Correct |
0 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
10 ms |
1280 KB |
Output is correct |
3 |
Correct |
9 ms |
1280 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
1 ms |
256 KB |
Output is correct |
8 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |