Submission #654690

#TimeUsernameProblemLanguageResultExecution timeMemory
654690horiseunStrange Device (APIO19_strange_device)C++11
100 / 100
645 ms69000 KiB
#include <iostream>
#include <vector>
#include <set>
#include <tuple>
#include <algorithm>
using namespace std;

#define ull unsigned long long
#define f first
#define s second

ull a, b, n, l, r, p, ans, c, lft;
vector<pair<ull, int>> v;

ull gcd(ull x, ull y) {
    if (y == 0) return x;
    return gcd(y, x % y);
}

int main() {

    ios_base::sync_with_stdio(false);
    cin.tie(0);

    cin >> n >> a >> b;
    p = b * (a / gcd(a, b + 1));
    for (int i = 0; i < n; i++) {
        cin >> l >> r;
        if (r - l >= p) {
            cout << p << "\n";
            return 0;
        }
        if ((l + p - 1) % p > (r + p - 1) % p) {
            v.push_back({1, 0});
            v.push_back({(l + p - 1) % p + 1, 0});
            v.push_back({(r + p - 1) % p + 1, 1});
            v.push_back({p, 1});
        } else {
            v.push_back({(l + p - 1) % p + 1, 0});
            v.push_back({(r + p - 1) % p + 1, 1});
        }
    }
    sort(v.begin(), v.end());

    lft = -1;
    for (pair<ull, int> i : v) {
        if (!i.s) {
            c++;
            if (lft == -1) lft = i.f;
        } else {
            c--;
            if (c == 0) {
                ans += (i.f - lft + 1);
                lft = -1;
            }
        }
    }
    cout << ans << "\n";

}

Compilation message (stderr)

strange_device.cpp: In function 'int main()':
strange_device.cpp:27:23: warning: comparison of integer expressions of different signedness: 'int' and 'long long unsigned int' [-Wsign-compare]
   27 |     for (int i = 0; i < n; i++) {
      |                     ~~^~~
strange_device.cpp:49:21: warning: comparison of integer expressions of different signedness: 'long long unsigned int' and 'int' [-Wsign-compare]
   49 |             if (lft == -1) lft = i.f;
      |                 ~~~~^~~~~
#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...