제출 #407069

#제출 시각아이디문제언어결과실행 시간메모리
407069tengiz05다리 (APIO19_bridges)C++17
0 / 100
1 ms324 KiB
#include <bits/stdc++.h>
using i64 = long long;
constexpr i64 inf = 1e18 + 100;
int main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    i64 n, A, B;
    std::cin >> n >> A >> B;
    i64 gcd = std::__gcd(A, B + 1);
    i64 cycle = A / gcd;
    if (A / gcd > inf / B) {
        cycle = inf;
    } else {
        cycle *= B;
    }
    std::vector<std::pair<i64, i64>> v;
    for (int i = 0; i < n; i++) {
        i64 l, r;
        std::cin >> l >> r;
        if (r - l + 1 >= cycle) {
            std::cout << cycle << "\n";
            return 0;
        }
        l %= cycle;
        r %= cycle;
        if (l <= r) {
            v.emplace_back(l, r);
        } else {
            v.emplace_back(0, r);
            v.emplace_back(l, cycle - 1);
        }
    }
    sort(v.begin(), v.end());
    std::vector<std::pair<i64, i64>> nw;
    nw.push_back(v[0]);
    for (int i = 1; i < int(v.size()); i++) {
        if (nw[nw.size() - 1].second >= v[i].first) {
            nw[nw.size() - 1].first = std::min(nw[nw.size() - 1].first, v[i].first);
            nw[nw.size() - 1].second = std::max(nw[nw.size() - 1].second, v[i].second);
        } else {
            nw.emplace_back(v[i]);
        }
    }
    i64 ans = 0;
    for (auto [l, r] : nw) {
        ans += r - l + 1;
    }
    std::cout << ans << '\n';
}
#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...