제출 #171601

#제출 시각아이디문제언어결과실행 시간메모리
171601rama_pang이상한 기계 (APIO19_strange_device)C++14
65 / 100
791 ms19948 KiB
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
const lint INF = 1e18 + 100;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);

    int n;
    lint A, B;
    cin >> n >> A >> B;
    lint g = __gcd(A, B + 1);
    lint t = 0;

    if (A / g >= (INF / B) + 1) {
        t = INF;
    } else {
        t = (A / g) * B;
    }

    deque<pair<lint, lint>> interval;
    
    for (int i = 0; i < n; i++) {
        lint l, r;
        cin >> l >> r;
        l %= t;
        r %= t;
        if (l <= r) {
            interval.emplace_back(l, r);
        } else {
            interval.emplace_back(l, t - 1);
            interval.emplace_back(0, r);
        }
    }

    sort(begin(interval), end(interval));
    lint ans = 0;

    for (int i = 0; i < (int)interval.size(); i++) {
        lint l = interval[i].first;
        lint r = interval[i].second;
        int nxt = i;
        while (nxt + 1 < (int)interval.size() && interval[nxt + 1].first <= r) {
            r = max(r, interval[nxt + 1].second);
            nxt++;
        }
        ans += (r - l + 1);
        i = nxt;
    }

    cout << ans << "\n";
    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...