Submission #1279690

#TimeUsernameProblemLanguageResultExecution timeMemory
1279690swishy123Strange Device (APIO19_strange_device)C++20
0 / 100
298 ms17112 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;

    vector<pl> sm;
    for (int i = 0; i < n; i++){
        ll l, r;
        cin >> l >> r;
        
        l %= cycle; r %= cycle;
        if (l <= r)
            sm.push_back({l, r});
        else{
            sm.push_back({l, cycle - 1});
            sm.push_back({0, r});
        }
    }
    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 += r - max_r;
        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:60:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   60 |         freopen("input.txt", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
strange_device.cpp:61:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   61 |         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...