Submission #485563

# Submission time Handle Problem Language Result Execution time Memory
485563 2021-11-08T10:12:02 Z dxz05 Strange Device (APIO19_strange_device) C++14
100 / 100
442 ms 53208 KB
#pragma GCC optimize("Ofast,O2,O3,unroll-loops")
#pragma GCC target("avx2")

#include <bits/stdc++.h>

using namespace std;

void debug_out() { cerr << endl; }

template<typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
    cerr << "[" << H << "]";
    debug_out(T...);
}

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

#define SZ(s) ((int)s.size())
#define all(x) (x).begin(), (x).end()
#define lla(x) (x).rbegin(), (x).rend()

clock_t startTime;

double getCurrentTime() {
    return (double) (clock() - startTime) / CLOCKS_PER_SEC;
}

#define MP make_pair

typedef long long ll;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
const double eps = 0.000001;
const int MOD = 1e9 + 7;
const int INF = 1000000101;
const long long LLINF = 1223372000000000555;
const int N = 1e6 + 3e2;
const int M = 2222;

void solve(int TC) {
    int n; ll a, b;
    cin >> n >> a >> b;

    ll x;
    if (double(a) / __gcd(a, b + 1) * b > 3e18){
        x = 4e18;
    } else x = a / __gcd(a, b + 1) * b;

    vector<pair<ll, ll>> segments;
    for (int i = 1; i <= n; i++){
        ll l, r;
        cin >> l >> r;

        if (r - l + 1 >= x){
            cout << x << endl;
            return;
        }

        l %= x, r %= x;

        if (l > r){
            segments.emplace_back(l, x - 1);
            segments.emplace_back(0, r);
        } else segments.emplace_back(l, r);

    }

    sort(all(segments));

    ll ans = 0;
    n = segments.size();
    for (int i = 0; i < n;){
        ll l = segments[i].first, r = segments[i].second;
        int j = i;
        while (j < n && segments[j].first <= r){
            r = max(r, segments[j].second);
            j++;
        }

        ans += r - l + 1;

        i = j;
    }

    cout << ans;

}

int main() {
    startTime = clock();
    ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);

