Submission #268709

# Submission time Handle Problem Language Result Execution time Memory
268709 2020-08-16T19:36:50 Z evpipis Strange Device (APIO19_strange_device) C++11
100 / 100
621 ms 77868 KB
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair<ll, ll> pll;

const ll mx = 1e18;
vector<pll> vec, help;

ll gcd(ll a, ll b){
    if (b == 0)
        return a;
    return gcd(b, a%b);
}

ll find_fir(ll a, ll b){
    ll ans = a/gcd(a, b+1);
    if (ans <= mx/b)
        ans = ans*b;
    else
        ans = mx+1;
    return ans;
}

ll solve(ll m){
    for (int i = 0; i < vec.size(); i++){
        pll cur = vec[i];
        if (cur.se-cur.fi+1 >= m)
            return m;

        cur.fi %= m;
        cur.se %= m;

        if (cur.fi <= cur.se)
            help.pb(cur);
        else
            help.pb(mp(0, cur.se)), help.pb(mp(cur.fi, m-1));
    }

    vec.clear();
    sort(help.begin(), help.end());
    for (int i = 0; i < help.size(); i++){
        pll cur = help[i];
        while (!vec.empty() && cur.fi == vec.back().fi)
            vec.pop_back();
        if (vec.empty() || cur.se > vec.back().se)
            vec.pb(cur);
    }

    ll ans = 0;
    for (int i = 0; i < vec.size(); i++){
        pll cur = vec[i];
        if (i+1 < vec.size() && vec[i+1].fi-1 < cur.se)
            ans += vec[i+1].fi-cur.fi;
        else
            ans += cur.se-cur.fi+1;
    }

    return ans;
}

int main(){
    int n;
    ll a, b;
    scanf("%d %lld %lld", &n, &a, &b);

    ll m = find_fir(a, b);

    for (int i = 0; i < n; i++){
        ll l, r;
        scanf("%lld %lld", &l, &r);
        vec.pb(mp(l, r));
    }

    printf("%lld\n", solve(m));
    return 0;
}

Compilation message

