제출 #635699

#제출 시각아이디문제언어결과실행 시간메모리
635699PoonYaPatStrange Device (APIO19_strange_device)C++14
10 / 100
918 ms100436 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;

ll n,A,B,mod,ans;
int cnt;
set<pll> s; //(start, end)

void insert(pll a) {
    if (s.empty()) {
        s.insert(a);
        ans+=(a.second-a.first+1);
    } else {
        auto it1=s.upper_bound(pll(a.first,LLONG_MAX));
        auto it2=s.upper_bound(pll(a.second,LLONG_MAX));
        ll mmin, mmax;
        auto ed=it1,st=it1;

        if (it1==s.begin()) mmin=a.first;
        else {
            auto h=it1; --h;
            if ((*h).second>=a.first) mmin=(*h).first,st=h;
            else mmin=a.first,st=it1;
        }

        ed=it2;
        if (it2==s.begin()) mmax=a.second;
        else {
            --it2;
            mmax=max(a.second,(*it2).second);
        }

        for (auto k=st; k!=ed; ++k) {
            ans-=((*k).second-(*k).first+1);
            //if (cnt==2) cout<<(*k).first<<" "<<(*k).second<<"\n";
            //cout<<(*p).first<<" "<<(*p).second<<"\n";
            //auto p=k; s.erase(p);
        }

        s.insert(pll(mmin,mmax));
        ans+=(mmax-mmin+1);
    }
    ++cnt;
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin>>n>>A>>B;

    mod=(A*B)/__gcd(A,B+1);

    while (n--) {
        ll l,r;
        cin>>l>>r;
        if (r-l+1>=mod) {
            cout<<mod;
            return 0;
        }
        l%=mod;
        r%=mod;

        if (l<=r) insert(pll(l,r));
        else insert(pll(l,mod-1)), insert(pll(0,r));
    }

    cout<<ans;
}
/*
2 3 3
7 9
17 18
*/
#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...