#ifdef dddxxz
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    int TC = 1;
//    cin >> TC;

    for (int test = 1; test <= TC; test++) {
        //debug(test);
        //cout << "Case #" << test << ": ";
        solve(test);
    }

    cerr << endl << "Time: " << int(getCurrentTime() * 1000) << " ms" << endl;

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 4 ms 1056 KB Output is correct
3 Correct 4 ms 976 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 1 ms 316 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 7 ms 976 KB Output is correct
17 Correct 45 ms 3520 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 312 KB Output is correct
2 Correct 0 ms 312 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 272 ms 17908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 359 ms 16916 KB Output is correct
3 Correct 356 ms 18028 KB Output is correct
4 Correct 356 ms 17812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 359 ms 16916 KB Output is correct
3 Correct 356 ms 18028 KB Output is correct
4 Correct 356 ms 17812 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 362 ms 17912 KB Output is correct
7 Correct 363 ms 17928 KB Output is correct
8 Correct 354 ms 17828 KB Output is correct
9 Correct 407 ms 16932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 359 ms 16916 KB Output is correct
3 Correct 356 ms 18028 KB Output is correct
4 Correct 356 ms 17812 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 40 ms 3460 KB Output is correct
7 Correct 38 ms 3480 KB Output is correct
8 Correct 37 ms 3468 KB Output is correct
9 Correct 37 ms 3472 KB Output is correct
10 Correct 45 ms 3520 KB Output is correct
11 Correct 37 ms 3520 KB Output is correct
12 Correct 35 ms 3468 KB Output is correct
13 Correct 38 ms 3500 KB Output is correct
14 Correct 37 ms 3520 KB Output is correct
15 Correct 41 ms 3460 KB Output is correct
16 Correct 51 ms 3520 KB Output is correct
17 Correct 41 ms 3568 KB Output is correct
18 Correct 361 ms 17872 KB Output is correct
19 Correct 353 ms 17880 KB Output is correct
20 Correct 413 ms 17828 KB Output is correct
21 Correct 40 ms 3496 KB Output is correct
22 Correct 38 ms 3544 KB Output is correct
23 Correct 121 ms 9676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 40 ms 3468 KB Output is correct
3 Correct 42 ms 3504 KB Output is correct
4 Correct 420 ms 17920 KB Output is correct
5 Correct 38 ms 3508 KB Output is correct
6 Correct 39 ms 3528 KB Output is correct
7 Correct 42 ms 3520 KB Output is correct
8 Correct 40 ms 3456 KB Output is correct
9 Correct 38 ms 3420 KB Output is correct
10 Correct 41 ms 3436 KB Output is correct
11 Correct 38 ms 3360 KB Output is correct
12 Correct 35 ms 3352 KB Output is correct
13 Correct 39 ms 3304 KB Output is correct
14 Correct 400 ms 17736 KB Output is correct
15 Correct 38 ms 3244 KB Output is correct
16 Correct 351 ms 16872 KB Output is correct
17 Correct 364 ms 16888 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 4 ms 1056 KB Output is correct
3 Correct 4 ms 976 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 1 ms 316 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 7 ms 976 KB Output is correct
17 Correct 45 ms 3520 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 0 ms 312 KB Output is correct
20 Correct 0 ms 312 KB Output is correct
21 Correct 0 ms 204 KB Output is correct
22 Correct 0 ms 204 KB Output is correct
23 Correct 0 ms 316 KB Output is correct
24 Correct 0 ms 204 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 272 ms 17908 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 359 ms 16916 KB Output is correct
31 Correct 356 ms 18028 KB Output is correct
32 Correct 356 ms 17812 KB Output is correct
33 Correct 0 ms 204 KB Output is correct
34 Correct 362 ms 17912 KB Output is correct
35 Correct 363 ms 17928 KB Output is correct
36 Correct 354 ms 17828 KB Output is correct
37 Correct 407 ms 16932 KB Output is correct
38 Correct 0 ms 204 KB Output is correct
39 Correct 40 ms 3460 KB Output is correct
40 Correct 38 ms 3480 KB Output is correct
41 Correct 37 ms 3468 KB Output is correct
42 Correct 37 ms 3472 KB Output is correct
43 Correct 45 ms 3520 KB Output is correct
44 Correct 37 ms 3520 KB Output is correct
45 Correct 35 ms 3468 KB Output is correct
46 Correct 38 ms 3500 KB Output is correct
47 Correct 37 ms 3520 KB Output is correct
48 Correct 41 ms 3460 KB Output is correct
49 Correct 51 ms 3520 KB Output is correct
50 Correct 41 ms 3568 KB Output is correct
51 Correct 361 ms 17872 KB Output is correct
52 Correct 353 ms 17880 KB Output is correct
53 Correct 413 ms 17828 KB Output is correct
54 Correct 40 ms 3496 KB Output is correct
55 Correct 38 ms 3544 KB Output is correct
56 Correct 121 ms 9676 KB Output is correct
57 Correct 1 ms 204 KB Output is correct
58 Correct 40 ms 3468 KB Output is correct
59 Correct 42 ms 3504 KB Output is correct
60 Correct 420 ms 17920 KB Output is correct
61 Correct 38 ms 3508 KB Output is correct
62 Correct 39 ms 3528 KB Output is correct
63 Correct 42 ms 3520 KB Output is correct
64 Correct 40 ms 3456 KB Output is correct
65 Correct 38 ms 3420 KB Output is correct
66 Correct 41 ms 3436 KB Output is correct
67 Correct 38 ms 3360 KB Output is correct
68 Correct 35 ms 3352 KB Output is correct
69 Correct 39 ms 3304 KB Output is correct
70 Correct 400 ms 17736 KB Output is correct
71 Correct 38 ms 3244 KB Output is correct
72 Correct 351 ms 16872 KB Output is correct
73 Correct 364 ms 16888 KB Output is correct
74 Correct 0 ms 204 KB Output is correct
75 Correct 0 ms 204 KB Output is correct
76 Correct 0 ms 204 KB Output is correct
77 Correct 0 ms 324 KB Output is correct
78 Correct 0 ms 204 KB Output is correct
79 Correct 4 ms 1060 KB Output is correct
80 Correct 442 ms 53176 KB Output is correct
81 Correct 432 ms 53136 KB Output is correct
82 Correct 414 ms 53208 KB Output is correct
83 Correct 411 ms 53136 KB Output is correct
84 Correct 418 ms 53160 KB Output is correct
85 Correct 411 ms 53132 KB Output is correct
86 Correct 116 ms 18388 KB Output is correct
87 Correct 1 ms 204 KB Output is correct