#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);
segmp[bckt].insert({r+1, tmpr}).first;
ans += tmpr - r;
break;
}
}
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);
segmp[bckt].insert({tmpl, l-1}).first;
ans += l - tmpl;
break;
}
}
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) * b;
long tmpr = it->second;
blockmp.erase(it);
blockmp.insert({r+1, tmpr}).first;
ans += (tmpr - r) * b;
break;
}
}
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);
blockmp.insert({tmpl, l-1}).first;
ans += (l - tmpl) * b;
break;
}
}
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:102:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
102 | scanf("%d%lld%lld",&n,&a,&b);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
strange_device.cpp:107:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
107 | scanf("%lld%lld",&l,&r);
| ~~~~~^~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
9 ms |
1408 KB |
Output is correct |
3 |
Correct |
9 ms |
1280 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
1 ms |
256 KB |
Output is correct |
7 |
Correct |
1 ms |
256 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
256 KB |
Output is correct |
10 |
Correct |
1 ms |
256 KB |
Output is correct |
11 |
Correct |
1 ms |
256 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
0 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
1 ms |
288 KB |
Output is correct |
16 |
Correct |
10 ms |
1408 KB |
Output is correct |
17 |
Correct |
98 ms |
10364 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 |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
5 ms |
512 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
512 KB |
Output is correct |
5 |
Correct |
459 ms |
25592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
1522 ms |
157124 KB |
Output is correct |
3 |
Correct |
4476 ms |
376928 KB |
Output is correct |
4 |
Correct |
4744 ms |
376376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
1522 ms |
157124 KB |
Output is correct |
3 |
Correct |
4476 ms |
376928 KB |
Output is correct |
4 |
Correct |
4744 ms |
376376 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
3775 ms |
282224 KB |
Output is correct |
7 |
Correct |
1301 ms |
131576 KB |
Output is correct |
8 |
Correct |
3681 ms |
319324 KB |
Output is correct |
9 |
Correct |
4698 ms |
319476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
1522 ms |
157124 KB |
Output is correct |
3 |
Correct |
4476 ms |
376928 KB |
Output is correct |
4 |
Correct |
4744 ms |
376376 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
174 ms |
23672 KB |
Output is correct |
7 |
Correct |
251 ms |
29944 KB |
Output is correct |
8 |
Correct |
268 ms |
32120 KB |
Output is correct |
9 |
Correct |
325 ms |
32376 KB |
Output is correct |
10 |
Correct |
154 ms |
21752 KB |
Output is correct |
11 |
Correct |
243 ms |
32120 KB |
Output is correct |
12 |
Correct |
282 ms |
32232 KB |
Output is correct |
13 |
Correct |
319 ms |
32248 KB |
Output is correct |
14 |
Correct |
91 ms |
10232 KB |
Output is correct |
15 |
Correct |
188 ms |
26360 KB |
Output is correct |
16 |
Correct |
86 ms |
10616 KB |
Output is correct |
17 |
Correct |
342 ms |
32248 KB |
Output is correct |
18 |
Correct |
3692 ms |
319512 KB |
Output is correct |
19 |
Correct |
4033 ms |
319592 KB |
Output is correct |
20 |
Correct |
4506 ms |
319356 KB |
Output is correct |
21 |
Correct |
285 ms |
32248 KB |
Output is correct |
22 |
Correct |
312 ms |
32252 KB |
Output is correct |
23 |
Correct |
211 ms |
12920 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
90 ms |
9664 KB |
Output is correct |
3 |
Correct |
88 ms |
10036 KB |
Output is correct |
4 |
Correct |
1288 ms |
126972 KB |
Output is correct |
5 |
Correct |
117 ms |
11064 KB |
Output is correct |
6 |
Correct |
98 ms |
10872 KB |
Output is correct |
7 |
Correct |
92 ms |
11000 KB |
Output is correct |
8 |
Correct |
104 ms |
13048 KB |
Output is correct |
9 |
Correct |
101 ms |
13048 KB |
Output is correct |
10 |
Correct |
93 ms |
9592 KB |
Output is correct |
11 |
Correct |
103 ms |
9464 KB |
Output is correct |
12 |
Correct |
82 ms |
9464 KB |
Output is correct |
13 |
Correct |
92 ms |
11256 KB |
Output is correct |
14 |
Correct |
863 ms |
63756 KB |
Output is correct |
15 |
Correct |
110 ms |
9336 KB |
Output is correct |
16 |
Correct |
942 ms |
63580 KB |
Output is correct |
17 |
Correct |
1301 ms |
122364 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
9 ms |
1408 KB |
Output is correct |
3 |
Correct |
9 ms |
1280 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
1 ms |
256 KB |
Output is correct |
7 |
Correct |
1 ms |
256 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
256 KB |
Output is correct |
10 |
Correct |
1 ms |
256 KB |
Output is correct |
11 |
Correct |
1 ms |
256 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
0 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
1 ms |
288 KB |
Output is correct |
16 |
Correct |
10 ms |
1408 KB |
Output is correct |
17 |
Correct |
98 ms |
10364 KB |
Output is correct |
18 |
Correct |
0 ms |
256 KB |
Output is correct |
19 |
Correct |
0 ms |
256 KB |
Output is correct |
20 |
Correct |
1 ms |
256 KB |
Output is correct |
21 |
Correct |
1 ms |
256 KB |
Output is correct |
22 |
Correct |
1 ms |
256 KB |
Output is correct |
23 |
Correct |
1 ms |
256 KB |
Output is correct |
24 |
Correct |
1 ms |
256 KB |
Output is correct |
25 |
Correct |
5 ms |
512 KB |
Output is correct |
26 |
Correct |
2 ms |
384 KB |
Output is correct |
27 |
Correct |
2 ms |
512 KB |
Output is correct |
28 |
Correct |
459 ms |
25592 KB |
Output is correct |
29 |
Correct |
1 ms |
256 KB |
Output is correct |
30 |
Correct |
1522 ms |
157124 KB |
Output is correct |
31 |
Correct |
4476 ms |
376928 KB |
Output is correct |
32 |
Correct |
4744 ms |
376376 KB |
Output is correct |
33 |
Correct |
1 ms |
256 KB |
Output is correct |
34 |
Correct |
3775 ms |
282224 KB |
Output is correct |
35 |
Correct |
1301 ms |
131576 KB |
Output is correct |
36 |
Correct |
3681 ms |
319324 KB |
Output is correct |
37 |
Correct |
4698 ms |
319476 KB |
Output is correct |
38 |
Correct |
0 ms |
256 KB |
Output is correct |
39 |
Correct |
174 ms |
23672 KB |
Output is correct |
40 |
Correct |
251 ms |
29944 KB |
Output is correct |
41 |
Correct |
268 ms |
32120 KB |
Output is correct |
42 |
Correct |
325 ms |
32376 KB |
Output is correct |
43 |
Correct |
154 ms |
21752 KB |
Output is correct |
44 |
Correct |
243 ms |
32120 KB |
Output is correct |
45 |
Correct |
282 ms |
32232 KB |
Output is correct |
46 |
Correct |
319 ms |
32248 KB |
Output is correct |
47 |
Correct |
91 ms |
10232 KB |
Output is correct |
48 |
Correct |
188 ms |
26360 KB |
Output is correct |
49 |
Correct |
86 ms |
10616 KB |
Output is correct |
50 |
Correct |
342 ms |
32248 KB |
Output is correct |
51 |
Correct |
3692 ms |
319512 KB |
Output is correct |
52 |
Correct |
4033 ms |
319592 KB |
Output is correct |
53 |
Correct |
4506 ms |
319356 KB |
Output is correct |
54 |
Correct |
285 ms |
32248 KB |
Output is correct |
55 |
Correct |
312 ms |
32252 KB |
Output is correct |
56 |
Correct |
211 ms |
12920 KB |
Output is correct |
57 |
Correct |
1 ms |
256 KB |
Output is correct |
58 |
Correct |
90 ms |
9664 KB |
Output is correct |
59 |
Correct |
88 ms |
10036 KB |
Output is correct |
60 |
Correct |
1288 ms |
126972 KB |
Output is correct |
61 |
Correct |
117 ms |
11064 KB |
Output is correct |
62 |
Correct |
98 ms |
10872 KB |
Output is correct |
63 |
Correct |
92 ms |
11000 KB |
Output is correct |
64 |
Correct |
104 ms |
13048 KB |
Output is correct |
65 |
Correct |
101 ms |
13048 KB |
Output is correct |
66 |
Correct |
93 ms |
9592 KB |
Output is correct |
67 |
Correct |
103 ms |
9464 KB |
Output is correct |
68 |
Correct |
82 ms |
9464 KB |
Output is correct |
69 |
Correct |
92 ms |
11256 KB |
Output is correct |
70 |
Correct |
863 ms |
63756 KB |
Output is correct |
71 |
Correct |
110 ms |
9336 KB |
Output is correct |
72 |
Correct |
942 ms |
63580 KB |
Output is correct |
73 |
Correct |
1301 ms |
122364 KB |
Output is correct |
74 |
Correct |
1 ms |
256 KB |
Output is correct |
75 |
Correct |
1 ms |
256 KB |
Output is correct |
76 |
Correct |
1 ms |
256 KB |
Output is correct |
77 |
Correct |
0 ms |
256 KB |
Output is correct |
78 |
Correct |
1 ms |
256 KB |
Output is correct |
79 |
Correct |
14 ms |
2328 KB |
Output is correct |
80 |
Correct |
935 ms |
102816 KB |
Output is correct |
81 |
Correct |
1444 ms |
163504 KB |
Output is correct |
82 |
Correct |
4757 ms |
319692 KB |
Output is correct |
83 |
Correct |
4618 ms |
319276 KB |
Output is correct |
84 |
Correct |
4717 ms |
319344 KB |
Output is correct |
85 |
Correct |
4179 ms |
319296 KB |
Output is correct |
86 |
Correct |
203 ms |
12920 KB |
Output is correct |
87 |
Correct |
1 ms |
256 KB |
Output is correct |