#include <bits/stdc++.h>
#define pb push_back
// #define endl ("\n")
#define all(aa) aa.begin(), aa.end()
typedef long long ll;
using namespace std;
ll ggcd(ll a, ll b){
if(b == 0) return a;
return ggcd(b, a % b);
}
const ll inf = ll(1e18) + 1;
int main(){
ll n, a, b;
cin >> n >> a >> b;
vector<pair<ll, ll>> v(n);
ll mx = 0;
for(int i = 0; i < n; i++){
ll l, r;
cin >> l >> r;
v[i] = {l, r};
mx = max(mx, r);
}
ll g = ggcd(a, b + 1);
ll sade = a / g;
ll mod;
if(double(sade) * double(b) >= double(inf)){
mod = inf;
}
else mod = sade * b;
set<pair<ll, int>> s;
s.insert({-1, 1});
s.insert({mod, 0});
function<void(ll, ll)> add = [&](ll ql, ll qr){
// cout << ql << ' ' << qr << endl;
auto ileri = s.lower_bound({ql, 0});
auto geri = prev(ileri);
if(ileri->second == 0){
if(qr >= ileri->first)
s.erase(ileri);
s.insert({ql, 0});
}
ileri = s.lower_bound({qr, 0});
geri = prev(ileri);
if(ileri->second == 0){
if(geri->second == 1 && ql <= geri->first)
s.erase(geri);
s.insert({qr, 1});
}
// for(auto [x, y] : s){
// // if(x == -1 || x == mod) continue;
// // if(y == 0)
// // ans -= x;
// // else
// // ans += x + 1;
// cout << x << ' ' << y << endl;
// } cout << endl;
};
for(int i = 0; i < n; i++){
ll tl = v[i].first;
ll tr = v[i].second;
if(tr - tl + 1 >= mod){
cout << mod << endl;
exit(0);
}
ll sub = v[i].first / mod;
tl -= sub * mod;
tr -= sub * mod;
if(tr < mod){
add(tl, tr);
}
else{
add(tl, mod - 1);
add(0, tr - mod);
}
}
ll ans = 0;
for(auto [x, y] : s){
if(x == -1 || x == mod) continue;
if(y == 0)
ans -= x;
else
ans += x + 1;
// cout << x << ' ' << y << endl;
}
cout << ans << endl;
}
Compilation message
strange_device.cpp: In function 'int main()':
strange_device.cpp:85:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
85 | for(auto [x, y] : s){
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
10 ms |
1632 KB |
Output is correct |
3 |
Correct |
9 ms |
1628 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
1127 ms |
141096 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
1127 ms |
141096 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
1127 ms |
141096 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
107 ms |
14272 KB |
Output is correct |
3 |
Correct |
105 ms |
14420 KB |
Output is correct |
4 |
Correct |
1166 ms |
141272 KB |
Output is correct |
5 |
Correct |
109 ms |
14420 KB |
Output is correct |
6 |
Correct |
112 ms |
14284 KB |
Output is correct |
7 |
Correct |
108 ms |
14416 KB |
Output is correct |
8 |
Correct |
103 ms |
14420 KB |
Output is correct |
9 |
Correct |
104 ms |
14420 KB |
Output is correct |
10 |
Correct |
116 ms |
14420 KB |
Output is correct |
11 |
Correct |
102 ms |
14420 KB |
Output is correct |
12 |
Correct |
101 ms |
14420 KB |
Output is correct |
13 |
Correct |
111 ms |
14288 KB |
Output is correct |
14 |
Correct |
1223 ms |
141140 KB |
Output is correct |
15 |
Correct |
89 ms |
13008 KB |
Output is correct |
16 |
Correct |
1105 ms |
141140 KB |
Output is correct |
17 |
Correct |
1064 ms |
141140 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
10 ms |
1632 KB |
Output is correct |
3 |
Correct |
9 ms |
1628 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |