이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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;
multiset<pair<ll, int>> s;
for(int i = 0; i < n; i++){
ll tl = v[i].first;
ll tr = v[i].second;
ll sub = tl / mod;
tl -= sub * mod;
tr -= sub * mod;
if(tr - tl + 1 >= mod){
cout << mod << endl;
exit(0);
}
if(tr >= mod){
s.insert({0, 0});
s.insert({tr - mod, 1});
s.insert({mod - 1, 1});
s.insert({tl, 0});
}
else{
s.insert({tl, 0});
s.insert({tr, 1});
}
}
ll ans = 0;
int cnt = 0;
ll beg = -1;
for(auto it = s.begin(); it != s.end(); it++){
if(it->second == 0 && cnt == 0){
beg = it->first;
}
if(it->second == 0){
cnt++;
}
if(it->second == 1 && cnt == 1){
ans += it->first - beg + 1;
}
if(it->second == 1)
cnt--;
}
cout << ans << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |