Submission #1095036

# Submission time Handle Problem Language Result Execution time Memory
1095036 2024-10-01T07:48:29 Z ro9669 Strange Device (APIO19_strange_device) C++17
100 / 100
336 ms 69076 KB
#include <bits/stdc++.h>
#define fi first
#define se second
#define all(v) v.begin() , v.end()
#define sz(v) int(v.size())
#define unq(v) sort(all(v)); v.resize(unique(all(v)) - v.begin());
using namespace std;

typedef long long ll;
typedef pair<int , int> ii;
typedef pair<long long , int> lli;

const int maxN = int(2e6)+7;
const ll inf = ll(1e18)+7;

pair<ll , ll> p[maxN] , nxt_p[maxN];

void solve(){
    int n; ll a , b;
    cin >> n >> a >> b;
    for (int i = 1 ; i <= n ; i++) cin >> p[i].fi >> p[i].se;
    a /= __gcd(a , b + 1);
    if (inf / a < b){
        ll ans = 0;
        for (int i = 1 ; i <= n ; i++) ans += 1ll * (p[i].se - p[i].fi + 1);
        cout << ans << "\n";
    }
    else{
        ll x = a * b;
        int num = 0;
        for (int i = 1 ; i <= n ; i++){
            if (p[i].fi + x - 1 <= p[i].se){
                cout << x << "\n";
                return;
            }
            if (p[i].fi % x <= p[i].se % x){
                nxt_p[++num] = {p[i].fi % x , p[i].se % x};
            }
            else{
                nxt_p[++num] = {p[i].fi % x , x - 1};
                nxt_p[++num] = {0 , p[i].se % x};
            }
        }
        n = num;
        sort(nxt_p + 1 , nxt_p + n + 1);
        ll ans = 1ll * (nxt_p[1].se - nxt_p[1].fi + 1) , cur = nxt_p[1].se + 1;
        for (int i = 2 ; i <= n ; i++){
            if (max(cur , nxt_p[i].fi) <= nxt_p[i].se){
                ans += nxt_p[i].se - max(cur , nxt_p[i].fi) + 1;
                cur = nxt_p[i].se + 1;
            }
        }
        cout << ans << "\n";
    }
}

#define name "A"

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    if (fopen(name".INP" , "r")){
        freopen(name".INP" , "r" , stdin);
        freopen(name".OUT" , "w" , stdout);
    }
    int t = 1; //cin >> t;
    while (t--) solve();
    return 0;
}

Compilation message

