Submission #678090

#TimeUsernameProblemLanguageResultExecution timeMemory
678090FedShatStrange Device (APIO19_strange_device)C++17
100 / 100
712 ms83456 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; using i128 = __int128; constexpr int INF = numeric_limits<int>::max() / 2; constexpr ll INFLL = numeric_limits<ll>::max() / 2; template<class T> istream &operator>>(istream &is, vector<T> &a) { for (auto &i : a) { is >> i; } return is; } #ifdef __APPLE__ #include "../../debug.h" #else #define debug(...) 42 #endif int main() { cin.tie(0)->sync_with_stdio(0); #ifdef __APPLE__ freopen("input.txt", "r", stdin); #endif int n; ll a, b; cin >> n >> a >> b; vector<pair<ll, ll>> lr; i128 len = ((i128) a * (i128) b) / gcd(a, b + 1); for (int i = 0; i < n; ++i) { ll l, r; cin >> l >> r; if (l == r) { lr.push_back({l % len, r % len}); continue; } if (r - l + 1 >= len) { lr.push_back({0, len - 1}); continue; } l %= len; r %= len; if (l < r) { lr.push_back({l, r}); } else { lr.push_back({l, len - 1}); lr.push_back({0, r}); } } vector<pair<ll, int>> segs; for (auto [l, r] : lr) { segs.push_back({l, 1}); segs.push_back({r + 1, -1}); } sort(segs.begin(), segs.end(), [&](const pair<ll, int> &a, const pair<ll, int> &b) { return a.first < b.first || (a.first == b.first && a.second > b.second); }); ll ans = 0; { int balance = 0, curl = 0, curr = 0; for (auto [x, t] : segs) { balance += t; if (balance == 0) { ans += segs[curr].first - segs[curl].first; curl = curr + 1; } ++curr; } } cout << ans; }
#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...