Submission #977369

#TimeUsernameProblemLanguageResultExecution timeMemory
977369notyouStrange Device (APIO19_strange_device)C++17
100 / 100
377 ms45168 KiB
#include <bits/stdc++.h>

// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>

#define ll long long
#define pb push_back
#define vl vector<ll>
#define fi first
#define se second
#define in insert
#define all(v) v.begin(), v.end()
#define bpc(x) __builtin_popcount(x)
#define endl "\n"

using namespace std;
const ll inf = 1e18;
const ll mod = 1e9 + 7;
const int sz = 4e5 + 5;
const int LG = 21;
// using namespace __gnu_pbds;
// tree <ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> rbt;
ll gcd(ll a, ll b){
    return __gcd(a, b);
}
ll get(ll a, ll b){
    ll c = (a / gcd(a, b + 1)), hi = inf / b;
    if(c > hi){
        c = inf;
    }
    else{
        c *= b;
    }
    return c;
}
void solve(){
    ll n, a, b, i, l, r;
    cin >> n >> a >> b;
    if(a == 1 && b == 1){
        cout << 1 << endl;
        return ;
    }
    ll x = get(a, b);
    vector<array<ll, 2>> v;
    bool ok = 1;
    for(i = 1; i <= n; i++){
        cin >> l >> r;
        ok &= ((r - l + 1) >= x);
        l %= x;
        r %= x;
        if(r < l){
            v.pb({l, x - 1});
            v.pb({0, r});
        }
        else{
            v.pb({l, r});
        }
    }
    if(ok){
        cout << x << endl;
        return ;
    }
    sort(all(v));
    ll s = 0, hi = -1;
    for(i = 0; i < v.size(); i++){
        // cout << v[i][0] << ' ' << v[i][1] << endl;
        if(v[i][0] > hi){
            s += (v[i][1] - v[i][0] + 1);
            hi = v[i][1];
        }
        else if(v[i][1] > hi){
            s += (v[i][1] - hi);
            hi = v[i][1];
        }
    }
    cout << s << endl;


}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll t = 1;
    // cin >> t;
    while(t--){
        solve();
    }
}
/*
3 3 3
4 4
7 9
17 18
*/

Compilation message (stderr)

strange_device.cpp: In function 'void solve()':
strange_device.cpp:65:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   65 |     for(i = 0; i < v.size(); i++){
      |                ~~^~~~~~~~~~
#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...