Submission #1313303

#TimeUsernameProblemLanguageResultExecution timeMemory
1313303TimoshStrange Device (APIO19_strange_device)C++20
100 / 100
336 ms47564 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T>
using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define int long long
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define vi vector<int>
#define vii vector<vi>
#define ld long double
#define pii pair<int, int>
mt19937 mt(time(0));

namespace io
{
    template <typename T, typename F>
    istream &operator>>(istream &cin, pair<T, F> &pr)
    {
        cin >> pr.first >> pr.second;
        return cin;
    }
    template <typename T, typename F>
    ostream &operator<<(ostream &cout, pair<T, F> &pr)
    {
        cout << pr.first << ' ' << pr.second;
        return cout;
    }
    template <typename T>
    istream &operator>>(istream &cin, vector<T> &vec)
    {
        for (T &i : vec)
            cin >> i;
        return cin;
    }
    template <typename T>
    ostream &operator<<(ostream &cout, vector<T> vec)
    {
        for (T i : vec)
            cout << i << ' ';
        return cout;
    }
}
using namespace io;

void solve()
{
    int n, a, b;
    int inf = 2e18;
    cin >> n >> a >> b;
    vector<pii> v(n);
    cin >> v;
    int A = a / __gcd(a, b + 1);
    int m;
    if (inf / A < b)
    {
        sort(all(v));
        int R = -1;
        int ans = 0;
        for (auto &[l, r] : v)
        {
            if (r < R)
                continue;
            ans += r - max(l, R + 1) + 1;
            R = max(R, r);
        }
        cout << ans;
        return;
    }
    else
    {
        m = A * b;
        for (auto &[l, r] : v)
        {
            int x = l / m;
            l -= x * m;
            r -= x * m;
            if (r - l + 1 >= m)
                return cout << m, void();
        }
        vector<pii> nw;
        for (auto &[l, r] : v)
        {
            if (r < m)
                nw.push_back({l, r});
            else
            {
                nw.push_back({l, m - 1});
                nw.push_back({0, r - m});
            }
        }
        v = nw;
        sort(all(v));
        int R = -1;
        int ans = 0;
        for (auto &[l, r] : v)
        {
            if (r < R)
                continue;
            ans += r - max(l, R + 1) + 1;
            R = max(R, r);
        }
        cout << ans;
    }
}

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
    // cin >> t;
    while (t--)
        solve(), cout << '\n';
    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...