#include<bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define eb emplace_back
#define all(a) (a).begin(),(a).end()
#define SZ(a) (int)(a).size()
#define FOR(i, a, b) for(int i=(a); i<=(b); ++i)
#define ROF(i, a, b) for(int i=(a); i>=(b); --i)
#define make_unique(a) sort(all((a))), (a).resize(unique(all((a)))-(a).begin())
using namespace std;
typedef pair<int,int> PII;
typedef long long LL;
typedef double DD;
typedef long double LD;
typedef pair<LL,LL> PLL;
typedef pair<DD,DD> PDD;
typedef vector<int> VI;
typedef vector<LL> VL;
const int N = 1000005;
int n;
LL A, B;
set<PLL> keep;
set<PLL>::iterator it, it2;
void solve(){
scanf("%d %lld %lld",&n,&A,&B);
LL loop = A/__gcd(A,B+1);
if(loop > 1e18/B){
loop = 1e18+2;
}else loop *= B;
//printf("loop is %lld\n",loop);
int ch = 0;
LL ans = 0;
auto find = [&](LL a){
return keep.lower_bound(PLL(a,0));
};
auto add = [&](LL l, LL r){
//printf("adding (%lld, %lld)\n",l,r);
it = find(l+1);
if(it != keep.begin()) --it;
for(; it != keep.end();){
PLL e = *it;
it2 = it;
++it;
if(e.x > r+1) break;
if(e.y < l-1) continue;
l = min(l, e.x); r = max(r, e.y);
keep.erase(it2);
}
keep.insert(PLL(l,r));
};
FOR(i, 1, n){
LL l, r;
scanf("%lld %lld",&l,&r);
if(r-l+1 >= loop){
ch = 1; ans = loop;
}
// there is a function called 'prev'
l %= loop;
r %= loop;
if(l > r) add(0,r), add(l,loop-1);
else add(l,r);
//printf("keep : ");
//for(PLL e : keep) printf("[%lld, %lld] ",e.x,e.y);
//puts("");
}
if(ch){ printf("%lld",ans); return ; }
for(PLL e : keep) ans += e.y-e.x+1;
printf("%lld",ans);
}
int main(){
solve();
return 0;
}
/*
*
*
*
*
*
*
*
*
*
*
*/
Compilation message
strange_device.cpp: In function 'void solve()':
strange_device.cpp:29:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %lld %lld",&n,&A,&B);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
strange_device.cpp:57:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld %lld",&l,&r);
~~~~~^~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
6 ms |
384 KB |
Output is correct |
3 |
Correct |
6 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
256 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 |
Correct |
1 ms |
256 KB |
Output is correct |
9 |
Correct |
1 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
0 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 |
256 KB |
Output is correct |
16 |
Correct |
6 ms |
384 KB |
Output is correct |
17 |
Correct |
54 ms |
384 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 |
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 |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
433 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
528 ms |
384 KB |
Output is correct |
3 |
Correct |
516 ms |
384 KB |
Output is correct |
4 |
Correct |
518 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
528 ms |
384 KB |
Output is correct |
3 |
Correct |
516 ms |
384 KB |
Output is correct |
4 |
Correct |
518 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
525 ms |
384 KB |
Output is correct |
7 |
Correct |
549 ms |
504 KB |
Output is correct |
8 |
Correct |
529 ms |
504 KB |
Output is correct |
9 |
Correct |
536 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
528 ms |
384 KB |
Output is correct |
3 |
Correct |
516 ms |
384 KB |
Output is correct |
4 |
Correct |
518 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
58 ms |
384 KB |
Output is correct |
7 |
Correct |
55 ms |
384 KB |
Output is correct |
8 |
Correct |
52 ms |
376 KB |
Output is correct |
9 |
Correct |
53 ms |
256 KB |
Output is correct |
10 |
Correct |
54 ms |
380 KB |
Output is correct |
11 |
Correct |
54 ms |
384 KB |
Output is correct |
12 |
Correct |
53 ms |
376 KB |
Output is correct |
13 |
Correct |
52 ms |
256 KB |
Output is correct |
14 |
Correct |
55 ms |
384 KB |
Output is correct |
15 |
Correct |
54 ms |
376 KB |
Output is correct |
16 |
Correct |
57 ms |
376 KB |
Output is correct |
17 |
Correct |
53 ms |
376 KB |
Output is correct |
18 |
Correct |
539 ms |
504 KB |
Output is correct |
19 |
Correct |
515 ms |
376 KB |
Output is correct |
20 |
Correct |
538 ms |
376 KB |
Output is correct |
21 |
Correct |
53 ms |
504 KB |
Output is correct |
22 |
Correct |
57 ms |
256 KB |
Output is correct |
23 |
Correct |
200 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
55 ms |
384 KB |
Output is correct |
3 |
Correct |
53 ms |
384 KB |
Output is correct |
4 |
Correct |
504 ms |
384 KB |
Output is correct |
5 |
Correct |
55 ms |
376 KB |
Output is correct |
6 |
Correct |
56 ms |
376 KB |
Output is correct |
7 |
Correct |
52 ms |
256 KB |
Output is correct |
8 |
Correct |
63 ms |
384 KB |
Output is correct |
9 |
Correct |
52 ms |
376 KB |
Output is correct |
10 |
Correct |
52 ms |
376 KB |
Output is correct |
11 |
Correct |
55 ms |
384 KB |
Output is correct |
12 |
Correct |
53 ms |
256 KB |
Output is correct |
13 |
Correct |
53 ms |
384 KB |
Output is correct |
14 |
Correct |
524 ms |
380 KB |
Output is correct |
15 |
Correct |
55 ms |
376 KB |
Output is correct |
16 |
Correct |
531 ms |
384 KB |
Output is correct |
17 |
Correct |
538 ms |
384 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 |
6 ms |
384 KB |
Output is correct |
3 |
Correct |
6 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
256 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 |
Correct |
1 ms |
256 KB |
Output is correct |
9 |
Correct |
1 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
0 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 |
256 KB |
Output is correct |
16 |
Correct |
6 ms |
384 KB |
Output is correct |
17 |
Correct |
54 ms |
384 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 |
0 ms |
256 KB |
Output is correct |
24 |
Correct |
0 ms |
256 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
433 ms |
256 KB |
Output is correct |
29 |
Correct |
1 ms |
256 KB |
Output is correct |
30 |
Correct |
528 ms |
384 KB |
Output is correct |
31 |
Correct |
516 ms |
384 KB |
Output is correct |
32 |
Correct |
518 ms |
256 KB |
Output is correct |
33 |
Correct |
0 ms |
256 KB |
Output is correct |
34 |
Correct |
525 ms |
384 KB |
Output is correct |
35 |
Correct |
549 ms |
504 KB |
Output is correct |
36 |
Correct |
529 ms |
504 KB |
Output is correct |
37 |
Correct |
536 ms |
376 KB |
Output is correct |
38 |
Correct |
0 ms |
256 KB |
Output is correct |
39 |
Correct |
58 ms |
384 KB |
Output is correct |
40 |
Correct |
55 ms |
384 KB |
Output is correct |
41 |
Correct |
52 ms |
376 KB |
Output is correct |
42 |
Correct |
53 ms |
256 KB |
Output is correct |
43 |
Correct |
54 ms |
380 KB |
Output is correct |
44 |
Correct |
54 ms |
384 KB |
Output is correct |
45 |
Correct |
53 ms |
376 KB |
Output is correct |
46 |
Correct |
52 ms |
256 KB |
Output is correct |
47 |
Correct |
55 ms |
384 KB |
Output is correct |
48 |
Correct |
54 ms |
376 KB |
Output is correct |
49 |
Correct |
57 ms |
376 KB |
Output is correct |
50 |
Correct |
53 ms |
376 KB |
Output is correct |
51 |
Correct |
539 ms |
504 KB |
Output is correct |
52 |
Correct |
515 ms |
376 KB |
Output is correct |
53 |
Correct |
538 ms |
376 KB |
Output is correct |
54 |
Correct |
53 ms |
504 KB |
Output is correct |
55 |
Correct |
57 ms |
256 KB |
Output is correct |
56 |
Correct |
200 ms |
256 KB |
Output is correct |
57 |
Correct |
0 ms |
256 KB |
Output is correct |
58 |
Correct |
55 ms |
384 KB |
Output is correct |
59 |
Correct |
53 ms |
384 KB |
Output is correct |
60 |
Correct |
504 ms |
384 KB |
Output is correct |
61 |
Correct |
55 ms |
376 KB |
Output is correct |
62 |
Correct |
56 ms |
376 KB |
Output is correct |
63 |
Correct |
52 ms |
256 KB |
Output is correct |
64 |
Correct |
63 ms |
384 KB |
Output is correct |
65 |
Correct |
52 ms |
376 KB |
Output is correct |
66 |
Correct |
52 ms |
376 KB |
Output is correct |
67 |
Correct |
55 ms |
384 KB |
Output is correct |
68 |
Correct |
53 ms |
256 KB |
Output is correct |
69 |
Correct |
53 ms |
384 KB |
Output is correct |
70 |
Correct |
524 ms |
380 KB |
Output is correct |
71 |
Correct |
55 ms |
376 KB |
Output is correct |
72 |
Correct |
531 ms |
384 KB |
Output is correct |
73 |
Correct |
538 ms |
384 KB |
Output is correct |
74 |
Correct |
0 ms |
256 KB |
Output is correct |
75 |
Correct |
0 ms |
256 KB |
Output is correct |
76 |
Correct |
0 ms |
256 KB |
Output is correct |
77 |
Correct |
0 ms |
256 KB |
Output is correct |
78 |
Correct |
0 ms |
256 KB |
Output is correct |
79 |
Correct |
6 ms |
384 KB |
Output is correct |
80 |
Correct |
509 ms |
384 KB |
Output is correct |
81 |
Correct |
509 ms |
376 KB |
Output is correct |
82 |
Correct |
528 ms |
408 KB |
Output is correct |
83 |
Correct |
521 ms |
376 KB |
Output is correct |
84 |
Correct |
525 ms |
376 KB |
Output is correct |
85 |
Correct |
515 ms |
384 KB |
Output is correct |
86 |
Correct |
200 ms |
256 KB |
Output is correct |
87 |
Correct |
0 ms |
256 KB |
Output is correct |