# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
719495 | 2023-04-06T06:12:00 Z | keisuke6 | Strange Device (APIO19_strange_device) | C++14 | 0 ms | 0 KB |
#include <bits/stdc++.h> using namespace std; #define int long long signed main(){ int N,A,B; cin>>N>>A>>B; B++; int g = gcd(A,B); B--; int p = 0; if(1.0*A*B/g >= 4e18) p = 2e18; else p = A*B/g; map<int,int> m; for(int i=0;i<N;i++){ int l,r; cin>>l>>r; if(l == r){ l %= p; r %= p; m[l]++; m[r+1]--; continue; } l %= p; r %= p; if(l >= r){ m[l]++; m[p]--; m[0]++; m[r+1]--; } else{ m[l]++; m[r+1]--; } } vector<pair<int,int>> S = {}; for(auto x:m) S.push_back(x); sort(S.begin(),S.end()); int now = S[0].second; int ans = 0; for(int i=1;i<S.size();i++){ if(now){ ans += S[i].first-S[i-1].first; } now += S[i].second; } cout<<ans<<endl; }