Submission #245783

# Submission time Handle Problem Language Result Execution time Memory
245783 2020-07-07T11:57:23 Z kenken714 Strange Device (APIO19_strange_device) C++14
100 / 100
2312 ms 61168 KB
#include <bits/stdc++.h>
using namespace std;
 
#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define FORR(i, m, n) for (ll i = m; i >= n; i--)
#define REPO(i, n) for (ll i = 1; i <= n; i++)
#define ll long long
#define INF (ll)1 << 60
#define MINF (-1 * INF)
#define ALL(n) n.begin(), n.end()
#define MOD 1000000007
#define P pair<ll, ll>

ll n, a, b, r;
vector<P> s;
bool ok = false;
ll gcd(ll a, ll b){
    return (b == 0 ? a : gcd(b, a % b));
}

ll f(ll a){
    ll res = a % r;
    if (res == 0) return r;
    return res;
}
void solve(ll a, ll b){
    ll c = (a + r - 1) / r * r;
    if(c > b){
        s.push_back(P(f(a), f(b)));
        return;
    }
    if(c - a + 1 == r){
        ok = true;
        return;
    }
    s.push_back(P(f(a), f(c)));
    if (c == b) return;
    solve(c + 1, b);
}
int main() {
    cin >> n >> a >> b;
    r = a / gcd(a, b + 1);
    if((ll)1e18 / r < b){
        ll ans = 0;
        REP(i, n){
            ll a, b;
            cin >> a >> b;
            ans += b - a + 1;
        }
        cout << ans << endl;
        return 0;
    }
    r *= b;
    REP(i, n){
        ll a, b;
        cin >> a >> b;
        solve(a, b);
    }
    if (ok){
       cout << r << endl;
       return 0;
    }
    ll ans = 0;
    sort(ALL(s));
    vector<P> t;
    t.push_back(P(MINF, MINF));
    REP(i, s.size()){
        //cout << s[i].first << " " << s[i].second << endl;
        if(t.back().second < s[i].first){
            t.push_back(s[i]);
            ans += s[i].second - s[i].first + 1;
        }
        else if(t.back().second < s[i].second){
            ans += s[i].second - t.back().second;
            t.back().second = s[i].second;
        }
    }
    cout << ans << endl;
}

Compilation message

