Submission #1279700

#TimeUsernameProblemLanguageResultExecution timeMemory
1279700swishy123Strange Device (APIO19_strange_device)C++20
100 / 100
387 ms17580 KiB
#include <bits/stdc++.h>

#define ll long long
#define pi pair<int, int>
#define pl pair<ll, ll>
#define x first
#define y second

const ll inf = 1e18;
const int def = 1e5+1;
using namespace std;

typedef __int128_t i128;
void solve(){
    ll n, A, B;
    cin >> n >> A >> B;

    ll g = gcd(A, B + 1);
    i128 x = A / g;
    i128 cycle = (i128)B * x;

    bool epic = 0;
    vector<pl> sm;
    for (int i = 0; i < n; i++){
        ll l, r;
        cin >> l >> r;
        
        if (r - l + 1 >= cycle)
            epic = 1;

        l %= cycle; r %= cycle;
        if (l <= r)
            sm.push_back({l, r});
        else{
            sm.push_back({l, cycle - 1});
            sm.push_back({0, r});
        }
    }
    if (epic){
        cout << (ll)cycle;
        return;
    }

    sort(sm.begin(), sm.end());
    ll res = 0, max_r = -inf;

    for (int i = 0; i < sm.size(); i++){
        auto [l, r] = sm[i];
        max_r = max(max_r, l - 1);

        res += max(r - max_r, 0ll);
        max_r = max(max_r, r);
    }
    cout << res;
}   

/*
find x so that (b + 1) * x mod a = 0
1 20
0 18
39 48
*/

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    if (ifstream("input.txt").good()){
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout); 
    }

    int t;
    t = 1;
    
    while (t--){
        solve();
    }
}

Compilation message (stderr)

strange_device.cpp: In function 'int main()':
strange_device.cpp:69:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   69 |         freopen("input.txt", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
strange_device.cpp:70:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   70 |         freopen("output.txt", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...