#include <bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cerr.tie(0)
#define mp make_pair
#define xx first
#define yy second
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define all(x) x.begin(),x.end()
#define ff(i,a,b) for (int i = a; i < b; i++)
#define fff(i,a,b) for (int i = a; i <= b; i++)
#define bff(i,a,b) for (int i = b-1; i >= a; i--)
#define bfff(i,a,b) for (int i = b; i >= a; i--)
using namespace std;
long double typedef ld;
unsigned int typedef ui;
long long int typedef li;
pair<int,int> typedef pii;
pair<li,li> typedef pli;
pair<ld,ld> typedef pld;
vector<vector<int>> typedef graph;
unsigned long long int typedef ull;
//const int mod = 998244353;
const int mod = 1000000007;
//Note to self: Check for overflow
const li inf=2e18;
map<li,vector<pli>> segs;
set<li> kords;
map<li,li> cisti;
int main()
{
FAST;
int n; cin>>n;
li A,B; cin>>A>>B;
li d=__gcd(A,B+1);
A/=d;
li mozdanskurans=0;
vector<pli> ulaz;
while (n--)
{
li l,r; cin>>l>>r; mozdanskurans+=r-l+1;
if (A<inf/B)
{
if (l/(A*B)!=r/(A*B))
{
ulaz.pb({l%(A*B),A*B-1});
ulaz.pb({0,r%(A*B)});
if (l/(A*B)+1<r/(A*B)) return cout<<A*B,0;
}
else
{
l%=(A*B),r%=(A*B);
if (l>r) ulaz.pb({0,r}),ulaz.pb({l,A*B-1});
else ulaz.pb({l,r});
}
}
}
if (A>=inf/B || A*B>=1'000'000'000'000'000'000ll)
return cout<<mozdanskurans,0;
for (auto [l,r] : ulaz)
{
if (r/A == l/A) segs[l/A].pb({l%A,r%A}),kords.insert(l/A);
else
{
segs[l/A].pb({l%A,A-1});
segs[r/A].pb({0,r%A});
cisti[l/A+1]++;
cisti[r/A]--;
kords.insert(l/A);
kords.insert(l/A+1);
kords.insert(r/A);
}
}
li ans=0;
li kolkoclear=0;
li majmuncina=-1;
for (auto x : kords)
{
bool staromeso=false;
if (kolkoclear==0) staromeso=true;
kolkoclear+=cisti[x]; //cout<<x<<": "<<cisti[x]<<endl;
if (kolkoclear>0 && staromeso) majmuncina=x;
//cout<<"marmun "<<kolkoclear<<endl;
if (kolkoclear==0 && majmuncina!=-1) ans+=(x-majmuncina)*A,majmuncina=-1;
if (kolkoclear==0)
{
vector<pair<li,li>> lensi;
for (auto it : segs[x]) lensi.pb({it.xx,1}),lensi.pb({it.yy+1,-1});
sort(all(lensi));
li preth=-1;
li kolkocur=0;
for (auto y : lensi)
{
if (preth!=-1 && kolkocur) ans+=y.xx-preth;
preth=y.xx; kolkocur+=y.yy;
}
}
}
cout<<ans<<"\n";
}
//Note to self: Check for overflow
/*
1 999999999 1000000000
1 1000000000000000000
*/
Compilation message
strange_device.cpp: In function 'int main()':
strange_device.cpp:80:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
80 | for (auto [l,r] : ulaz)
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
5 ms |
844 KB |
Output is correct |
3 |
Correct |
6 ms |
976 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 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 |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
320 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
316 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 |
340 KB |
Output is correct |
16 |
Correct |
6 ms |
884 KB |
Output is correct |
17 |
Correct |
49 ms |
3992 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 |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
320 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 |
3 ms |
712 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
627 ms |
97012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
563 ms |
65056 KB |
Output is correct |
3 |
Correct |
569 ms |
65296 KB |
Output is correct |
4 |
Correct |
536 ms |
64996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
563 ms |
65056 KB |
Output is correct |
3 |
Correct |
569 ms |
65296 KB |
Output is correct |
4 |
Correct |
536 ms |
64996 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
571 ms |
88200 KB |
Output is correct |
7 |
Correct |
608 ms |
81316 KB |
Output is correct |
8 |
Correct |
572 ms |
70260 KB |
Output is correct |
9 |
Correct |
588 ms |
83216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
563 ms |
65056 KB |
Output is correct |
3 |
Correct |
569 ms |
65296 KB |
Output is correct |
4 |
Correct |
536 ms |
64996 KB |
Output is correct |
5 |
Correct |
0 ms |
320 KB |
Output is correct |
6 |
Correct |
56 ms |
4712 KB |
Output is correct |
7 |
Correct |
54 ms |
5564 KB |
Output is correct |
8 |
Correct |
55 ms |
5572 KB |
Output is correct |
9 |
Correct |
48 ms |
5844 KB |
Output is correct |
10 |
Correct |
54 ms |
4160 KB |
Output is correct |
11 |
Correct |
49 ms |
4092 KB |
Output is correct |
12 |
Correct |
49 ms |
4048 KB |
Output is correct |
13 |
Correct |
43 ms |
4092 KB |
Output is correct |
14 |
Correct |
194 ms |
37544 KB |
Output is correct |
15 |
Correct |
46 ms |
4672 KB |
Output is correct |
16 |
Correct |
56 ms |
5840 KB |
Output is correct |
17 |
Correct |
205 ms |
36660 KB |
Output is correct |
18 |
Correct |
465 ms |
33368 KB |
Output is correct |
19 |
Correct |
446 ms |
33740 KB |
Output is correct |
20 |
Correct |
438 ms |
34388 KB |
Output is correct |
21 |
Correct |
49 ms |
4160 KB |
Output is correct |
22 |
Correct |
46 ms |
4300 KB |
Output is correct |
23 |
Correct |
201 ms |
46576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
50 ms |
5060 KB |
Output is correct |
3 |
Correct |
77 ms |
9152 KB |
Output is correct |
4 |
Correct |
825 ms |
87524 KB |
Output is correct |
5 |
Correct |
237 ms |
36588 KB |
Output is correct |
6 |
Correct |
267 ms |
36808 KB |
Output is correct |
7 |
Correct |
244 ms |
36672 KB |
Output is correct |
8 |
Correct |
49 ms |
4204 KB |
Output is correct |
9 |
Correct |
97 ms |
12992 KB |
Output is correct |
10 |
Correct |
183 ms |
36648 KB |
Output is correct |
11 |
Correct |
191 ms |
36800 KB |
Output is correct |
12 |
Correct |
185 ms |
36752 KB |
Output is correct |
13 |
Correct |
47 ms |
4672 KB |
Output is correct |
14 |
Correct |
483 ms |
34168 KB |
Output is correct |
15 |
Correct |
81 ms |
10528 KB |
Output is correct |
16 |
Correct |
2061 ms |
361588 KB |
Output is correct |
17 |
Correct |
456 ms |
34440 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 |
844 KB |
Output is correct |
3 |
Correct |
6 ms |
976 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 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 |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
320 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
316 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 |
340 KB |
Output is correct |
16 |
Correct |
6 ms |
884 KB |
Output is correct |
17 |
Correct |
49 ms |
3992 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
320 KB |
Output is correct |
23 |
Correct |
1 ms |
320 KB |
Output is correct |
24 |
Correct |
1 ms |
324 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
3 ms |
712 KB |
Output is correct |
27 |
Correct |
2 ms |
468 KB |
Output is correct |
28 |
Correct |
627 ms |
97012 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
563 ms |
65056 KB |
Output is correct |
31 |
Correct |
569 ms |
65296 KB |
Output is correct |
32 |
Correct |
536 ms |
64996 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
571 ms |
88200 KB |
Output is correct |
35 |
Correct |
608 ms |
81316 KB |
Output is correct |
36 |
Correct |
572 ms |
70260 KB |
Output is correct |
37 |
Correct |
588 ms |
83216 KB |
Output is correct |
38 |
Correct |
0 ms |
320 KB |
Output is correct |
39 |
Correct |
56 ms |
4712 KB |
Output is correct |
40 |
Correct |
54 ms |
5564 KB |
Output is correct |
41 |
Correct |
55 ms |
5572 KB |
Output is correct |
42 |
Correct |
48 ms |
5844 KB |
Output is correct |
43 |
Correct |
54 ms |
4160 KB |
Output is correct |
44 |
Correct |
49 ms |
4092 KB |
Output is correct |
45 |
Correct |
49 ms |
4048 KB |
Output is correct |
46 |
Correct |
43 ms |
4092 KB |
Output is correct |
47 |
Correct |
194 ms |
37544 KB |
Output is correct |
48 |
Correct |
46 ms |
4672 KB |
Output is correct |
49 |
Correct |
56 ms |
5840 KB |
Output is correct |
50 |
Correct |
205 ms |
36660 KB |
Output is correct |
51 |
Correct |
465 ms |
33368 KB |
Output is correct |
52 |
Correct |
446 ms |
33740 KB |
Output is correct |
53 |
Correct |
438 ms |
34388 KB |
Output is correct |
54 |
Correct |
49 ms |
4160 KB |
Output is correct |
55 |
Correct |
46 ms |
4300 KB |
Output is correct |
56 |
Correct |
201 ms |
46576 KB |
Output is correct |
57 |
Correct |
0 ms |
212 KB |
Output is correct |
58 |
Correct |
50 ms |
5060 KB |
Output is correct |
59 |
Correct |
77 ms |
9152 KB |
Output is correct |
60 |
Correct |
825 ms |
87524 KB |
Output is correct |
61 |
Correct |
237 ms |
36588 KB |
Output is correct |
62 |
Correct |
267 ms |
36808 KB |
Output is correct |
63 |
Correct |
244 ms |
36672 KB |
Output is correct |
64 |
Correct |
49 ms |
4204 KB |
Output is correct |
65 |
Correct |
97 ms |
12992 KB |
Output is correct |
66 |
Correct |
183 ms |
36648 KB |
Output is correct |
67 |
Correct |
191 ms |
36800 KB |
Output is correct |
68 |
Correct |
185 ms |
36752 KB |
Output is correct |
69 |
Correct |
47 ms |
4672 KB |
Output is correct |
70 |
Correct |
483 ms |
34168 KB |
Output is correct |
71 |
Correct |
81 ms |
10528 KB |
Output is correct |
72 |
Correct |
2061 ms |
361588 KB |
Output is correct |
73 |
Correct |
456 ms |
34440 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 |
320 KB |
Output is correct |
77 |
Correct |
1 ms |
316 KB |
Output is correct |
78 |
Correct |
1 ms |
212 KB |
Output is correct |
79 |
Correct |
6 ms |
984 KB |
Output is correct |
80 |
Correct |
863 ms |
105080 KB |
Output is correct |
81 |
Correct |
903 ms |
107508 KB |
Output is correct |
82 |
Correct |
676 ms |
73276 KB |
Output is correct |
83 |
Correct |
3161 ms |
380844 KB |
Output is correct |
84 |
Correct |
3120 ms |
381044 KB |
Output is correct |
85 |
Correct |
1375 ms |
220252 KB |
Output is correct |
86 |
Correct |
209 ms |
58888 KB |
Output is correct |
87 |
Correct |
0 ms |
212 KB |
Output is correct |