strange_device.cpp: In function 'int main()':
strange_device.cpp:4:36: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define REP(i, n) for (ll i = 0; i < n; i++)
strange_device.cpp:69:9:
     REP(i, s.size()){
         ~~~~~~~~~~~                 
strange_device.cpp:69:5: note: in expansion of macro 'REP'
     REP(i, s.size()){
     ^~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 256 KB Output is correct
2 Correct 26 ms 1276 KB Output is correct
3 Correct 26 ms 1276 KB Output is correct
4 Correct 5 ms 256 KB Output is correct
5 Correct 4 ms 256 KB Output is correct
6 Correct 4 ms 256 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 256 KB Output is correct
9 Correct 5 ms 256 KB Output is correct
10 Correct 4 ms 256 KB Output is correct
11 Correct 5 ms 256 KB Output is correct
12 Correct 5 ms 376 KB Output is correct
13 Correct 6 ms 288 KB Output is correct
14 Correct 5 ms 256 KB Output is correct
15 Correct 5 ms 256 KB Output is correct
16 Correct 32 ms 1268 KB Output is correct
17 Correct 235 ms 8804 KB Output is correct
18 Correct 5 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 256 KB Output is correct
2 Correct 4 ms 256 KB Output is correct
3 Correct 5 ms 256 KB Output is correct
4 Correct 5 ms 128 KB Output is correct
5 Correct 4 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 256 KB Output is correct
2 Correct 7 ms 384 KB Output is correct
3 Correct 7 ms 384 KB Output is correct
4 Correct 6 ms 384 KB Output is correct
5 Correct 1583 ms 57020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 256 KB Output is correct
2 Correct 2160 ms 61128 KB Output is correct
3 Correct 2139 ms 46288 KB Output is correct
4 Correct 2137 ms 46300 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 256 KB Output is correct
2 Correct 2160 ms 61128 KB Output is correct
3 Correct 2139 ms 46288 KB Output is correct
4 Correct 2137 ms 46300 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 2127 ms 46488 KB Output is correct
7 Correct 2163 ms 46036 KB Output is correct
8 Correct 2116 ms 46116 KB Output is correct
9 Correct 2206 ms 46092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 256 KB Output is correct
2 Correct 2160 ms 61128 KB Output is correct
3 Correct 2139 ms 46288 KB Output is correct
4 Correct 2137 ms 46300 KB Output is correct
5 Correct 5 ms 256 KB Output is correct
6 Correct 218 ms 8780 KB Output is correct
7 Correct 221 ms 8804 KB Output is correct
8 Correct 216 ms 8676 KB Output is correct
9 Correct 225 ms 8740 KB Output is correct
10 Correct 219 ms 8676 KB Output is correct
11 Correct 215 ms 8676 KB Output is correct
12 Correct 219 ms 8684 KB Output is correct
13 Correct 217 ms 8676 KB Output is correct
14 Correct 217 ms 8676 KB Output is correct
15 Correct 220 ms 8676 KB Output is correct
16 Correct 226 ms 8804 KB Output is correct
17 Correct 221 ms 8676 KB Output is correct
18 Correct 2149 ms 46236 KB Output is correct
19 Correct 2103 ms 46288 KB Output is correct
20 Correct 2189 ms 46328 KB Output is correct
21 Correct 218 ms 8676 KB Output is correct
22 Correct 213 ms 8676 KB Output is correct
23 Correct 736 ms 26788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 235 ms 8676 KB Output is correct
3 Correct 224 ms 8696 KB Output is correct
4 Correct 2223 ms 58864 KB Output is correct
5 Correct 223 ms 8676 KB Output is correct
6 Correct 229 ms 8804 KB Output is correct
7 Correct 225 ms 8804 KB Output is correct
8 Correct 221 ms 8676 KB Output is correct
9 Correct 221 ms 8724 KB Output is correct
10 Correct 221 ms 8796 KB Output is correct
11 Correct 223 ms 8676 KB Output is correct
12 Correct 218 ms 8676 KB Output is correct
13 Correct 221 ms 8700 KB Output is correct
14 Correct 2183 ms 58792 KB Output is correct
15 Correct 226 ms 8676 KB Output is correct
16 Correct 2149 ms 46204 KB Output is correct
17 Correct 2130 ms 46388 KB Output is correct
18 Correct 5 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 256 KB Output is correct
2 Correct 26 ms 1276 KB Output is correct
3 Correct 26 ms 1276 KB Output is correct
4 Correct 5 ms 256 KB Output is correct
5 Correct 4 ms 256 KB Output is correct
6 Correct 4 ms 256 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 256 KB Output is correct
9 Correct 5 ms 256 KB Output is correct
10 Correct 4 ms 256 KB Output is correct
11 Correct 5 ms 256 KB Output is correct
12 Correct 5 ms 376 KB Output is correct
13 Correct 6 ms 288 KB Output is correct
14 Correct 5 ms 256 KB Output is correct
15 Correct 5 ms 256 KB Output is correct
16 Correct 32 ms 1268 KB Output is correct
17 Correct 235 ms 8804 KB Output is correct
18 Correct 5 ms 256 KB Output is correct
19 Correct 4 ms 256 KB Output is correct
20 Correct 4 ms 256 KB Output is correct
21 Correct 5 ms 256 KB Output is correct
22 Correct 5 ms 128 KB Output is correct
23 Correct 4 ms 256 KB Output is correct
24 Correct 5 ms 256 KB Output is correct
25 Correct 7 ms 384 KB Output is correct
26 Correct 7 ms 384 KB Output is correct
27 Correct 6 ms 384 KB Output is correct
28 Correct 1583 ms 57020 KB Output is correct
29 Correct 5 ms 256 KB Output is correct
30 Correct 2160 ms 61128 KB Output is correct
31 Correct 2139 ms 46288 KB Output is correct
32 Correct 2137 ms 46300 KB Output is correct
33 Correct 5 ms 384 KB Output is correct
34 Correct 2127 ms 46488 KB Output is correct
35 Correct 2163 ms 46036 KB Output is correct
36 Correct 2116 ms 46116 KB Output is correct
37 Correct 2206 ms 46092 KB Output is correct
38 Correct 5 ms 256 KB Output is correct
39 Correct 218 ms 8780 KB Output is correct
40 Correct 221 ms 8804 KB Output is correct
41 Correct 216 ms 8676 KB Output is correct
42 Correct 225 ms 8740 KB Output is correct
43 Correct 219 ms 8676 KB Output is correct
44 Correct 215 ms 8676 KB Output is correct
45 Correct 219 ms 8684 KB Output is correct
46 Correct 217 ms 8676 KB Output is correct
47 Correct 217 ms 8676 KB Output is correct
48 Correct 220 ms 8676 KB Output is correct
49 Correct 226 ms 8804 KB Output is correct
50 Correct 221 ms 8676 KB Output is correct
51 Correct 2149 ms 46236 KB Output is correct
52 Correct 2103 ms 46288 KB Output is correct
53 Correct 2189 ms 46328 KB Output is correct
54 Correct 218 ms 8676 KB Output is correct
55 Correct 213 ms 8676 KB Output is correct
56 Correct 736 ms 26788 KB Output is correct
57 Correct 5 ms 384 KB Output is correct
58 Correct 235 ms 8676 KB Output is correct
59 Correct 224 ms 8696 KB Output is correct
60 Correct 2223 ms 58864 KB Output is correct
61 Correct 223 ms 8676 KB Output is correct
62 Correct 229 ms 8804 KB Output is correct
63 Correct 225 ms 8804 KB Output is correct
64 Correct 221 ms 8676 KB Output is correct
65 Correct 221 ms 8724 KB Output is correct
66 Correct 221 ms 8796 KB Output is correct
67 Correct 223 ms 8676 KB Output is correct
68 Correct 218 ms 8676 KB Output is correct
69 Correct 221 ms 8700 KB Output is correct
70 Correct 2183 ms 58792 KB Output is correct
71 Correct 226 ms 8676 KB Output is correct
72 Correct 2149 ms 46204 KB Output is correct
73 Correct 2130 ms 46388 KB Output is correct
74 Correct 5 ms 256 KB Output is correct
75 Correct 5 ms 256 KB Output is correct
76 Correct 5 ms 256 KB Output is correct
77 Correct 5 ms 256 KB Output is correct
78 Correct 5 ms 256 KB Output is correct
79 Correct 26 ms 1268 KB Output is correct
80 Correct 2219 ms 61116 KB Output is correct
81 Correct 2312 ms 61168 KB Output is correct
82 Correct 2248 ms 61100 KB Output is correct
83 Correct 2215 ms 52000 KB Output is correct
84 Correct 2191 ms 51960 KB Output is correct
85 Correct 2218 ms 61020 KB Output is correct
86 Correct 725 ms 26808 KB Output is correct
87 Correct 5 ms 256 KB Output is correct