Submission #785879

#TimeUsernameProblemLanguageResultExecution timeMemory
785879aykhnStrange Device (APIO19_strange_device)C++14
100 / 100
607 ms115840 KiB
#include <bits/stdc++.h>

// author: aykhn

using namespace std;

typedef long long ll;

#define TC int t; cin >> t; while (t--) _();
#define OPT ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define all(v) v.begin(), v.end()
#define pii pair<int, int>
#define mpr make_pair
#define eb emplace_back
#define new int32_t
#define pb push_back
#define ts to_string
#define fi first
#define se second
#define int ll
#define ins insert
#define inf 0x3F3F3F3F
#define infll 0x3F3F3F3F3F3F3F3FLL
#define bpc __builtin_popcount

__int128 n, a, b;

/*int x(int i)
{
    return (i + i/b) % a;
}
int y(int i)
{
    return i % b;
}*/
__int128 read() {
    __int128 x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return x * f;
}
void print(__int128 x) {
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x > 9) print(x / 10);
    putchar(x % 10 + '0');
}
bool cmp(__int128 x, __int128 y) { return x >= y; }

new main()
{
    n = read();
    a = read();
    b = read();

    __int128 x = a/__gcd(a, b + 1) * b;

    set<pair<__int128, __int128>> s;

    for (__int128 i = 0; i < n; i++)
    {
        __int128 a, b;
        a = read();
        b = read();

        if (b - a + 1 >= x)
        {
            s.ins(mpr(1, x));
        }
        else
        {
            __int128 l = a % x + 1;
            __int128 r = b % x + 1;

            if (l <= r)
            {
                s.ins(mpr(l, r));
            }
            else
            {
                s.ins(mpr(l, x));
                s.ins(mpr(1, r));
            }
        }
    }

    set<pair<__int128, __int128>> nw;

    while (s.size() > 1)
    {
        auto it = s.begin();
        pair<__int128, __int128> a = *it;
        it++;
        pair<__int128, __int128> b = *it;

        if (max(a.fi, b.fi) <= min(a.se, b.se))
        {
            s.erase(s.begin());
            s.erase(s.begin());
            s.ins(mpr(min(a.fi, b.fi), max(a.se, b.se)));
        }
        else
        {
            s.erase(s.begin());
            nw.ins(a);
        }
    }

    nw.ins(*s.begin());

    __int128 res = 0;

    for (pii a : nw)
    {
        res += a.se - a.fi + 1;
    }

    print(res);
    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...