// In the name of god
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template < class T > using ordered_set = tree < T , null_type , less < T > , rb_tree_tag , tree_order_statistics_node_update >;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define all(x) (x).begin(),(x).end()
#define lc(x) (x) << 1
#define rc(x) (x) << 1 | 1
#define F first
#define S second
#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("in.txt" , "r+" , stdin) ; freopen("out.txt" , "w+" , stdout);
#define mp make_pair
ll poww(ll a, ll b, ll md) {
ll ans = 1;
for(; b; b >>= 1, a = a * a % md){
if(b & 1){
ans = ans * a % md;
}
}
return ans % md;
}
const int MAXN = 2e6 + 10;
const int MAXLG = 18;
const int MOD = 1e9 + 7;
const int MOD2 = 998244353;
const ll INF = 2e18 + 10;
ll n, val[MAXN];
ll A, B, ans, l[MAXN], r[MAXN];
vector<ll> vec;
vector<pll> seg;
unordered_map<ll, ll> um;
int main()
{
um.max_load_factor(0.13);
scanf("%lld %lld %lld", &n, &A, &B);
for(ll i = 1; i <= n; i++){
scanf("%lld %lld", l + i, r + i);
}
ll x = A / __gcd(A, B + 1);
if(x > INF / B){
for(ll i = 1; i <= n; i++){
ans += r[i] - l[i] + 1;
}
printf("%lld\n", ans);
return 0;
}
x *= B;
vec.push_back(0);
vec.push_back(x);
for(ll i = 1; i <= n; i++){
if(r[i] - l[i] + 1 >= x){
printf("%lld\n", x);
return 0;
}
ll L = l[i], R = r[i];
L %= x;
R %= x;
vec.push_back(L);
vec.push_back(R + 1);
if(L > R){
seg.push_back(mp(L, x - 1));
seg.push_back(mp(0, R));
}
else{
seg.push_back(mp(L, R));
}
}
sort(all(vec));
vec.erase(unique(all(vec)), vec.end());
for(int i = 0; i < (int)vec.size(); i++){
um[vec[i]] = i;
}
for(auto v : seg){
ll ind1;
ll ind2;
ind1 = um[v.F];
ind2 = um[v.S + 1];
val[ind1]++;
val[ind2]--;
}
for(ll i = 1; i < (ll)vec.size(); i++){
val[i] += val[i - 1];
}
for(ll i = 0; i < (ll)vec.size() - 1; i++){
if(val[i]){
ans += vec[i + 1] - vec[i];
}
}
printf("%lld\n", ans);
}
Compilation message
strange_device.cpp:3: warning: ignoring #pragma comment [-Wunknown-pragmas]
3 | #pragma comment(linker, "/stack:200000000")
|
strange_device.cpp: In function 'int main()':
strange_device.cpp:51:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
51 | scanf("%lld %lld %lld", &n, &A, &B);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
strange_device.cpp:53:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
53 | scanf("%lld %lld", l + i, r + i);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
8 ms |
2508 KB |
Output is correct |
3 |
Correct |
8 ms |
2508 KB |
Output is correct |
4 |
Correct |
0 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 |
0 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 |
364 KB |
Output is correct |
16 |
Correct |
8 ms |
2508 KB |
Output is correct |
17 |
Correct |
83 ms |
19472 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
620 KB |
Output is correct |
3 |
Correct |
1 ms |
620 KB |
Output is correct |
4 |
Correct |
1 ms |
620 KB |
Output is correct |
5 |
Correct |
409 ms |
47636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
783 ms |
168608 KB |
Output is correct |
3 |
Correct |
1081 ms |
168464 KB |
Output is correct |
4 |
Correct |
1047 ms |
168480 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
783 ms |
168608 KB |
Output is correct |
3 |
Correct |
1081 ms |
168464 KB |
Output is correct |
4 |
Correct |
1047 ms |
168480 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1044 ms |
168460 KB |
Output is correct |
7 |
Correct |
774 ms |
168608 KB |
Output is correct |
8 |
Correct |
1025 ms |
168340 KB |
Output is correct |
9 |
Correct |
1160 ms |
168608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
783 ms |
168608 KB |
Output is correct |
3 |
Correct |
1081 ms |
168464 KB |
Output is correct |
4 |
Correct |
1047 ms |
168480 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
77 ms |
19452 KB |
Output is correct |
7 |
Correct |
84 ms |
19580 KB |
Output is correct |
8 |
Correct |
84 ms |
19452 KB |
Output is correct |
9 |
Correct |
100 ms |
19452 KB |
Output is correct |
10 |
Correct |
80 ms |
19452 KB |
Output is correct |
11 |
Correct |
102 ms |
19452 KB |
Output is correct |
12 |
Correct |
98 ms |
19452 KB |
Output is correct |
13 |
Correct |
106 ms |
19572 KB |
Output is correct |
14 |
Correct |
82 ms |
19452 KB |
Output is correct |
15 |
Correct |
100 ms |
19580 KB |
Output is correct |
16 |
Correct |
100 ms |
19452 KB |
Output is correct |
17 |
Correct |
93 ms |
19452 KB |
Output is correct |
18 |
Correct |
812 ms |
168480 KB |
Output is correct |
19 |
Correct |
1060 ms |
168500 KB |
Output is correct |
20 |
Correct |
1163 ms |
168676 KB |
Output is correct |
21 |
Correct |
103 ms |
20860 KB |
Output is correct |
22 |
Correct |
94 ms |
20732 KB |
Output is correct |
23 |
Correct |
193 ms |
20552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
85 ms |
19452 KB |
Output is correct |
3 |
Correct |
103 ms |
19452 KB |
Output is correct |
4 |
Correct |
1134 ms |
168352 KB |
Output is correct |
5 |
Correct |
103 ms |
20732 KB |
Output is correct |
6 |
Correct |
100 ms |
20620 KB |
Output is correct |
7 |
Correct |
101 ms |
20732 KB |
Output is correct |
8 |
Correct |
103 ms |
20732 KB |
Output is correct |
9 |
Correct |
99 ms |
20732 KB |
Output is correct |
10 |
Correct |
97 ms |
20732 KB |
Output is correct |
11 |
Correct |
117 ms |
20732 KB |
Output is correct |
12 |
Correct |
96 ms |
20732 KB |
Output is correct |
13 |
Correct |
109 ms |
20732 KB |
Output is correct |
14 |
Correct |
912 ms |
170784 KB |
Output is correct |
15 |
Correct |
86 ms |
20604 KB |
Output is correct |
16 |
Correct |
804 ms |
170412 KB |
Output is correct |
17 |
Correct |
858 ms |
170104 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
8 ms |
2508 KB |
Output is correct |
3 |
Correct |
8 ms |
2508 KB |
Output is correct |
4 |
Correct |
0 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 |
0 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 |
364 KB |
Output is correct |
16 |
Correct |
8 ms |
2508 KB |
Output is correct |
17 |
Correct |
83 ms |
19472 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
0 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 |
0 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
620 KB |
Output is correct |
26 |
Correct |
1 ms |
620 KB |
Output is correct |
27 |
Correct |
1 ms |
620 KB |
Output is correct |
28 |
Correct |
409 ms |
47636 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
783 ms |
168608 KB |
Output is correct |
31 |
Correct |
1081 ms |
168464 KB |
Output is correct |
32 |
Correct |
1047 ms |
168480 KB |
Output is correct |
33 |
Correct |
1 ms |
364 KB |
Output is correct |
34 |
Correct |
1044 ms |
168460 KB |
Output is correct |
35 |
Correct |
774 ms |
168608 KB |
Output is correct |
36 |
Correct |
1025 ms |
168340 KB |
Output is correct |
37 |
Correct |
1160 ms |
168608 KB |
Output is correct |
38 |
Correct |
1 ms |
364 KB |
Output is correct |
39 |
Correct |
77 ms |
19452 KB |
Output is correct |
40 |
Correct |
84 ms |
19580 KB |
Output is correct |
41 |
Correct |
84 ms |
19452 KB |
Output is correct |
42 |
Correct |
100 ms |
19452 KB |
Output is correct |
43 |
Correct |
80 ms |
19452 KB |
Output is correct |
44 |
Correct |
102 ms |
19452 KB |
Output is correct |
45 |
Correct |
98 ms |
19452 KB |
Output is correct |
46 |
Correct |
106 ms |
19572 KB |
Output is correct |
47 |
Correct |
82 ms |
19452 KB |
Output is correct |
48 |
Correct |
100 ms |
19580 KB |
Output is correct |
49 |
Correct |
100 ms |
19452 KB |
Output is correct |
50 |
Correct |
93 ms |
19452 KB |
Output is correct |
51 |
Correct |
812 ms |
168480 KB |
Output is correct |
52 |
Correct |
1060 ms |
168500 KB |
Output is correct |
53 |
Correct |
1163 ms |
168676 KB |
Output is correct |
54 |
Correct |
103 ms |
20860 KB |
Output is correct |
55 |
Correct |
94 ms |
20732 KB |
Output is correct |
56 |
Correct |
193 ms |
20552 KB |
Output is correct |
57 |
Correct |
1 ms |
364 KB |
Output is correct |
58 |
Correct |
85 ms |
19452 KB |
Output is correct |
59 |
Correct |
103 ms |
19452 KB |
Output is correct |
60 |
Correct |
1134 ms |
168352 KB |
Output is correct |
61 |
Correct |
103 ms |
20732 KB |
Output is correct |
62 |
Correct |
100 ms |
20620 KB |
Output is correct |
63 |
Correct |
101 ms |
20732 KB |
Output is correct |
64 |
Correct |
103 ms |
20732 KB |
Output is correct |
65 |
Correct |
99 ms |
20732 KB |
Output is correct |
66 |
Correct |
97 ms |
20732 KB |
Output is correct |
67 |
Correct |
117 ms |
20732 KB |
Output is correct |
68 |
Correct |
96 ms |
20732 KB |
Output is correct |
69 |
Correct |
109 ms |
20732 KB |
Output is correct |
70 |
Correct |
912 ms |
170784 KB |
Output is correct |
71 |
Correct |
86 ms |
20604 KB |
Output is correct |
72 |
Correct |
804 ms |
170412 KB |
Output is correct |
73 |
Correct |
858 ms |
170104 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 |
11 ms |
2892 KB |
Output is correct |
80 |
Correct |
1145 ms |
170224 KB |
Output is correct |
81 |
Correct |
1116 ms |
170320 KB |
Output is correct |
82 |
Correct |
1148 ms |
170308 KB |
Output is correct |
83 |
Correct |
1135 ms |
170204 KB |
Output is correct |
84 |
Correct |
1098 ms |
171296 KB |
Output is correct |
85 |
Correct |
1112 ms |
171296 KB |
Output is correct |
86 |
Correct |
172 ms |
19912 KB |
Output is correct |
87 |
Correct |
1 ms |
364 KB |
Output is correct |