This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define int long long
using namespace std;
int gcd(int a, int b) {
a = abs(a);
b = abs(b);
if(a < b) swap(a, b);
while(b) {
a %= b;
swap(a, b);
}
return a;
}
vector<int> B;
int getidx(int n) {
return lower_bound(B.begin(),B.end(),n) - B.begin();
}
struct SegTree {
vector<int> seg;
int MAX;
SegTree(int N) {
int i;
for(i=2;i<2*N;i*=2) {}
seg.resize(i);
MAX = i;
}
int sum(int s, int e, int n, int ns, int ne) {
if(e<=ns||ne<=s) return 0;
if(s<=ns&&ne<=e) return seg[n];
int mid = ns + ne >> 1;
return sum(s, e, 2*n, ns, mid) + sum(s, e, 2*n+1, mid, ne);
}
void update(int n) {
n += MAX/2;
n /= 2;
while(n) {
seg[n] = seg[2*n] + seg[2*n+1];
n /= 2;
}
}
void Plus(int n) {
seg[n+MAX/2]++;
update(n);
}
void Minus(int n) {
seg[n+MAX/2]--;
update(n);
}
int sum(int s, int e) {
return sum(s, e, 1, 0, MAX/2);
}
};
int L[1000005];
int R[1000005];
bool Check(int a, int b) {
int d = 1e9;
int a1 = a / d;
int a2 = a % d;
int b1 = b / d;
int b2 = b % d;
if(a1&&b1) return true;
if(a1*b2+a2*b1 + (a2*b2/d) >= d) return true;
return false;
}
main() {
cin.sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int N, a, b;
cin >> N >> a >> b;
int d = gcd(a, b + 1);
int c = a / d;
assert(a % d == 0);
int k;
if(Check(b, c)) k = 1e18 + 5;
else k = b * c;
assert(k < 1e18 + 10);
assert(k > 0);
bool isAll = false;
int i;
B.push_back(0);
B.push_back(k);
for(i=0;i<N;i++) {
cin >> L[i] >> R[i];
if(R[i] - L[i] >= k-1) isAll = true;
L[i] %= k;
R[i] %= k;
R[i]++;
B.push_back(L[i]);
B.push_back(R[i]);
}
sort(B.begin(),B.end());
B.erase(unique(B.begin(),B.end()),B.end());
SegTree tree(B.size() + 10);
for(i=0;i<N;i++) {
int l = getidx(L[i]);
int r = getidx(R[i]);
if(l < r) {
tree.Plus(l);
tree.Minus(r);
}
else {
tree.Plus(0);
tree.Minus(r);
tree.Plus(l);
tree.Minus(B.size());
}
}
int cnt = 0;
for(i=0;i<B.size()-1;i++) {
if(tree.sum(0,i+1)!=0) {
cnt += B[i+1]-B[i];
}
}
if(isAll) cnt = k;
cout << cnt;
}
Compilation message (stderr)
strange_device.cpp: In member function 'long long int SegTree::sum(long long int, long long int, long long int, long long int, long long int)':
strange_device.cpp:30:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
30 | int mid = ns + ne >> 1;
| ~~~^~~~
strange_device.cpp: At global scope:
strange_device.cpp:65:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
65 | main() {
| ^~~~
strange_device.cpp: In function 'int main()':
strange_device.cpp:110:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
110 | for(i=0;i<B.size()-1;i++) {
| ~^~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |