#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define se second
#define fi first
#define mp make_pair
int n;
LL A,B;
LL L[1000005], R[1000005];
set < pair < LL , LL > > S;
LL gcd( LL a , LL b ){
if ( b == 0 ) return a;
return gcd(b,a%b);
}
LL add( LL l, LL r ){
LL ans = 0;
while ( true ){
auto it = S.lower_bound({l,-1});
if ( it != S.end() ){
LL ll = it->se, rr = it->fi;
if ( rr < l || ll > r ) break;
if ( ll < l ){
if ( rr <= r ){
S.erase(it);
ans += rr - l + 1;
S.insert({l-1,ll});
} else if ( rr > r ){
S.erase(it);
ans += r - l + 1;
S.insert({l-1,ll});
S.insert({rr,r+1});
}
} else if ( ll >= l ){
if ( rr <= r ){
ans += rr - ll + 1;
S.erase(it);
} else if (rr > r) {
S.erase(it);
ans += r - ll + 1;
S.insert({rr,r+1});
}
}
} else break;
}
while ( true ){
auto it = S.lower_bound({l,-1});
if ( it != S.begin() ){
it--;
LL ll = it->se, rr = it->fi;
if ( rr < l || ll > r ) break;
if ( ll < l ){
if ( rr <= r ){
S.erase(it);
ans += rr - l + 1;
S.insert({l-1,ll});
} else if ( rr > r ){
S.erase(it);
ans += r - l + 1;
S.insert({l-1,ll});
S.insert({rr,r+1});
}
} else if ( ll >= l ){
if ( rr <= r ){
ans += rr - ll + 1;
S.erase(it);
} else if (rr > r) {
S.erase(it);
ans += r - ll + 1;
S.insert({rr,r+1});
}
}
} else break;
}
return ans;
}
int main(){
scanf("%d%lld%lld",&n,&A,&B);
for ( int i = 1; i <= n; i++ ){
scanf("%lld%lld",&L[i],&R[i]);
}
LL g = gcd(A,B+1);
LL ag = A / g;
if ( R[n] / ag + ((R[n] % ag) > 0) < B ){
LL ans = 0;
for ( int i = 1; i <= n; i++ ){
ans += R[i] - L[i] + 1;
}
printf("%lld\n",ans);
} else {
LL ans = 0;
LL cap = ag * 1LL * B;
S.clear();
S.insert({cap-1,0});
for ( int i = 1; i <= n; i++ ){
if ( (R[i] - L[i] + 1) / cap > 0 ){
ans = cap;
break;
} else {
if ( L[i] % cap <= R[i] % cap ){
ans += add(L[i]%cap, R[i]%cap);
} else {
ans += add(L[i] % cap, cap - 1) + add(0,R[i] % cap);
}
}
}
printf("%lld\n",ans);
}
return 0;
}
Compilation message
strange_device.cpp: In function 'int main()':
strange_device.cpp:84:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
84 | scanf("%d%lld%lld",&n,&A,&B);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
strange_device.cpp:86:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
86 | scanf("%lld%lld",&L[i],&R[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
5 ms |
852 KB |
Output is correct |
3 |
Correct |
5 ms |
852 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 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 |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
5 ms |
852 KB |
Output is correct |
17 |
Correct |
45 ms |
5540 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
308 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 |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
298 ms |
41120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
451 ms |
53172 KB |
Output is correct |
3 |
Correct |
437 ms |
53172 KB |
Output is correct |
4 |
Correct |
420 ms |
53052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
451 ms |
53172 KB |
Output is correct |
3 |
Correct |
437 ms |
53172 KB |
Output is correct |
4 |
Correct |
420 ms |
53052 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
434 ms |
53088 KB |
Output is correct |
7 |
Correct |
449 ms |
53036 KB |
Output is correct |
8 |
Correct |
435 ms |
52996 KB |
Output is correct |
9 |
Correct |
420 ms |
53072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
451 ms |
53172 KB |
Output is correct |
3 |
Correct |
437 ms |
53172 KB |
Output is correct |
4 |
Correct |
420 ms |
53052 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
50 ms |
5516 KB |
Output is correct |
7 |
Correct |
44 ms |
5568 KB |
Output is correct |
8 |
Correct |
41 ms |
5496 KB |
Output is correct |
9 |
Correct |
42 ms |
5648 KB |
Output is correct |
10 |
Correct |
45 ms |
5524 KB |
Output is correct |
11 |
Correct |
42 ms |
5568 KB |
Output is correct |
12 |
Correct |
41 ms |
5564 KB |
Output is correct |
13 |
Correct |
43 ms |
5584 KB |
Output is correct |
14 |
Correct |
42 ms |
5604 KB |
Output is correct |
15 |
Correct |
44 ms |
5548 KB |
Output is correct |
16 |
Correct |
48 ms |
5480 KB |
Output is correct |
17 |
Correct |
51 ms |
5528 KB |
Output is correct |
18 |
Correct |
438 ms |
53028 KB |
Output is correct |
19 |
Correct |
463 ms |
53168 KB |
Output is correct |
20 |
Correct |
411 ms |
53148 KB |
Output is correct |
21 |
Correct |
53 ms |
5596 KB |
Output is correct |
22 |
Correct |
48 ms |
5592 KB |
Output is correct |
23 |
Correct |
128 ms |
18240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
45 ms |
5604 KB |
Output is correct |
3 |
Correct |
45 ms |
5524 KB |
Output is correct |
4 |
Correct |
416 ms |
53064 KB |
Output is correct |
5 |
Correct |
43 ms |
5484 KB |
Output is correct |
6 |
Correct |
43 ms |
5556 KB |
Output is correct |
7 |
Correct |
43 ms |
5580 KB |
Output is correct |
8 |
Correct |
44 ms |
5596 KB |
Output is correct |
9 |
Correct |
46 ms |
5564 KB |
Output is correct |
10 |
Correct |
43 ms |
5564 KB |
Output is correct |
11 |
Correct |
44 ms |
5592 KB |
Output is correct |
12 |
Correct |
44 ms |
5480 KB |
Output is correct |
13 |
Correct |
44 ms |
5556 KB |
Output is correct |
14 |
Correct |
432 ms |
52944 KB |
Output is correct |
15 |
Correct |
44 ms |
5580 KB |
Output is correct |
16 |
Correct |
438 ms |
52968 KB |
Output is correct |
17 |
Correct |
435 ms |
53004 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
5 ms |
852 KB |
Output is correct |
3 |
Correct |
5 ms |
852 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 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 |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
5 ms |
852 KB |
Output is correct |
17 |
Correct |
45 ms |
5540 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
308 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 |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
320 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
298 ms |
41120 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
451 ms |
53172 KB |
Output is correct |
31 |
Correct |
437 ms |
53172 KB |
Output is correct |
32 |
Correct |
420 ms |
53052 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
434 ms |
53088 KB |
Output is correct |
35 |
Correct |
449 ms |
53036 KB |
Output is correct |
36 |
Correct |
435 ms |
52996 KB |
Output is correct |
37 |
Correct |
420 ms |
53072 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
50 ms |
5516 KB |
Output is correct |
40 |
Correct |
44 ms |
5568 KB |
Output is correct |
41 |
Correct |
41 ms |
5496 KB |
Output is correct |
42 |
Correct |
42 ms |
5648 KB |
Output is correct |
43 |
Correct |
45 ms |
5524 KB |
Output is correct |
44 |
Correct |
42 ms |
5568 KB |
Output is correct |
45 |
Correct |
41 ms |
5564 KB |
Output is correct |
46 |
Correct |
43 ms |
5584 KB |
Output is correct |
47 |
Correct |
42 ms |
5604 KB |
Output is correct |
48 |
Correct |
44 ms |
5548 KB |
Output is correct |
49 |
Correct |
48 ms |
5480 KB |
Output is correct |
50 |
Correct |
51 ms |
5528 KB |
Output is correct |
51 |
Correct |
438 ms |
53028 KB |
Output is correct |
52 |
Correct |
463 ms |
53168 KB |
Output is correct |
53 |
Correct |
411 ms |
53148 KB |
Output is correct |
54 |
Correct |
53 ms |
5596 KB |
Output is correct |
55 |
Correct |
48 ms |
5592 KB |
Output is correct |
56 |
Correct |
128 ms |
18240 KB |
Output is correct |
57 |
Correct |
1 ms |
212 KB |
Output is correct |
58 |
Correct |
45 ms |
5604 KB |
Output is correct |
59 |
Correct |
45 ms |
5524 KB |
Output is correct |
60 |
Correct |
416 ms |
53064 KB |
Output is correct |
61 |
Correct |
43 ms |
5484 KB |
Output is correct |
62 |
Correct |
43 ms |
5556 KB |
Output is correct |
63 |
Correct |
43 ms |
5580 KB |
Output is correct |
64 |
Correct |
44 ms |
5596 KB |
Output is correct |
65 |
Correct |
46 ms |
5564 KB |
Output is correct |
66 |
Correct |
43 ms |
5564 KB |
Output is correct |
67 |
Correct |
44 ms |
5592 KB |
Output is correct |
68 |
Correct |
44 ms |
5480 KB |
Output is correct |
69 |
Correct |
44 ms |
5556 KB |
Output is correct |
70 |
Correct |
432 ms |
52944 KB |
Output is correct |
71 |
Correct |
44 ms |
5580 KB |
Output is correct |
72 |
Correct |
438 ms |
52968 KB |
Output is correct |
73 |
Correct |
435 ms |
53004 KB |
Output is correct |
74 |
Correct |
0 ms |
212 KB |
Output is correct |
75 |
Correct |
0 ms |
212 KB |
Output is correct |
76 |
Correct |
0 ms |
212 KB |
Output is correct |
77 |
Correct |
1 ms |
212 KB |
Output is correct |
78 |
Correct |
1 ms |
212 KB |
Output is correct |
79 |
Correct |
6 ms |
836 KB |
Output is correct |
80 |
Correct |
440 ms |
53160 KB |
Output is correct |
81 |
Correct |
426 ms |
53128 KB |
Output is correct |
82 |
Correct |
460 ms |
53060 KB |
Output is correct |
83 |
Correct |
464 ms |
53080 KB |
Output is correct |
84 |
Correct |
444 ms |
53172 KB |
Output is correct |
85 |
Correct |
429 ms |
53072 KB |
Output is correct |
86 |
Correct |
120 ms |
18252 KB |
Output is correct |
87 |
Correct |
1 ms |
304 KB |
Output is correct |