Submission #785873

#TimeUsernameProblemLanguageResultExecution timeMemory
785873aykhnStrange Device (APIO19_strange_device)C++14
35 / 100
1220 ms66304 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

int n, a, b;

/*int x(int i)
{
    return (i + i/b) % a;
}
int y(int i)
{
    return i % b;
}*/

new main()
{
    cin >> n >> a >> b;

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

    set<pii> s;

    for (int i = 0; i < n; i++)
    {
        int a, b;
        cin >> a >> b;

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

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

    stack<pii> nw;

    nw.push(*s.begin());
    s.erase(s.begin());

    while (!s.empty())
    {
        pii t = nw.top();
        pii a = *s.begin();

        if (t.se < a.fi) nw.push(a);
        else
        {
            nw.pop();
            nw.push(mpr(min(t.fi, a.fi), max(t.se, a.se)));
        }

        s.erase(s.begin());
    }


    int res = 0;

    while (!nw.empty())
    {
        res += nw.top().se - nw.top().fi + 1;
        nw.pop();
    }

    cout << res << endl;
    return 0;
}
// 4, 5 -> 10;
// 3, 6 -> 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...