strange_device.cpp: In function 'll solve(ll)':
strange_device.cpp:30:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   30 |     for (int i = 0; i < vec.size(); i++){
      |                     ~~^~~~~~~~~~~~
strange_device.cpp:46:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   46 |     for (int i = 0; i < help.size(); i++){
      |                     ~~^~~~~~~~~~~~~
strange_device.cpp:55:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   55 |     for (int i = 0; i < vec.size(); i++){
      |                     ~~^~~~~~~~~~~~
strange_device.cpp:57:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |         if (i+1 < vec.size() && vec[i+1].fi-1 < cur.se)
      |             ~~~~^~~~~~~~~~~~
strange_device.cpp: In function 'int main()':
strange_device.cpp:69:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   69 |     scanf("%d %lld %lld", &n, &a, &b);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
strange_device.cpp:75:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   75 |         scanf("%lld %lld", &l, &r);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 6 ms 1276 KB Output is correct
3 Correct 7 ms 1276 KB Output is correct
4 Correct 1 ms 512 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 1 ms 256 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 0 ms 256 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 0 ms 256 KB Output is correct
11 Correct 1 ms 256 KB Output is correct
12 Correct 0 ms 256 KB Output is correct
13 Correct 0 ms 256 KB Output is correct
14 Correct 0 ms 256 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 6 ms 1276 KB Output is correct
17 Correct 63 ms 8680 KB Output is correct
18 Correct 1 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 446 ms 46140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 557 ms 41188 KB Output is correct
3 Correct 553 ms 43452 KB Output is correct
4 Correct 560 ms 43728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 557 ms 41188 KB Output is correct
3 Correct 553 ms 43452 KB Output is correct
4 Correct 560 ms 43728 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 559 ms 43708 KB Output is correct
7 Correct 562 ms 43580 KB Output is correct
8 Correct 546 ms 43580 KB Output is correct
9 Correct 616 ms 43580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 557 ms 41188 KB Output is correct
3 Correct 553 ms 43452 KB Output is correct
4 Correct 560 ms 43728 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 60 ms 8708 KB Output is correct
7 Correct 60 ms 8676 KB Output is correct
8 Correct 55 ms 8680 KB Output is correct
9 Correct 56 ms 8624 KB Output is correct
10 Correct 60 ms 8676 KB Output is correct
11 Correct 59 ms 8676 KB Output is correct
12 Correct 56 ms 8676 KB Output is correct
13 Correct 60 ms 8680 KB Output is correct
14 Correct 57 ms 8680 KB Output is correct
15 Correct 60 ms 8676 KB Output is correct
16 Correct 60 ms 8680 KB Output is correct
17 Correct 58 ms 8832 KB Output is correct
18 Correct 572 ms 54588 KB Output is correct
19 Correct 540 ms 54028 KB Output is correct
20 Correct 612 ms 54716 KB Output is correct
21 Correct 59 ms 8676 KB Output is correct
22 Correct 55 ms 8676 KB Output is correct
23 Correct 213 ms 30564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 62 ms 8680 KB Output is correct
3 Correct 58 ms 8676 KB Output is correct
4 Correct 614 ms 43612 KB Output is correct
5 Correct 56 ms 8680 KB Output is correct
6 Correct 60 ms 8676 KB Output is correct
7 Correct 58 ms 8676 KB Output is correct
8 Correct 62 ms 8684 KB Output is correct
9 Correct 60 ms 8676 KB Output is correct
10 Correct 60 ms 8724 KB Output is correct
11 Correct 60 ms 8676 KB Output is correct
12 Correct 52 ms 8676 KB Output is correct
13 Correct 59 ms 8676 KB Output is correct
14 Correct 621 ms 43656 KB Output is correct
15 Correct 72 ms 8676 KB Output is correct
16 Correct 591 ms 47540 KB Output is correct
17 Correct 605 ms 56252 KB Output is correct
18 Correct 0 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 6 ms 1276 KB Output is correct
3 Correct 7 ms 1276 KB Output is correct
4 Correct 1 ms 512 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 1 ms 256 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 0 ms 256 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 0 ms 256 KB Output is correct
11 Correct 1 ms 256 KB Output is correct
12 Correct 0 ms 256 KB Output is correct
13 Correct 0 ms 256 KB Output is correct
14 Correct 0 ms 256 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 6 ms 1276 KB Output is correct
17 Correct 63 ms 8680 KB Output is correct
18 Correct 1 ms 256 KB Output is correct
19 Correct 0 ms 256 KB Output is correct
20 Correct 1 ms 256 KB Output is correct
21 Correct 1 ms 256 KB Output is correct
22 Correct 0 ms 256 KB Output is correct
23 Correct 1 ms 256 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 446 ms 46140 KB Output is correct
29 Correct 1 ms 256 KB Output is correct
30 Correct 557 ms 41188 KB Output is correct
31 Correct 553 ms 43452 KB Output is correct
32 Correct 560 ms 43728 KB Output is correct
33 Correct 0 ms 256 KB Output is correct
34 Correct 559 ms 43708 KB Output is correct
35 Correct 562 ms 43580 KB Output is correct
36 Correct 546 ms 43580 KB Output is correct
37 Correct 616 ms 43580 KB Output is correct
38 Correct 0 ms 256 KB Output is correct
39 Correct 60 ms 8708 KB Output is correct
40 Correct 60 ms 8676 KB Output is correct
41 Correct 55 ms 8680 KB Output is correct
42 Correct 56 ms 8624 KB Output is correct
43 Correct 60 ms 8676 KB Output is correct
44 Correct 59 ms 8676 KB Output is correct
45 Correct 56 ms 8676 KB Output is correct
46 Correct 60 ms 8680 KB Output is correct
47 Correct 57 ms 8680 KB Output is correct
48 Correct 60 ms 8676 KB Output is correct
49 Correct 60 ms 8680 KB Output is correct
50 Correct 58 ms 8832 KB Output is correct
51 Correct 572 ms 54588 KB Output is correct
52 Correct 540 ms 54028 KB Output is correct
53 Correct 612 ms 54716 KB Output is correct
54 Correct 59 ms 8676 KB Output is correct
55 Correct 55 ms 8676 KB Output is correct
56 Correct 213 ms 30564 KB Output is correct
57 Correct 0 ms 256 KB Output is correct
58 Correct 62 ms 8680 KB Output is correct
59 Correct 58 ms 8676 KB Output is correct
60 Correct 614 ms 43612 KB Output is correct
61 Correct 56 ms 8680 KB Output is correct
62 Correct 60 ms 8676 KB Output is correct
63 Correct 58 ms 8676 KB Output is correct
64 Correct 62 ms 8684 KB Output is correct
65 Correct 60 ms 8676 KB Output is correct
66 Correct 60 ms 8724 KB Output is correct
67 Correct 60 ms 8676 KB Output is correct
68 Correct 52 ms 8676 KB Output is correct
69 Correct 59 ms 8676 KB Output is correct
70 Correct 621 ms 43656 KB Output is correct
71 Correct 72 ms 8676 KB Output is correct
72 Correct 591 ms 47540 KB Output is correct
73 Correct 605 ms 56252 KB Output is correct
74 Correct 0 ms 256 KB Output is correct
75 Correct 0 ms 256 KB Output is correct
76 Correct 0 ms 256 KB Output is correct
77 Correct 1 ms 256 KB Output is correct
78 Correct 1 ms 256 KB Output is correct
79 Correct 6 ms 1276 KB Output is correct
80 Correct 621 ms 57540 KB Output is correct
81 Correct 611 ms 54628 KB Output is correct
82 Correct 580 ms 56904 KB Output is correct
83 Correct 576 ms 59756 KB Output is correct
84 Correct 576 ms 66748 KB Output is correct
85 Correct 609 ms 77868 KB Output is correct
86 Correct 212 ms 30416 KB Output is correct
87 Correct 0 ms 256 KB Output is correct