Submission #283187

#TimeUsernameProblemLanguageResultExecution timeMemory
283187WLZStrange Device (APIO19_strange_device)C++14
100 / 100
563 ms53584 KiB
#include <bits/stdc++.h>
using namespace std;

long long gcd(long long a, long long b) {
  while (b > 0) {
    swap(a, b);
    b %= a;
  }
  return a;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  long long a, b;
  cin >> n >> a >> b;
  long long md = (long long) 1e18 + 1;
  a /= gcd(a, b + 1);
  if (a <= numeric_limits<long long>::max() / b) {
    md = a * b;
  }
  vector< pair<long long, long long> > v;
  for (int i = 0; i < n; i++) {
    long long l, r;
    cin >> l >> r;
    if (r - l + 1 >= md) {
      cout << md << '\n';
      return 0;
    }
    long long x = l % md, y = r % md;
    if (x <= y) {
      v.push_back({x, y});
    } else {
      v.push_back({x, md - 1});
      v.push_back({0, y});
    }
  }
  sort(v.begin(), v.end());
  long long ans = 0;
  long long l = v[0].first, r = v[0].second;
  for (auto& p : v) {
    if (p.first > r) {
      ans += r - l + 1;
      l = p.first;
      r = p.second;
    } else {
      r = max(r, p.second);
    }
  }
  ans += r - l + 1;
  cout << ans << '\n';
  return 0;
}
#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...