Submission #145362

# Submission time Handle Problem Language Result Execution time Memory
145362 2019-08-19T17:29:47 Z dolphingarlic Strange Device (APIO19_strange_device) C++14
100 / 100
727 ms 41496 KB
#include <bits/stdc++.h>
#pragma GCC Optimize("O3")
#define FOR(i, x, y) for (int i = x; i < y; i++)
#define MOD 1000000007
typedef long long ll;
using namespace std;

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

    ll n, a, b;
    cin >> n >> a >> b;

    vector<pair<ll, ll>> segs;

    ll s = 0;

    FOR(i, 1, n + 1) {
        ll l, r;
        cin >> l >> r;

        segs.push_back({l, r});

        s += r - l + 1;
    }

    ll l = segs[0].first;
    ll r = segs[0].second;

    ll k = a / __gcd(a, b + 1);

    if (log(k) + log(b) >= 18 * log(10)) {
        cout << s << '\n';
    } else {
        k *= b;

        vector<pair<ll, ll>> intervals;

        for (auto seg : segs) {
            if (seg.second - seg.first + 1 >= k) {
                cout << k << endl;
                return 0;
            }

            if (seg.first % k > seg.second % k) {
                intervals.push_back({0, seg.second % k});
                intervals.push_back({seg.first % k, k - 1});
            }

            else {
                intervals.push_back({seg.first % k, seg.second % k});
            }
        }

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

        ll cur_start = intervals[0].first;
        ll cur_end = intervals[0].second;

        ll ans = 0;

        FOR(i, 1, intervals.size()) {
            if (intervals[i].first > cur_end + 1) {
                ans += cur_end - cur_start + 1;
                cur_start = intervals[i].first;
                cur_end = intervals[i].second;
            }

            else {
                cur_end = max(cur_end, intervals[i].second);
            }
        }

        ans += cur_end - cur_start + 1;

        cout << ans << '\n';
    }

    return 0;
}

Compilation message

strange_device.cpp:2:0: warning: ignoring #pragma GCC Optimize [-Wunknown-pragmas]
 #pragma GCC Optimize("O3")
 