strange_device.cpp: In function 'int main()':
strange_device.cpp:62:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   62 |         freopen(name".INP" , "r" , stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
strange_device.cpp:63:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   63 |         freopen(name".OUT" , "w" , stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 3 ms 1116 KB Output is correct
3 Correct 3 ms 1116 KB Output is correct
4 Correct 0 ms 464 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 3 ms 992 KB Output is correct
17 Correct 29 ms 7276 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 185 ms 56916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 296 ms 68760 KB Output is correct
3 Correct 268 ms 68732 KB Output is correct
4 Correct 282 ms 68960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 296 ms 68760 KB Output is correct
3 Correct 268 ms 68732 KB Output is correct
4 Correct 282 ms 68960 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 265 ms 69060 KB Output is correct
7 Correct 258 ms 69052 KB Output is correct
8 Correct 274 ms 68944 KB Output is correct
9 Correct 301 ms 68944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 296 ms 68760 KB Output is correct
3 Correct 268 ms 68732 KB Output is correct
4 Correct 282 ms 68960 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 24 ms 7120 KB Output is correct
7 Correct 27 ms 7248 KB Output is correct
8 Correct 29 ms 7256 KB Output is correct
9 Correct 27 ms 7296 KB Output is correct
10 Correct 26 ms 7252 KB Output is correct
11 Correct 33 ms 7180 KB Output is correct
12 Correct 26 ms 7212 KB Output is correct
13 Correct 40 ms 7260 KB Output is correct
14 Correct 25 ms 7260 KB Output is correct
15 Correct 30 ms 7260 KB Output is correct
16 Correct 38 ms 7292 KB Output is correct
17 Correct 27 ms 7248 KB Output is correct
18 Correct 260 ms 68732 KB Output is correct
19 Correct 259 ms 68952 KB Output is correct
20 Correct 322 ms 69076 KB Output is correct
21 Correct 36 ms 7248 KB Output is correct
22 Correct 24 ms 7268 KB Output is correct
23 Correct 77 ms 23676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 27 ms 7296 KB Output is correct
3 Correct 27 ms 7172 KB Output is correct
4 Correct 302 ms 68640 KB Output is correct
5 Correct 30 ms 7252 KB Output is correct
6 Correct 29 ms 7252 KB Output is correct
7 Correct 27 ms 7256 KB Output is correct
8 Correct 29 ms 7260 KB Output is correct
9 Correct 27 ms 7296 KB Output is correct
10 Correct 28 ms 7068 KB Output is correct
11 Correct 27 ms 7260 KB Output is correct
12 Correct 24 ms 7176 KB Output is correct
13 Correct 31 ms 7088 KB Output is correct
14 Correct 296 ms 68812 KB Output is correct
15 Correct 28 ms 7252 KB Output is correct
16 Correct 277 ms 68820 KB Output is correct
17 Correct 273 ms 68812 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 3 ms 1116 KB Output is correct
3 Correct 3 ms 1116 KB Output is correct
4 Correct 0 ms 464 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 3 ms 992 KB Output is correct
17 Correct 29 ms 7276 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 185 ms 56916 KB Output is correct
29 Correct 1 ms 344 KB Output is correct
30 Correct 296 ms 68760 KB Output is correct
31 Correct 268 ms 68732 KB Output is correct
32 Correct 282 ms 68960 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 265 ms 69060 KB Output is correct
35 Correct 258 ms 69052 KB Output is correct
36 Correct 274 ms 68944 KB Output is correct
37 Correct 301 ms 68944 KB Output is correct
38 Correct 0 ms 344 KB Output is correct
39 Correct 24 ms 7120 KB Output is correct
40 Correct 27 ms 7248 KB Output is correct
41 Correct 29 ms 7256 KB Output is correct
42 Correct 27 ms 7296 KB Output is correct
43 Correct 26 ms 7252 KB Output is correct
44 Correct 33 ms 7180 KB Output is correct
45 Correct 26 ms 7212 KB Output is correct
46 Correct 40 ms 7260 KB Output is correct
47 Correct 25 ms 7260 KB Output is correct
48 Correct 30 ms 7260 KB Output is correct
49 Correct 38 ms 7292 KB Output is correct
50 Correct 27 ms 7248 KB Output is correct
51 Correct 260 ms 68732 KB Output is correct
52 Correct 259 ms 68952 KB Output is correct
53 Correct 322 ms 69076 KB Output is correct
54 Correct 36 ms 7248 KB Output is correct
55 Correct 24 ms 7268 KB Output is correct
56 Correct 77 ms 23676 KB Output is correct
57 Correct 0 ms 348 KB Output is correct
58 Correct 27 ms 7296 KB Output is correct
59 Correct 27 ms 7172 KB Output is correct
60 Correct 302 ms 68640 KB Output is correct
61 Correct 30 ms 7252 KB Output is correct
62 Correct 29 ms 7252 KB Output is correct
63 Correct 27 ms 7256 KB Output is correct
64 Correct 29 ms 7260 KB Output is correct
65 Correct 27 ms 7296 KB Output is correct
66 Correct 28 ms 7068 KB Output is correct
67 Correct 27 ms 7260 KB Output is correct
68 Correct 24 ms 7176 KB Output is correct
69 Correct 31 ms 7088 KB Output is correct
70 Correct 296 ms 68812 KB Output is correct
71 Correct 28 ms 7252 KB Output is correct
72 Correct 277 ms 68820 KB Output is correct
73 Correct 273 ms 68812 KB Output is correct
74 Correct 0 ms 348 KB Output is correct
75 Correct 1 ms 604 KB Output is correct
76 Correct 0 ms 348 KB Output is correct
77 Correct 0 ms 460 KB Output is correct
78 Correct 0 ms 348 KB Output is correct
79 Correct 3 ms 984 KB Output is correct
80 Correct 296 ms 68944 KB Output is correct
81 Correct 336 ms 68948 KB Output is correct
82 Correct 288 ms 68836 KB Output is correct
83 Correct 324 ms 68936 KB Output is correct
84 Correct 304 ms 68828 KB Output is correct
85 Correct 294 ms 68944 KB Output is correct
86 Correct 104 ms 23440 KB Output is correct
87 Correct 0 ms 344 KB Output is correct