Submission #591729

#TimeUsernameProblemLanguageResultExecution timeMemory
591729piOOEStrange Device (APIO19_strange_device)C++17
100 / 100
657 ms86172 KiB
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
 
#define all(x) begin(x), end(x)
#define mp make_pair
#define pb push_back
#define sz(x) ((int) size(x))
 
const int infI = 1e9 + 7;
const ll infL = 3e18;
const int N = 1000001;
 
ll L[N], R[N];
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    ll A, B;
    cin >> A >> B;
    for (int i = 0; i < n; ++i) {
        cin >> L[i] >> R[i];
    }
    ll d = gcd(B + 1, A);
    ll c = A / d;
    if (LLONG_MAX / B <= c) {
        ll ans = 0;
        for (int i = 0; i < n; ++i) {
            ans += R[i] - L[i] + 1;
        }
        cout << ans;
        return 0;
    }
    vector<pair<ll, int>> v;
    ll Z = B * c;
    for (int i = 0; i < n; ++i) {
        if (R[i] / Z == L[i] / Z) {
            v.emplace_back(L[i] % Z, 0);
            v.emplace_back(R[i] % Z, 1);
        } else {
            if ((R[i] - L[i] + 1) / B >= c) {
                cout << Z;
                return 0;
            } else {
                ll nxt = (L[i] + Z - 1) / Z * Z;
                assert(nxt <= R[i]);
                assert(R[i] / Z * Z == nxt);
                v.emplace_back(L[i] % Z, 0);
                v.emplace_back(Z - 1, 1);
                v.emplace_back(0, 0);
                v.emplace_back(R[i] % Z, 1);
            }
        }
    }
    ll last = 0;
    int opened = 0;
    sort(all(v));
    ll ans = 0;
    for (auto [x, t] : v) {
        if (t == 0) {
            ++opened;
            if (opened == 1) {
                last = x;
            }
        } else {
            --opened;
            if (opened == 0) {
                ans += x - last + 1;
            }
        }
    }
    cout << ans;
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...