#include <bits/stdc++.h>
#define lp(i,a,b) for(int i = a; i < b; i++)
#define pb push_back
#define sz(x) (int)(x.size())
#define all(x) x.begin(),x.end()
#define debug printf
#define tiii tuple<int,int,int>
#define mkt make_tuple
#define pii pair<int,int>
#define mk make_pair
#define ll long long
#define ff first
#define ss second
using namespace std ;
int N ;
ll A , B , G , ans ;
vector< pair<ll,ll> > vectorInteiros , vectorParciais , intervalosInteiros ;
map< ll , vector< pair<ll,ll> > > miniLineSweep ;
bool pegueiInteiro(ll block)
{
auto it = upper_bound( all(intervalosInteiros) , mk(block,G+5) ) ;
if( it == intervalosInteiros.begin() ) return false ;
it-- ;
return (it->ff <= block && it->ss >= block) ;
}
int main()
{
scanf("%d%lld%lld", &N , &A, &B ) ;
G = __gcd(A,B+1);
G = A/G ;
const ll period = ( LLONG_MAX / B < G ) ? LLONG_MAX : B*G ;
for(int i = 1 ; i <= N ; i++ )
{
ll L , R ;
scanf("%lld %lld", &L , &R ) ;
if(R - L + 1LL >= period )
{
printf("%lld\n" , period ) ;
return 0 ;
}
ll actualL = ( (L+B)/B ) * B ;
ll actualR = ( (R-B)/B ) * B + B-1 ;
if(actualL - 1 >= L)
vectorParciais.pb( mk(L, min(actualL-1, R) ) ) ;
if(actualR+1 <= R)
vectorParciais.pb( mk(max(L,actualR+1), R ) ) ;
if( actualL > actualR ) continue ;
actualL /= B ;
actualR /= B ;
actualL %= G ;
actualR %= G ;
if(actualL > actualR )
{
vectorInteiros.pb( mk(actualL, G-1) ) ;
actualL = 0 ;
}
vectorInteiros.pb( mk(actualL, actualR) ) ;
}
if( sz(vectorInteiros) > 0 )
{
sort(all(vectorInteiros)) ;
ll mn = vectorInteiros[0].ff ;
ll mx = vectorInteiros[0].ss ;
vectorInteiros.pb( mk(G+5, G+5) ) ;
for(int i = 1 ; i < sz(vectorInteiros) ; i++ )
{
if( vectorInteiros[i].ff > mx )
{
intervalosInteiros.pb( mk(mn,mx) ) ;
ans += (mx - mn + 1LL ) * B ;
mn = vectorInteiros[i].ff ;
mx = vectorInteiros[i].ss ;
}
else if( vectorInteiros[i].ss > mx ) mx = vectorInteiros[i].ss ;
}
}
for(auto e : vectorParciais )
{
ll L = e.ff ;
ll R = e.ss ;
ll block = L/B ;
block %= G ;
L %= B ;
R %= B ;
miniLineSweep[ block ].pb( mk(L,R) ) ;
}
for(auto block : miniLineSweep )
{
if( pegueiInteiro(block.ff) ) continue ;
// debug("Estou vendo o modulo %d, e tenho nele:\n" , block.ff ) ;
vector< pair<ll,ll> > &sweep = block.ss ;
sort(all(sweep)) ;
// for(auto e :sweep ) debug("%lld %lld\n" ,e.ff, e.ss ) ;
// debug("\n") ;
ll mn = sweep[0].ff ;
ll mx = sweep[0].ss ;
sweep.pb( mk(B+5, B+5) ) ;
for(int i = 1 ;i < sz(sweep) ; i++ )
{
if(sweep[i].ff > mx )
{
ans += mx - mn + 1LL ;
mn = sweep[i].ff ;
mx = sweep[i].ss ;
}
else if( mx < sweep[i].ss ) mx = sweep[i].ss ;
}
}
printf("%lld\n" , ans ) ;
}
Compilation message
strange_device.cpp: In function 'int main()':
strange_device.cpp:38:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
38 | scanf("%d%lld%lld", &N , &A, &B ) ;
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
strange_device.cpp:48:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
48 | scanf("%lld %lld", &L , &R ) ;
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
8 ms |
1528 KB |
Output is correct |
3 |
Correct |
8 ms |
1400 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
1 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 |
384 KB |
Output is correct |
16 |
Correct |
8 ms |
1528 KB |
Output is correct |
17 |
Correct |
74 ms |
9692 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 |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
0 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 |
2 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
512 KB |
Output is correct |
5 |
Correct |
688 ms |
121360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
1237 ms |
160972 KB |
Output is correct |
3 |
Correct |
1817 ms |
286396 KB |
Output is correct |
4 |
Correct |
1840 ms |
284432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
1237 ms |
160972 KB |
Output is correct |
3 |
Correct |
1817 ms |
286396 KB |
Output is correct |
4 |
Correct |
1840 ms |
284432 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
1456 ms |
190548 KB |
Output is correct |
7 |
Correct |
936 ms |
106272 KB |
Output is correct |
8 |
Correct |
1469 ms |
189720 KB |
Output is correct |
9 |
Correct |
1558 ms |
189528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
1237 ms |
160972 KB |
Output is correct |
3 |
Correct |
1817 ms |
286396 KB |
Output is correct |
4 |
Correct |
1840 ms |
284432 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
118 ms |
18104 KB |
Output is correct |
7 |
Correct |
151 ms |
21340 KB |
Output is correct |
8 |
Correct |
140 ms |
21340 KB |
Output is correct |
9 |
Correct |
164 ms |
21340 KB |
Output is correct |
10 |
Correct |
120 ms |
15684 KB |
Output is correct |
11 |
Correct |
148 ms |
21376 KB |
Output is correct |
12 |
Correct |
143 ms |
21260 KB |
Output is correct |
13 |
Correct |
143 ms |
21288 KB |
Output is correct |
14 |
Correct |
83 ms |
9948 KB |
Output is correct |
15 |
Correct |
171 ms |
18320 KB |
Output is correct |
16 |
Correct |
81 ms |
9964 KB |
Output is correct |
17 |
Correct |
140 ms |
21388 KB |
Output is correct |
18 |
Correct |
1499 ms |
189584 KB |
Output is correct |
19 |
Correct |
1483 ms |
189740 KB |
Output is correct |
20 |
Correct |
1660 ms |
189592 KB |
Output is correct |
21 |
Correct |
139 ms |
21352 KB |
Output is correct |
22 |
Correct |
137 ms |
21336 KB |
Output is correct |
23 |
Correct |
311 ms |
46004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
71 ms |
10332 KB |
Output is correct |
3 |
Correct |
68 ms |
10480 KB |
Output is correct |
4 |
Correct |
934 ms |
111128 KB |
Output is correct |
5 |
Correct |
75 ms |
11868 KB |
Output is correct |
6 |
Correct |
73 ms |
11508 KB |
Output is correct |
7 |
Correct |
82 ms |
11816 KB |
Output is correct |
8 |
Correct |
85 ms |
14556 KB |
Output is correct |
9 |
Correct |
88 ms |
14300 KB |
Output is correct |
10 |
Correct |
70 ms |
10220 KB |
Output is correct |
11 |
Correct |
81 ms |
10704 KB |
Output is correct |
12 |
Correct |
69 ms |
10636 KB |
Output is correct |
13 |
Correct |
82 ms |
13276 KB |
Output is correct |
14 |
Correct |
677 ms |
68068 KB |
Output is correct |
15 |
Correct |
73 ms |
10784 KB |
Output is correct |
16 |
Correct |
714 ms |
68596 KB |
Output is correct |
17 |
Correct |
1157 ms |
110100 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 |
8 ms |
1528 KB |
Output is correct |
3 |
Correct |
8 ms |
1400 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
1 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 |
384 KB |
Output is correct |
16 |
Correct |
8 ms |
1528 KB |
Output is correct |
17 |
Correct |
74 ms |
9692 KB |
Output is correct |
18 |
Correct |
0 ms |
256 KB |
Output is correct |
19 |
Correct |
0 ms |
256 KB |
Output is correct |
20 |
Correct |
0 ms |
256 KB |
Output is correct |
21 |
Correct |
0 ms |
256 KB |
Output is correct |
22 |
Correct |
0 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 |
2 ms |
512 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
2 ms |
512 KB |
Output is correct |
28 |
Correct |
688 ms |
121360 KB |
Output is correct |
29 |
Correct |
0 ms |
256 KB |
Output is correct |
30 |
Correct |
1237 ms |
160972 KB |
Output is correct |
31 |
Correct |
1817 ms |
286396 KB |
Output is correct |
32 |
Correct |
1840 ms |
284432 KB |
Output is correct |
33 |
Correct |
0 ms |
256 KB |
Output is correct |
34 |
Correct |
1456 ms |
190548 KB |
Output is correct |
35 |
Correct |
936 ms |
106272 KB |
Output is correct |
36 |
Correct |
1469 ms |
189720 KB |
Output is correct |
37 |
Correct |
1558 ms |
189528 KB |
Output is correct |
38 |
Correct |
0 ms |
256 KB |
Output is correct |
39 |
Correct |
118 ms |
18104 KB |
Output is correct |
40 |
Correct |
151 ms |
21340 KB |
Output is correct |
41 |
Correct |
140 ms |
21340 KB |
Output is correct |
42 |
Correct |
164 ms |
21340 KB |
Output is correct |
43 |
Correct |
120 ms |
15684 KB |
Output is correct |
44 |
Correct |
148 ms |
21376 KB |
Output is correct |
45 |
Correct |
143 ms |
21260 KB |
Output is correct |
46 |
Correct |
143 ms |
21288 KB |
Output is correct |
47 |
Correct |
83 ms |
9948 KB |
Output is correct |
48 |
Correct |
171 ms |
18320 KB |
Output is correct |
49 |
Correct |
81 ms |
9964 KB |
Output is correct |
50 |
Correct |
140 ms |
21388 KB |
Output is correct |
51 |
Correct |
1499 ms |
189584 KB |
Output is correct |
52 |
Correct |
1483 ms |
189740 KB |
Output is correct |
53 |
Correct |
1660 ms |
189592 KB |
Output is correct |
54 |
Correct |
139 ms |
21352 KB |
Output is correct |
55 |
Correct |
137 ms |
21336 KB |
Output is correct |
56 |
Correct |
311 ms |
46004 KB |
Output is correct |
57 |
Correct |
1 ms |
256 KB |
Output is correct |
58 |
Correct |
71 ms |
10332 KB |
Output is correct |
59 |
Correct |
68 ms |
10480 KB |
Output is correct |
60 |
Correct |
934 ms |
111128 KB |
Output is correct |
61 |
Correct |
75 ms |
11868 KB |
Output is correct |
62 |
Correct |
73 ms |
11508 KB |
Output is correct |
63 |
Correct |
82 ms |
11816 KB |
Output is correct |
64 |
Correct |
85 ms |
14556 KB |
Output is correct |
65 |
Correct |
88 ms |
14300 KB |
Output is correct |
66 |
Correct |
70 ms |
10220 KB |
Output is correct |
67 |
Correct |
81 ms |
10704 KB |
Output is correct |
68 |
Correct |
69 ms |
10636 KB |
Output is correct |
69 |
Correct |
82 ms |
13276 KB |
Output is correct |
70 |
Correct |
677 ms |
68068 KB |
Output is correct |
71 |
Correct |
73 ms |
10784 KB |
Output is correct |
72 |
Correct |
714 ms |
68596 KB |
Output is correct |
73 |
Correct |
1157 ms |
110100 KB |
Output is correct |
74 |
Correct |
1 ms |
256 KB |
Output is correct |
75 |
Correct |
1 ms |
256 KB |
Output is correct |
76 |
Correct |
0 ms |
256 KB |
Output is correct |
77 |
Correct |
1 ms |
256 KB |
Output is correct |
78 |
Correct |
0 ms |
256 KB |
Output is correct |
79 |
Correct |
11 ms |
1904 KB |
Output is correct |
80 |
Correct |
687 ms |
78952 KB |
Output is correct |
81 |
Correct |
943 ms |
118808 KB |
Output is correct |
82 |
Correct |
1533 ms |
199968 KB |
Output is correct |
83 |
Correct |
1522 ms |
200176 KB |
Output is correct |
84 |
Correct |
1633 ms |
200320 KB |
Output is correct |
85 |
Correct |
1511 ms |
199832 KB |
Output is correct |
86 |
Correct |
299 ms |
57092 KB |
Output is correct |
87 |
Correct |
1 ms |
384 KB |
Output is correct |