/*
Normie's Template v2.0
*/
// Standard library in one include.
#include <bits/stdc++.h>
using namespace std;
// ordered_set library.
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set(el) tree<el,null_type,less<el>,rb_tree_tag,tree_order_statistics_node_update>
// AtCoder library. (Comment out these two lines if you're not submitting in AtCoder.) (Or if you want to use it in other judges, run expander.py first.)
//#include <atcoder/all>
//using namespace atcoder;
//Pragmas (Comment out these three lines if you're submitting in szkopul.)
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//File I/O.
#define FILE_IN "cseq.inp"
#define FILE_OUT "cseq.out"
#define ofile freopen(FILE_IN,"r",stdin);freopen(FILE_OUT,"w",stdout)
//Fast I/O.
#define fio ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define nfio cin.tie(0);cout.tie(0)
#define endl "\n"
//Order checking.
#define ord(a,b,c) ((a>=b)and(b>=c))
//min/max redefines, so i dont have to resolve annoying compile errors.
#define min(a,b) (((a)<(b))?(a):(b))
#define max(a,b) (((a)>(b))?(a):(b))
//Constants.
#define MOD (ll(998244353))
#define MAX 300001
#define mag 320
//Pairs and 3-pairs.
#define p1 first
#define p2 second.first
#define p3 second.second
#define fi first
#define se second
#define pii(element_type) pair<element_type,element_type>
#define piii(element_type) pair<element_type,pii(element_type)>
//Quick power of 2.
#define pow2(x) (ll(1)<<x)
//Short for-loops.
#define ff(i,__,___) for(int i=__;i<=___;i++)
#define rr(i,__,___) for(int i=__;i>=___;i--)
//Typedefs.
#define bi BigInt
typedef long long ll;
typedef long double ld;
typedef short sh;
//---------END-------//
ll n,m,i,j,k,t,t1,u,v,a,b,res;
ll l[1000001],r[1000001];
vector<ll> pos;
vector<ll> diff;
int main()
{
cin>>n;
cin>>a>>b;
for (i=1;i<=n;i++)
{
cin>>l[i]>>r[i];
}
u=gcd(a,b+1);
u=a/u;
if (ll(1e18)/u<b)
{
for (i=1;i<=n;i++)
{
v+=r[i]-l[i]+1;
}
cout<<v;
}
else
{
t=u*b;
// cout<<t<<endl;
for (i=1;i<=n;i++)
{
u=l[i]%t;
v=(r[i]+1)%t;
if (r[i]-l[i]+1>=t) return cout<<t,0;
else
{
if (u<v)
{
pos.push_back(u);
pos.push_back(v);
}
else
{
pos.push_back(u);
pos.push_back(t);
pos.push_back(0);
pos.push_back(v);
}
}
}
sort(pos.begin(),pos.end());
auto last=unique(pos.begin(),pos.end());
pos.erase(last,pos.end());
for (int g : pos) diff.push_back(0);
for (i=1;i<=n;i++)
{
u=l[i]%t;
v=(r[i]+1)%t;
// cout<<u<<' '<<v<<endl;
if (r[i]-l[i]+1>=t) return cout<<t,0;
else
{
if (u<v)
{
a=lower_bound(pos.begin(),pos.end(),u)-pos.begin();
diff[a]++;
a=lower_bound(pos.begin(),pos.end(),v)-pos.begin();
diff[a]--;
}
else
{
a=lower_bound(pos.begin(),pos.end(),u)-pos.begin();
diff[a]++;
a=lower_bound(pos.begin(),pos.end(),t)-pos.begin();
diff[a]--;
a=lower_bound(pos.begin(),pos.end(),0)-pos.begin();
diff[a]++;
a=lower_bound(pos.begin(),pos.end(),v)-pos.begin();
diff[a]--;
}
}
}
ll cur=0;
for (i=0;i<pos.size();i++)
{
cur+=diff[i];
// cout<<cur<<' '<<i<<' '<<pos[i]<<endl;
if ((cur)and(i<pos.size()-1)) res+=pos[i+1]-pos[i];
}
cout<<res;
}
}
Compilation message
strange_device.cpp:20: warning: ignoring #pragma comment [-Wunknown-pragmas]
20 | #pragma comment(linker, "/stack:200000000")
|
strange_device.cpp: In function 'int main()':
strange_device.cpp:118:12: warning: unused variable 'g' [-Wunused-variable]
118 | for (int g : pos) diff.push_back(0);
| ^
strange_device.cpp:148:13: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
148 | for (i=0;i<pos.size();i++)
| ~^~~~~~~~~~~
strange_device.cpp:152:18: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
152 | if ((cur)and(i<pos.size()-1)) res+=pos[i+1]-pos[i];
| ~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
18 ms |
1260 KB |
Output is correct |
3 |
Correct |
18 ms |
1260 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
18 ms |
1260 KB |
Output is correct |
17 |
Correct |
188 ms |
9052 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
492 KB |
Output is correct |
3 |
Correct |
2 ms |
492 KB |
Output is correct |
4 |
Correct |
2 ms |
492 KB |
Output is correct |
5 |
Correct |
1126 ms |
57908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1768 ms |
66676 KB |
Output is correct |
3 |
Correct |
1761 ms |
85016 KB |
Output is correct |
4 |
Correct |
1700 ms |
84936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1768 ms |
66676 KB |
Output is correct |
3 |
Correct |
1761 ms |
85016 KB |
Output is correct |
4 |
Correct |
1700 ms |
84936 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1745 ms |
85088 KB |
Output is correct |
7 |
Correct |
1799 ms |
84876 KB |
Output is correct |
8 |
Correct |
1762 ms |
85016 KB |
Output is correct |
9 |
Correct |
1832 ms |
84964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1768 ms |
66676 KB |
Output is correct |
3 |
Correct |
1761 ms |
85016 KB |
Output is correct |
4 |
Correct |
1700 ms |
84936 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
178 ms |
9052 KB |
Output is correct |
7 |
Correct |
193 ms |
9052 KB |
Output is correct |
8 |
Correct |
171 ms |
9056 KB |
Output is correct |
9 |
Correct |
172 ms |
9196 KB |
Output is correct |
10 |
Correct |
171 ms |
9060 KB |
Output is correct |
11 |
Correct |
175 ms |
9180 KB |
Output is correct |
12 |
Correct |
171 ms |
9052 KB |
Output is correct |
13 |
Correct |
197 ms |
9076 KB |
Output is correct |
14 |
Correct |
168 ms |
9052 KB |
Output is correct |
15 |
Correct |
181 ms |
9052 KB |
Output is correct |
16 |
Correct |
194 ms |
9052 KB |
Output is correct |
17 |
Correct |
168 ms |
9052 KB |
Output is correct |
18 |
Correct |
1741 ms |
84848 KB |
Output is correct |
19 |
Correct |
1720 ms |
84984 KB |
Output is correct |
20 |
Correct |
1810 ms |
84924 KB |
Output is correct |
21 |
Correct |
181 ms |
8972 KB |
Output is correct |
22 |
Correct |
164 ms |
9052 KB |
Output is correct |
23 |
Correct |
508 ms |
26584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
180 ms |
9052 KB |
Output is correct |
3 |
Correct |
177 ms |
9052 KB |
Output is correct |
4 |
Correct |
1852 ms |
84788 KB |
Output is correct |
5 |
Correct |
184 ms |
9064 KB |
Output is correct |
6 |
Correct |
177 ms |
9052 KB |
Output is correct |
7 |
Correct |
211 ms |
9052 KB |
Output is correct |
8 |
Correct |
180 ms |
9180 KB |
Output is correct |
9 |
Correct |
175 ms |
9052 KB |
Output is correct |
10 |
Correct |
183 ms |
9052 KB |
Output is correct |
11 |
Correct |
179 ms |
8956 KB |
Output is correct |
12 |
Correct |
169 ms |
9052 KB |
Output is correct |
13 |
Correct |
179 ms |
9052 KB |
Output is correct |
14 |
Correct |
1825 ms |
84820 KB |
Output is correct |
15 |
Correct |
180 ms |
9052 KB |
Output is correct |
16 |
Correct |
1703 ms |
84956 KB |
Output is correct |
17 |
Correct |
1725 ms |
84928 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
18 ms |
1260 KB |
Output is correct |
3 |
Correct |
18 ms |
1260 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
18 ms |
1260 KB |
Output is correct |
17 |
Correct |
188 ms |
9052 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
2 ms |
492 KB |
Output is correct |
26 |
Correct |
2 ms |
492 KB |
Output is correct |
27 |
Correct |
2 ms |
492 KB |
Output is correct |
28 |
Correct |
1126 ms |
57908 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1768 ms |
66676 KB |
Output is correct |
31 |
Correct |
1761 ms |
85016 KB |
Output is correct |
32 |
Correct |
1700 ms |
84936 KB |
Output is correct |
33 |
Correct |
1 ms |
364 KB |
Output is correct |
34 |
Correct |
1745 ms |
85088 KB |
Output is correct |
35 |
Correct |
1799 ms |
84876 KB |
Output is correct |
36 |
Correct |
1762 ms |
85016 KB |
Output is correct |
37 |
Correct |
1832 ms |
84964 KB |
Output is correct |
38 |
Correct |
1 ms |
512 KB |
Output is correct |
39 |
Correct |
178 ms |
9052 KB |
Output is correct |
40 |
Correct |
193 ms |
9052 KB |
Output is correct |
41 |
Correct |
171 ms |
9056 KB |
Output is correct |
42 |
Correct |
172 ms |
9196 KB |
Output is correct |
43 |
Correct |
171 ms |
9060 KB |
Output is correct |
44 |
Correct |
175 ms |
9180 KB |
Output is correct |
45 |
Correct |
171 ms |
9052 KB |
Output is correct |
46 |
Correct |
197 ms |
9076 KB |
Output is correct |
47 |
Correct |
168 ms |
9052 KB |
Output is correct |
48 |
Correct |
181 ms |
9052 KB |
Output is correct |
49 |
Correct |
194 ms |
9052 KB |
Output is correct |
50 |
Correct |
168 ms |
9052 KB |
Output is correct |
51 |
Correct |
1741 ms |
84848 KB |
Output is correct |
52 |
Correct |
1720 ms |
84984 KB |
Output is correct |
53 |
Correct |
1810 ms |
84924 KB |
Output is correct |
54 |
Correct |
181 ms |
8972 KB |
Output is correct |
55 |
Correct |
164 ms |
9052 KB |
Output is correct |
56 |
Correct |
508 ms |
26584 KB |
Output is correct |
57 |
Correct |
1 ms |
364 KB |
Output is correct |
58 |
Correct |
180 ms |
9052 KB |
Output is correct |
59 |
Correct |
177 ms |
9052 KB |
Output is correct |
60 |
Correct |
1852 ms |
84788 KB |
Output is correct |
61 |
Correct |
184 ms |
9064 KB |
Output is correct |
62 |
Correct |
177 ms |
9052 KB |
Output is correct |
63 |
Correct |
211 ms |
9052 KB |
Output is correct |
64 |
Correct |
180 ms |
9180 KB |
Output is correct |
65 |
Correct |
175 ms |
9052 KB |
Output is correct |
66 |
Correct |
183 ms |
9052 KB |
Output is correct |
67 |
Correct |
179 ms |
8956 KB |
Output is correct |
68 |
Correct |
169 ms |
9052 KB |
Output is correct |
69 |
Correct |
179 ms |
9052 KB |
Output is correct |
70 |
Correct |
1825 ms |
84820 KB |
Output is correct |
71 |
Correct |
180 ms |
9052 KB |
Output is correct |
72 |
Correct |
1703 ms |
84956 KB |
Output is correct |
73 |
Correct |
1725 ms |
84928 KB |
Output is correct |
74 |
Correct |
1 ms |
364 KB |
Output is correct |
75 |
Correct |
1 ms |
364 KB |
Output is correct |
76 |
Correct |
1 ms |
364 KB |
Output is correct |
77 |
Correct |
1 ms |
364 KB |
Output is correct |
78 |
Correct |
1 ms |
364 KB |
Output is correct |
79 |
Correct |
17 ms |
1260 KB |
Output is correct |
80 |
Correct |
1820 ms |
84916 KB |
Output is correct |
81 |
Correct |
1840 ms |
84812 KB |
Output is correct |
82 |
Correct |
1818 ms |
84884 KB |
Output is correct |
83 |
Correct |
1849 ms |
84892 KB |
Output is correct |
84 |
Correct |
1792 ms |
84748 KB |
Output is correct |
85 |
Correct |
1776 ms |
84964 KB |
Output is correct |
86 |
Correct |
502 ms |
26584 KB |
Output is correct |
87 |
Correct |
1 ms |
492 KB |
Output is correct |