Submission #125401

# Submission time Handle Problem Language Result Execution time Memory
125401 2019-07-05T08:13:48 Z mlyean00 Strange Device (APIO19_strange_device) C++17
100 / 100
683 ms 17688 KB
#ifdef DEBUG
#define trace(x) cerr << "DEBUG: " << (#x) << " = " << (x) << endl
#else
#pragma GCC optimize("Ofast")
#define trace(x)
#endif

#include <bits/stdc++.h>

#define endl '\n'

using namespace std;
using ll = long long;

const ll MAX = 1'000'000'000'000'000'001LL;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    int n;
    ll A, B;
    cin >> n >> A >> B;

    ll g = gcd(A, B + 1);
    A /= g;

    ll mod;
    // Check overflow
    if (__lg(A) + __lg(B) > 60)
        mod = MAX;
    else
        mod = min(A * B, MAX);

    // The intervals to union
    vector<pair<ll, ll>> v;

    for (int i = 0; i < n; ++i) {
        ll l, r;
        cin >> l >> r;

        // Shift left endpoint such that 0 <= l < mod
        ll k = (l / mod) * mod;
        l -= k;
        r -= k;

        if (r - l + 1 >= mod) {
            v.emplace_back(0, mod - 1);
        } else if (r < mod) {
            v.emplace_back(l, r);
        } else {
            v.emplace_back(l, mod - 1);
            v.emplace_back(0, r - mod);
        }
    }

    sort(v.begin(), v.end());

    // Merge intervals
    vector<pair<ll, ll>> v_merged;
    v_merged.push_back(v.front());

    for (int i = 1; i < v.size(); ++i) {
        auto& last = v_merged.back();
        if (last.second + 1 < v[i].first) {
            v_merged.push_back(v[i]);
        } else if (v[i].second > last.second) {
            last.second = v[i].second;
        }
    }

    ll ans = 0;
    for (auto [l, r] : v_merged) {
        ans += r - l + 1;
    }

    cout << ans << endl;

    return 0;
}

Compilation message

