Submission #252087

#TimeUsernameProblemLanguageResultExecution timeMemory
252087jeqcho이상한 기계 (APIO19_strange_device)C++17
100 / 100
587 ms16760 KiB

#include<bits/stdc++.h>
#define ll long long
#define N 1000005
 
using namespace std;
inline ll Get() {ll x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}while('0'<=ch&&ch<='9') {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}return x*f;}
 
int n;
ll A,B,len;
struct node {
    ll l,r;
    bool operator <(const node &a)const {
        if(l!=a.l) return l<a.l;
        return r<a.r;
    }
}s[N<<1];
ll gcd(ll a,ll b) {return !b?a:gcd(b,a%b);}
 
int main() {
    n=Get(),A=Get(),B=Get();
    for(int i=1;i<=n;i++) s[i].l=Get(),s[i].r=Get();
    ll g=gcd(A,B+1);
    ll ans=0;
    if((long double)A/g*B>1e18) {
        for(int i=1;i<=n;i++) ans+=s[i].r-s[i].l+1;
    } else {
        ll len=A/g*B;
        int tot=n;
        for(int i=1;i<=n;i++) {
            if(s[i].r-s[i].l+1>=len) {
                cout<<len;
                return 0;
            }
            s[i].l%=len,s[i].r%=len;
            if(s[i].l>s[i].r) {
                s[++tot].l=0,s[tot].r=s[i].r;
                s[i].r=len-1;
            }
        }
        sort(s+1,s+1+tot);
        ll last=-1;
        for(int i=1;i<=tot;i++) {
            if(s[i].r<last) continue ;
            ans+=s[i].r-max(s[i].l-1,last);
            last=s[i].r;
        }
    }
    cout<<ans;
    
    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...