#include<bits/stdc++.h>
#define TASKNAME "codeforce"
#define pb push_back
#define pli pair<ll,ll>
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
using ll=long long;
const ll maxN=2e5;
const ll inf=3e18;
const ll mod=1e9+7;
ll n,A,B;
vector<pli>vec;
void solve()
{
cin >> n >> A >> B;
ll x=(A/__gcd(A,B+1))*B;
bool ok=true;
ll sum=0;
for(int i=1;i<=n;i++)
{
ll l,r;
cin >> l >> r;
sum+=r-l+1;
if((r-l+1)>=x)
{
ok=false;
}
l%=x;
r%=x;
if(l<=r)
{
vec.pb({l,r});
}
else
{
vec.pb({l,x-1});
vec.pb({0,r});
}
}
ll cc=A/(__gcd(A,B+1));
if(inf/cc<B)
{
cout << sum;
return;
}
if(ok==false)
{
cout << x;
return;
}
sort(vec.begin(),vec.end());
ll ans=0;
for(int i=0;i<vec.size();i++)
{
ll j=i+1;
ll r=vec[i].se;
while(j<vec.size()&&vec[j].fi<=r)
{
r=max(r,vec[j].se);
j++;
}
ans+=r-vec[i].fi+1;
i=j-1;
}
cout << ans;
}
int main()
{
fastio
//freopen(TASKNAME".INP","r",stdin);
//freopen(TASKNAME".OUT","w",stdout);
solve();
}
Compilation message
strange_device.cpp: In function 'void solve()':
strange_device.cpp:55:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | for(int i=0;i<vec.size();i++)
| ~^~~~~~~~~~~
strange_device.cpp:59:16: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | while(j<vec.size()&&vec[j].fi<=r)
| ~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
5 ms |
976 KB |
Output is correct |
3 |
Correct |
5 ms |
1064 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
8 |
Correct |
1 ms |
316 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 |
312 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 |
324 KB |
Output is correct |
16 |
Correct |
4 ms |
1084 KB |
Output is correct |
17 |
Correct |
47 ms |
4808 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
324 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
318 ms |
17448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
403 ms |
16860 KB |
Output is correct |
3 |
Correct |
403 ms |
16940 KB |
Output is correct |
4 |
Correct |
446 ms |
20704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
403 ms |
16860 KB |
Output is correct |
3 |
Correct |
403 ms |
16940 KB |
Output is correct |
4 |
Correct |
446 ms |
20704 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
409 ms |
20192 KB |
Output is correct |
7 |
Correct |
423 ms |
19884 KB |
Output is correct |
8 |
Correct |
409 ms |
20108 KB |
Output is correct |
9 |
Correct |
423 ms |
19916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
403 ms |
16860 KB |
Output is correct |
3 |
Correct |
403 ms |
16940 KB |
Output is correct |
4 |
Correct |
446 ms |
20704 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
41 ms |
4276 KB |
Output is correct |
7 |
Correct |
41 ms |
4208 KB |
Output is correct |
8 |
Correct |
45 ms |
4288 KB |
Output is correct |
9 |
Correct |
49 ms |
4284 KB |
Output is correct |
10 |
Correct |
42 ms |
4296 KB |
Output is correct |
11 |
Correct |
42 ms |
4204 KB |
Output is correct |
12 |
Correct |
43 ms |
4168 KB |
Output is correct |
13 |
Correct |
43 ms |
4224 KB |
Output is correct |
14 |
Correct |
44 ms |
4232 KB |
Output is correct |
15 |
Correct |
48 ms |
4268 KB |
Output is correct |
16 |
Correct |
42 ms |
4048 KB |
Output is correct |
17 |
Correct |
42 ms |
4184 KB |
Output is correct |
18 |
Correct |
462 ms |
19228 KB |
Output is correct |
19 |
Correct |
403 ms |
19132 KB |
Output is correct |
20 |
Correct |
425 ms |
18136 KB |
Output is correct |
21 |
Correct |
55 ms |
3784 KB |
Output is correct |
22 |
Correct |
50 ms |
3656 KB |
Output is correct |
23 |
Correct |
127 ms |
9660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
44 ms |
3556 KB |
Output is correct |
3 |
Correct |
42 ms |
3616 KB |
Output is correct |
4 |
Correct |
458 ms |
18048 KB |
Output is correct |
5 |
Correct |
41 ms |
3528 KB |
Output is correct |
6 |
Correct |
42 ms |
3608 KB |
Output is correct |
7 |
Correct |
47 ms |
3712 KB |
Output is correct |
8 |
Correct |
56 ms |
3592 KB |
Output is correct |
9 |
Correct |
54 ms |
3644 KB |
Output is correct |
10 |
Correct |
44 ms |
3636 KB |
Output is correct |
11 |
Correct |
46 ms |
3520 KB |
Output is correct |
12 |
Correct |
40 ms |
3528 KB |
Output is correct |
13 |
Correct |
44 ms |
3472 KB |
Output is correct |
14 |
Correct |
443 ms |
17916 KB |
Output is correct |
15 |
Correct |
46 ms |
3524 KB |
Output is correct |
16 |
Correct |
403 ms |
18004 KB |
Output is correct |
17 |
Correct |
440 ms |
17740 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
5 ms |
976 KB |
Output is correct |
3 |
Correct |
5 ms |
1064 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
8 |
Correct |
1 ms |
316 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 |
312 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 |
324 KB |
Output is correct |
16 |
Correct |
4 ms |
1084 KB |
Output is correct |
17 |
Correct |
47 ms |
4808 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
324 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
328 KB |
Output is correct |
28 |
Correct |
318 ms |
17448 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
403 ms |
16860 KB |
Output is correct |
31 |
Correct |
403 ms |
16940 KB |
Output is correct |
32 |
Correct |
446 ms |
20704 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
409 ms |
20192 KB |
Output is correct |
35 |
Correct |
423 ms |
19884 KB |
Output is correct |
36 |
Correct |
409 ms |
20108 KB |
Output is correct |
37 |
Correct |
423 ms |
19916 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
41 ms |
4276 KB |
Output is correct |
40 |
Correct |
41 ms |
4208 KB |
Output is correct |
41 |
Correct |
45 ms |
4288 KB |
Output is correct |
42 |
Correct |
49 ms |
4284 KB |
Output is correct |
43 |
Correct |
42 ms |
4296 KB |
Output is correct |
44 |
Correct |
42 ms |
4204 KB |
Output is correct |
45 |
Correct |
43 ms |
4168 KB |
Output is correct |
46 |
Correct |
43 ms |
4224 KB |
Output is correct |
47 |
Correct |
44 ms |
4232 KB |
Output is correct |
48 |
Correct |
48 ms |
4268 KB |
Output is correct |
49 |
Correct |
42 ms |
4048 KB |
Output is correct |
50 |
Correct |
42 ms |
4184 KB |
Output is correct |
51 |
Correct |
462 ms |
19228 KB |
Output is correct |
52 |
Correct |
403 ms |
19132 KB |
Output is correct |
53 |
Correct |
425 ms |
18136 KB |
Output is correct |
54 |
Correct |
55 ms |
3784 KB |
Output is correct |
55 |
Correct |
50 ms |
3656 KB |
Output is correct |
56 |
Correct |
127 ms |
9660 KB |
Output is correct |
57 |
Correct |
1 ms |
212 KB |
Output is correct |
58 |
Correct |
44 ms |
3556 KB |
Output is correct |
59 |
Correct |
42 ms |
3616 KB |
Output is correct |
60 |
Correct |
458 ms |
18048 KB |
Output is correct |
61 |
Correct |
41 ms |
3528 KB |
Output is correct |
62 |
Correct |
42 ms |
3608 KB |
Output is correct |
63 |
Correct |
47 ms |
3712 KB |
Output is correct |
64 |
Correct |
56 ms |
3592 KB |
Output is correct |
65 |
Correct |
54 ms |
3644 KB |
Output is correct |
66 |
Correct |
44 ms |
3636 KB |
Output is correct |
67 |
Correct |
46 ms |
3520 KB |
Output is correct |
68 |
Correct |
40 ms |
3528 KB |
Output is correct |
69 |
Correct |
44 ms |
3472 KB |
Output is correct |
70 |
Correct |
443 ms |
17916 KB |
Output is correct |
71 |
Correct |
46 ms |
3524 KB |
Output is correct |
72 |
Correct |
403 ms |
18004 KB |
Output is correct |
73 |
Correct |
440 ms |
17740 KB |
Output is correct |
74 |
Correct |
1 ms |
212 KB |
Output is correct |
75 |
Correct |
1 ms |
212 KB |
Output is correct |
76 |
Correct |
1 ms |
212 KB |
Output is correct |
77 |
Correct |
0 ms |
212 KB |
Output is correct |
78 |
Correct |
1 ms |
320 KB |
Output is correct |
79 |
Correct |
4 ms |
972 KB |
Output is correct |
80 |
Correct |
530 ms |
44296 KB |
Output is correct |
81 |
Correct |
517 ms |
44548 KB |
Output is correct |
82 |
Correct |
482 ms |
44452 KB |
Output is correct |
83 |
Correct |
494 ms |
44688 KB |
Output is correct |
84 |
Correct |
469 ms |
44760 KB |
Output is correct |
85 |
Correct |
527 ms |
53176 KB |
Output is correct |
86 |
Correct |
139 ms |
18412 KB |
Output is correct |
87 |
Correct |
1 ms |
212 KB |
Output is correct |