strange_device.cpp: In function 'int main()':
strange_device.cpp:3:40: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define FOR(i, x, y) for (int i = x; i < y; i++)
strange_device.cpp:63:13:
         FOR(i, 1, intervals.size()) {
             ~~~~~~~~~~~~~~~~~~~~~~      
strange_device.cpp:63:9: note: in expansion of macro 'FOR'
         FOR(i, 1, intervals.size()) {
         ^~~
strange_device.cpp:28:8: warning: unused variable 'l' [-Wunused-variable]
     ll l = segs[0].first;
        ^
strange_device.cpp:29:8: warning: unused variable 'r' [-Wunused-variable]
     ll r = segs[0].second;
        ^
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 9 ms 1016 KB Output is correct
3 Correct 9 ms 1016 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 3 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 276 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 4 ms 988 KB Output is correct
17 Correct 71 ms 5352 KB Output is correct
18 Correct 2 ms 376 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 380 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 3 ms 376 KB Output is correct
3 Correct 3 ms 504 KB Output is correct
4 Correct 3 ms 476 KB Output is correct
5 Correct 491 ms 41224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 660 ms 41496 KB Output is correct
3 Correct 640 ms 40908 KB Output is correct
4 Correct 644 ms 40732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 660 ms 41496 KB Output is correct
3 Correct 640 ms 40908 KB Output is correct
4 Correct 644 ms 40732 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 636 ms 40680 KB Output is correct
7 Correct 631 ms 40800 KB Output is correct
8 Correct 630 ms 40760 KB Output is correct
9 Correct 685 ms 40836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 660 ms 41496 KB Output is correct
3 Correct 640 ms 40908 KB Output is correct
4 Correct 644 ms 40732 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 67 ms 5080 KB Output is correct
7 Correct 67 ms 5336 KB Output is correct
8 Correct 64 ms 5352 KB Output is correct
9 Correct 69 ms 5368 KB Output is correct
10 Correct 67 ms 5324 KB Output is correct
11 Correct 68 ms 5460 KB Output is correct
12 Correct 66 ms 5372 KB Output is correct
13 Correct 72 ms 5448 KB Output is correct
14 Correct 105 ms 5352 KB Output is correct
15 Correct 72 ms 5352 KB Output is correct
16 Correct 70 ms 5352 KB Output is correct
17 Correct 66 ms 5192 KB Output is correct
18 Correct 636 ms 40784 KB Output is correct
19 Correct 630 ms 40852 KB Output is correct
20 Correct 694 ms 40748 KB Output is correct
21 Correct 69 ms 5224 KB Output is correct
22 Correct 63 ms 5224 KB Output is correct
23 Correct 205 ms 18244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 69 ms 5096 KB Output is correct
3 Correct 70 ms 5096 KB Output is correct
4 Correct 727 ms 40720 KB Output is correct
5 Correct 70 ms 5172 KB Output is correct
6 Correct 69 ms 5412 KB Output is correct
7 Correct 69 ms 5352 KB Output is correct
8 Correct 71 ms 5352 KB Output is correct
9 Correct 70 ms 5364 KB Output is correct
10 Correct 70 ms 5480 KB Output is correct
11 Correct 76 ms 5352 KB Output is correct
12 Correct 64 ms 5328 KB Output is correct
13 Correct 69 ms 5480 KB Output is correct
14 Correct 680 ms 40972 KB Output is correct
15 Correct 69 ms 5648 KB Output is correct
16 Correct 629 ms 40960 KB Output is correct
17 Correct 619 ms 40740 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 9 ms 1016 KB Output is correct
3 Correct 9 ms 1016 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 3 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 276 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 4 ms 988 KB Output is correct
17 Correct 71 ms 5352 KB Output is correct
18 Correct 2 ms 376 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 380 KB Output is correct
23 Correct 2 ms 376 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 3 ms 376 KB Output is correct
26 Correct 3 ms 504 KB Output is correct
27 Correct 3 ms 476 KB Output is correct
28 Correct 491 ms 41224 KB Output is correct
29 Correct 2 ms 376 KB Output is correct
30 Correct 660 ms 41496 KB Output is correct
31 Correct 640 ms 40908 KB Output is correct
32 Correct 644 ms 40732 KB Output is correct
33 Correct 2 ms 376 KB Output is correct
34 Correct 636 ms 40680 KB Output is correct
35 Correct 631 ms 40800 KB Output is correct
36 Correct 630 ms 40760 KB Output is correct
37 Correct 685 ms 40836 KB Output is correct
38 Correct 2 ms 376 KB Output is correct
39 Correct 67 ms 5080 KB Output is correct
40 Correct 67 ms 5336 KB Output is correct
41 Correct 64 ms 5352 KB Output is correct
42 Correct 69 ms 5368 KB Output is correct
43 Correct 67 ms 5324 KB Output is correct
44 Correct 68 ms 5460 KB Output is correct
45 Correct 66 ms 5372 KB Output is correct
46 Correct 72 ms 5448 KB Output is correct
47 Correct 105 ms 5352 KB Output is correct
48 Correct 72 ms 5352 KB Output is correct
49 Correct 70 ms 5352 KB Output is correct
50 Correct 66 ms 5192 KB Output is correct
51 Correct 636 ms 40784 KB Output is correct
52 Correct 630 ms 40852 KB Output is correct
53 Correct 694 ms 40748 KB Output is correct
54 Correct 69 ms 5224 KB Output is correct
55 Correct 63 ms 5224 KB Output is correct
56 Correct 205 ms 18244 KB Output is correct
57 Correct 2 ms 256 KB Output is correct
58 Correct 69 ms 5096 KB Output is correct
59 Correct 70 ms 5096 KB Output is correct
60 Correct 727 ms 40720 KB Output is correct
61 Correct 70 ms 5172 KB Output is correct
62 Correct 69 ms 5412 KB Output is correct
63 Correct 69 ms 5352 KB Output is correct
64 Correct 71 ms 5352 KB Output is correct
65 Correct 70 ms 5364 KB Output is correct
66 Correct 70 ms 5480 KB Output is correct
67 Correct 76 ms 5352 KB Output is correct
68 Correct 64 ms 5328 KB Output is correct
69 Correct 69 ms 5480 KB Output is correct
70 Correct 680 ms 40972 KB Output is correct
71 Correct 69 ms 5648 KB Output is correct
72 Correct 629 ms 40960 KB Output is correct
73 Correct 619 ms 40740 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 380 KB Output is correct
77 Correct 2 ms 376 KB Output is correct
78 Correct 2 ms 380 KB Output is correct
79 Correct 8 ms 1144 KB Output is correct
80 Correct 713 ms 40964 KB Output is correct
81 Correct 707 ms 40672 KB Output is correct
82 Correct 684 ms 40856 KB Output is correct
83 Correct 687 ms 40708 KB Output is correct
84 Correct 695 ms 41092 KB Output is correct
85 Correct 679 ms 40956 KB Output is correct
86 Correct 202 ms 18264 KB Output is correct
87 Correct 2 ms 376 KB Output is correct