제출 #440402

#제출 시각아이디문제언어결과실행 시간메모리
440402KULIKOLD이상한 기계 (APIO19_strange_device)C++17
100 / 100
887 ms86180 KiB
//#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
const int DIM = 1E6+7;
pair<ll,ll> M[DIM];
const ll MAXN = 2E18;
ll mult(ll a,ll b){
    ll mult = b;
    b = 0;
    while(a){
        if (a&1)
            b = min(b+mult,MAXN);
        mult = min(MAXN,mult*2);
        a/=2;
    }
    return b;
}
int solve(){
    ll n,A,B;
    cin>>n>>A>>B;
    for(int i = 1;i<=n;++i){
        cin>>M[i].first>>M[i].second;
    }
    ll gcd = __gcd(A,(B+1)%A);
    ll d = A/gcd;
    B = d*B;
    vector<pair<ll,ll> > V;
    ll cnt = 0,sum = 0;
    for(int i = 1;i<=n;++i){
        sum+=M[i].second-M[i].first+1;
        if (M[i].first%B!=0) {
            ll start = M[i].first % B;
            ll fin = B - 1;
            if (fin - start > M[i].second - M[i].first) {
                V.push_back({M[i].first % B, 1});
                V.push_back({M[i].second % B + 1, -1});
                continue;
            }
            V.push_back({start, 1});
            V.push_back({fin + 1, -1});
            M[i].first += fin - start + 1;
        }
        if (M[i].second%B!=B-1) {
            ll fin = M[i].second % B;
            ll start = 0;
            if (fin - start > M[i].second - M[i].first) {
                V.push_back({M[i].first % B, 1});
                V.push_back({M[i].second % B + 1, -1});
                continue;
            }
            V.push_back({start, 1});
            V.push_back({fin + 1, -1});
            M[i].second -= fin - start + 1;

        }
        cnt += (M[i].second - M[i].first+1) / B;
    }
    sort(V.begin(),V.end());
    ll last = 0,res = 0;
    for(int i = 0;i<int(V.size());++i){
        res+=(V[i].first-last)*max(0ll,cnt-1);
        last = V[i].first;
        int cur = V[i].first;
        cnt+=V[i].second;
        while(i+1<int(V.size()) && V[i+1].first==cur){
            cnt+=V[++i].second;
        }
    }
    res+=(B-last)*max(0ll,cnt-1);
    cout<<sum-res<<endl;

    return 1;
}
int main() {

    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    assert(solve());
    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...