strange_device.cpp: In function 'int main()':
strange_device.cpp:63:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 1; i < v.size(); ++i) {
                     ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 252 KB Output is correct
2 Correct 8 ms 1016 KB Output is correct
3 Correct 8 ms 1144 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 348 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 3 ms 424 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 3 ms 436 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 9 ms 1016 KB Output is correct
17 Correct 67 ms 5100 KB Output is correct
18 Correct 1 ms 380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 3 ms 432 KB Output is correct
3 Correct 3 ms 428 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 436 ms 17620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 593 ms 17584 KB Output is correct
3 Correct 583 ms 17272 KB Output is correct
4 Correct 590 ms 17244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 593 ms 17584 KB Output is correct
3 Correct 583 ms 17272 KB Output is correct
4 Correct 590 ms 17244 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 598 ms 17296 KB Output is correct
7 Correct 592 ms 17276 KB Output is correct
8 Correct 591 ms 17448 KB Output is correct
9 Correct 649 ms 17520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 593 ms 17584 KB Output is correct
3 Correct 583 ms 17272 KB Output is correct
4 Correct 590 ms 17244 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 64 ms 4084 KB Output is correct
7 Correct 64 ms 3312 KB Output is correct
8 Correct 61 ms 3340 KB Output is correct
9 Correct 62 ms 3312 KB Output is correct
10 Correct 61 ms 3312 KB Output is correct
11 Correct 62 ms 3260 KB Output is correct
12 Correct 61 ms 3312 KB Output is correct
13 Correct 66 ms 3248 KB Output is correct
14 Correct 65 ms 3308 KB Output is correct
15 Correct 68 ms 3392 KB Output is correct
16 Correct 67 ms 3376 KB Output is correct
17 Correct 65 ms 3388 KB Output is correct
18 Correct 618 ms 17552 KB Output is correct
19 Correct 587 ms 17500 KB Output is correct
20 Correct 656 ms 17572 KB Output is correct
21 Correct 68 ms 3564 KB Output is correct
22 Correct 59 ms 3564 KB Output is correct
23 Correct 188 ms 10692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 67 ms 4840 KB Output is correct
3 Correct 65 ms 3440 KB Output is correct
4 Correct 678 ms 17524 KB Output is correct
5 Correct 65 ms 3400 KB Output is correct
6 Correct 66 ms 3568 KB Output is correct
7 Correct 67 ms 3440 KB Output is correct
8 Correct 67 ms 3440 KB Output is correct
9 Correct 76 ms 3396 KB Output is correct
10 Correct 68 ms 3372 KB Output is correct
11 Correct 68 ms 3352 KB Output is correct
12 Correct 61 ms 3436 KB Output is correct
13 Correct 65 ms 3440 KB Output is correct
14 Correct 640 ms 17496 KB Output is correct
15 Correct 64 ms 3564 KB Output is correct
16 Correct 591 ms 17604 KB Output is correct
17 Correct 583 ms 17520 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 252 KB Output is correct
2 Correct 8 ms 1016 KB Output is correct
3 Correct 8 ms 1144 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 348 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 3 ms 424 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 3 ms 436 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 9 ms 1016 KB Output is correct
17 Correct 67 ms 5100 KB Output is correct
18 Correct 1 ms 380 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 256 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 2 ms 376 KB Output is correct
23 Correct 2 ms 292 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 3 ms 432 KB Output is correct
26 Correct 3 ms 428 KB Output is correct
27 Correct 3 ms 376 KB Output is correct
28 Correct 436 ms 17620 KB Output is correct
29 Correct 2 ms 376 KB Output is correct
30 Correct 593 ms 17584 KB Output is correct
31 Correct 583 ms 17272 KB Output is correct
32 Correct 590 ms 17244 KB Output is correct
33 Correct 2 ms 376 KB Output is correct
34 Correct 598 ms 17296 KB Output is correct
35 Correct 592 ms 17276 KB Output is correct
36 Correct 591 ms 17448 KB Output is correct
37 Correct 649 ms 17520 KB Output is correct
38 Correct 2 ms 376 KB Output is correct
39 Correct 64 ms 4084 KB Output is correct
40 Correct 64 ms 3312 KB Output is correct
41 Correct 61 ms 3340 KB Output is correct
42 Correct 62 ms 3312 KB Output is correct
43 Correct 61 ms 3312 KB Output is correct
44 Correct 62 ms 3260 KB Output is correct
45 Correct 61 ms 3312 KB Output is correct
46 Correct 66 ms 3248 KB Output is correct
47 Correct 65 ms 3308 KB Output is correct
48 Correct 68 ms 3392 KB Output is correct
49 Correct 67 ms 3376 KB Output is correct
50 Correct 65 ms 3388 KB Output is correct
51 Correct 618 ms 17552 KB Output is correct
52 Correct 587 ms 17500 KB Output is correct
53 Correct 656 ms 17572 KB Output is correct
54 Correct 68 ms 3564 KB Output is correct
55 Correct 59 ms 3564 KB Output is correct
56 Correct 188 ms 10692 KB Output is correct
57 Correct 2 ms 376 KB Output is correct
58 Correct 67 ms 4840 KB Output is correct
59 Correct 65 ms 3440 KB Output is correct
60 Correct 678 ms 17524 KB Output is correct
61 Correct 65 ms 3400 KB Output is correct
62 Correct 66 ms 3568 KB Output is correct
63 Correct 67 ms 3440 KB Output is correct
64 Correct 67 ms 3440 KB Output is correct
65 Correct 76 ms 3396 KB Output is correct
66 Correct 68 ms 3372 KB Output is correct
67 Correct 68 ms 3352 KB Output is correct
68 Correct 61 ms 3436 KB Output is correct
69 Correct 65 ms 3440 KB Output is correct
70 Correct 640 ms 17496 KB Output is correct
71 Correct 64 ms 3564 KB Output is correct
72 Correct 591 ms 17604 KB Output is correct
73 Correct 583 ms 17520 KB Output is correct
74 Correct 2 ms 376 KB Output is correct
75 Correct 2 ms 376 KB Output is correct
76 Correct 2 ms 376 KB Output is correct
77 Correct 2 ms 348 KB Output is correct
78 Correct 2 ms 404 KB Output is correct
79 Correct 9 ms 1144 KB Output is correct
80 Correct 666 ms 17688 KB Output is correct
81 Correct 683 ms 17616 KB Output is correct
82 Correct 645 ms 17512 KB Output is correct
83 Correct 655 ms 17580 KB Output is correct
84 Correct 648 ms 17576 KB Output is correct
85 Correct 648 ms 17544 KB Output is correct
86 Correct 188 ms 11168 KB Output is correct
87 Correct 3 ms 376 KB Output is correct