Submission #216355

# Submission time Handle Problem Language Result Execution time Memory
216355 2020-03-27T07:47:28 Z usachevd0 Strange Device (APIO19_strange_device) C++14
100 / 100
798 ms 46020 KB
#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(a) (a).begin(), (a).end()

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef long double ld;

void debug_out()
{
    cerr << endl;
}

template<typename T1, typename... T2> void debug_out(T1 A, T2... B)
{
    cerr << ' ' << A;
    debug_out(B...);
}

#ifdef DEBUG
    #define time(...) 42
    #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
    #define debug(...) 42
#endif

template<typename T1, typename T2> bool chkmin(T1 &x, T2 y) { return y < x ? (x = y, true) : false; }
template<typename T1, typename T2> bool chkmax(T1 &x, T2 y) { return y > x ? (x = y, true) : false; }

ll gcd(ll a, ll b)
{
    for (; b; swap(a, b)) a %= b;
    return a;
}

signed main()
{
#ifdef DEBUG
    freopen("in", "r", stdin);
#endif
    ios::sync_with_stdio(0);
    cin.tie(0);

    int n;
    ll A, B;
    cin >> n >> A >> B;
    ll M1 = A / gcd(B + 1, A);
    ld temp = M1 * (ld)B;
    if (temp > 2000000000000000100LL)
    {
        ll sum = 0;
        while (n--)
        {
            ll l, r;
            cin >> l >> r;
            sum += r - l + 1;
        }
        cout << sum << '\n';
        exit(0);
    }
    ll M = M1 * B;
    vector<pll> ev;
    while (n--)
    {
        ll l, r;
        cin >> l >> r;
        if (r - l + 1 >= M)
        {
            cout << M << '\n';
            exit(0);
        }
        l %= M;
        r %= M;
        if (l <= r)
        {
            ev.emplace_back(l, 0);
            ev.emplace_back(r, 1);
        }
        else
        {
            ev.emplace_back(0, 0);
            ev.emplace_back(r, 1);
            ev.emplace_back(l, 0);
            ev.emplace_back(M - 1, 1);
        }
    }
    sort(all(ev));
    int cnt = 0;
    ll last = -1;
    ll ans = 0;
    for (auto &e : ev)
    {
        if (e.se == 0)
        {
            ++cnt;
            if (cnt == 1)
            {
                last = e.fi;
            }
        }
        else
        {
            --cnt;
            if (cnt == 0)
            {
                ans += e.fi - last + 1;
            }
        }
    }
    cout << ans << '\n';

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 10 ms 1024 KB Output is correct
3 Correct 10 ms 1024 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 4 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 4 ms 384 KB Output is correct
9 Correct 4 ms 384 KB Output is correct
10 Correct 4 ms 384 KB Output is correct
11 Correct 4 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 4 ms 384 KB Output is correct
14 Correct 4 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 11 ms 1024 KB Output is correct
17 Correct 76 ms 4596 KB Output is correct
18 Correct 4 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 4 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 445 ms 41920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 640 ms 33392 KB Output is correct
3 Correct 634 ms 33344 KB Output is correct
4 Correct 704 ms 45908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 640 ms 33392 KB Output is correct
3 Correct 634 ms 33344 KB Output is correct
4 Correct 704 ms 45908 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 661 ms 45992 KB Output is correct
7 Correct 647 ms 45764 KB Output is correct
8 Correct 643 ms 45888 KB Output is correct
9 Correct 764 ms 45888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 640 ms 33392 KB Output is correct
3 Correct 634 ms 33344 KB Output is correct
4 Correct 704 ms 45908 KB Output is correct
5 Correct 5 ms 512 KB Output is correct
6 Correct 68 ms 7276 KB Output is correct
7 Correct 67 ms 7276 KB Output is correct
8 Correct 65 ms 7276 KB Output is correct
9 Correct 74 ms 7272 KB Output is correct
10 Correct 66 ms 7276 KB Output is correct
11 Correct 70 ms 7284 KB Output is correct
12 Correct 65 ms 7400 KB Output is correct
13 Correct 72 ms 7396 KB Output is correct
14 Correct 73 ms 7276 KB Output is correct
15 Correct 74 ms 7276 KB Output is correct
16 Correct 75 ms 7272 KB Output is correct
17 Correct 67 ms 7396 KB Output is correct
18 Correct 705 ms 44616 KB Output is correct
19 Correct 668 ms 45856 KB Output is correct
20 Correct 798 ms 46020 KB Output is correct
21 Correct 75 ms 7272 KB Output is correct
22 Correct 68 ms 7368 KB Output is correct
23 Correct 186 ms 26708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 70 ms 4588 KB Output is correct
3 Correct 70 ms 4588 KB Output is correct
4 Correct 773 ms 33320 KB Output is correct
5 Correct 88 ms 4588 KB Output is correct
6 Correct 70 ms 4588 KB Output is correct
7 Correct 72 ms 4588 KB Output is correct
8 Correct 72 ms 4584 KB Output is correct
9 Correct 69 ms 4588 KB Output is correct
10 Correct 75 ms 4588 KB Output is correct
11 Correct 77 ms 4584 KB Output is correct
12 Correct 64 ms 4584 KB Output is correct
13 Correct 76 ms 7276 KB Output is correct
14 Correct 729 ms 45760 KB Output is correct
15 Correct 77 ms 7272 KB Output is correct
16 Correct 616 ms 45760 KB Output is correct
17 Correct 631 ms 45756 KB Output is correct
18 Correct 5 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 10 ms 1024 KB Output is correct
3 Correct 10 ms 1024 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 4 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 4 ms 384 KB Output is correct
9 Correct 4 ms 384 KB Output is correct
10 Correct 4 ms 384 KB Output is correct
11 Correct 4 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 4 ms 384 KB Output is correct
14 Correct 4 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 11 ms 1024 KB Output is correct
17 Correct 76 ms 4596 KB Output is correct
18 Correct 4 ms 384 KB Output is correct
19 Correct 4 ms 384 KB Output is correct
20 Correct 4 ms 384 KB Output is correct
21 Correct 4 ms 384 KB Output is correct
22 Correct 5 ms 384 KB Output is correct
23 Correct 4 ms 384 KB Output is correct
24 Correct 4 ms 384 KB Output is correct
25 Correct 5 ms 384 KB Output is correct
26 Correct 5 ms 384 KB Output is correct
27 Correct 5 ms 384 KB Output is correct
28 Correct 445 ms 41920 KB Output is correct
29 Correct 5 ms 384 KB Output is correct
30 Correct 640 ms 33392 KB Output is correct
31 Correct 634 ms 33344 KB Output is correct
32 Correct 704 ms 45908 KB Output is correct
33 Correct 5 ms 384 KB Output is correct
34 Correct 661 ms 45992 KB Output is correct
35 Correct 647 ms 45764 KB Output is correct
36 Correct 643 ms 45888 KB Output is correct
37 Correct 764 ms 45888 KB Output is correct
38 Correct 5 ms 512 KB Output is correct
39 Correct 68 ms 7276 KB Output is correct
40 Correct 67 ms 7276 KB Output is correct
41 Correct 65 ms 7276 KB Output is correct
42 Correct 74 ms 7272 KB Output is correct
43 Correct 66 ms 7276 KB Output is correct
44 Correct 70 ms 7284 KB Output is correct
45 Correct 65 ms 7400 KB Output is correct
46 Correct 72 ms 7396 KB Output is correct
47 Correct 73 ms 7276 KB Output is correct
48 Correct 74 ms 7276 KB Output is correct
49 Correct 75 ms 7272 KB Output is correct
50 Correct 67 ms 7396 KB Output is correct
51 Correct 705 ms 44616 KB Output is correct
52 Correct 668 ms 45856 KB Output is correct
53 Correct 798 ms 46020 KB Output is correct
54 Correct 75 ms 7272 KB Output is correct
55 Correct 68 ms 7368 KB Output is correct
56 Correct 186 ms 26708 KB Output is correct
57 Correct 5 ms 384 KB Output is correct
58 Correct 70 ms 4588 KB Output is correct
59 Correct 70 ms 4588 KB Output is correct
60 Correct 773 ms 33320 KB Output is correct
61 Correct 88 ms 4588 KB Output is correct
62 Correct 70 ms 4588 KB Output is correct
63 Correct 72 ms 4588 KB Output is correct
64 Correct 72 ms 4584 KB Output is correct
65 Correct 69 ms 4588 KB Output is correct
66 Correct 75 ms 4588 KB Output is correct
67 Correct 77 ms 4584 KB Output is correct
68 Correct 64 ms 4584 KB Output is correct
69 Correct 76 ms 7276 KB Output is correct
70 Correct 729 ms 45760 KB Output is correct
71 Correct 77 ms 7272 KB Output is correct
72 Correct 616 ms 45760 KB Output is correct
73 Correct 631 ms 45756 KB Output is correct
74 Correct 5 ms 384 KB Output is correct
75 Correct 5 ms 384 KB Output is correct
76 Correct 5 ms 384 KB Output is correct
77 Correct 5 ms 384 KB Output is correct
78 Correct 4 ms 384 KB Output is correct
79 Correct 11 ms 1408 KB Output is correct
80 Correct 779 ms 45888 KB Output is correct
81 Correct 783 ms 45892 KB Output is correct
82 Correct 720 ms 44444 KB Output is correct
83 Correct 732 ms 45888 KB Output is correct
84 Correct 733 ms 44572 KB Output is correct
85 Correct 722 ms 44444 KB Output is correct
86 Correct 198 ms 26836 KB Output is correct
87 Correct 5 ms 384 KB Output is correct