제출 #732845

#제출 시각아이디문제언어결과실행 시간메모리
732845Magikarp4000Strange Device (APIO19_strange_device)C++17
100 / 100
506 ms35208 KiB
#include <bits/stdc++.h>
using namespace std;
#define OPTM ios_base::sync_with_stdio(0); cin.tie(0);
#define INF int(1e9+7)
#define ln '\n' 
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define us unsigned short
#define FOR(i,s,n) for (int i = s; i < n; i++)
#define FORR(i,n,s) for (int i = n; i > s; i--)
#define FORX(u, arr) for (auto u : arr)
#define PB push_back
#define in(v,x) (v.find(x) != v.end())
#define F first
#define S second
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define UM unordered_map
#define US unordered_set
#define PQ priority_queue
#define ALL(v) v.begin(), v.end()
const ll LLINF = 2e18;

ll n,a,b;
vector<PLL> v;

__int128_t gcd(__int128_t a, __int128_t b) {
    return (b == 0) ? a : gcd(b,a%b);
}

int main() {
    OPTM;
    cin >> n >> a >> b;
    __int128_t p = __int128_t(a)*__int128_t(b)/gcd(__int128_t(a),__int128_t(b)+1);
    FOR(i,0,n) {
        ll l,r; cin >> l >> r;
        ll c = l%p, d = r%p;
        if (r-l+1 >= p) v.PB({0,p-1});
        else if (d >= c) v.PB({c,d});
        else {
            v.PB({c,p-1});
            v.PB({0ll,d});
        }
    }
    ll res = 0;
    sort(ALL(v));
    ll vn = v.size();
    ll s = v[0].F, e = v[0].S;
    FOR(i,1,vn) {
        if (v[i].F <= e) e = max(e,v[i].S);
        else {
            res += e-s+1;
            s = v[i].F;
            e = v[i].S;
        }
    }
    res += e-s+1;
    cout << res;
}
#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...