#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
// vector<vector<ll>> overlappedInterval(vector<vector<ll>>& intervals) {
// if (intervals.empty()) {
// return vector<vector<ll>>();
// }
// // Sort intervals based on start values
// sort(intervals.begin(), intervals.end(), [](const vector<ll>& a, const vector<ll>& b) {
// return a[0] < b[0];
// });
// stack<vector<ll>> mergedStack;
// mergedStack.push(intervals[0]);
// for (int i = 1; i < intervals.size(); i++) {
// vector<ll> current = intervals[i];
// vector<ll>& top = mergedStack.top();
// if (current[0] <= top[1]) {
// // If current interval overlaps with the top of the stack, merge them
// top[1] = max(top[1], current[1]);
// } else {
// // If no overlap, push the current interval onto the stack
// mergedStack.push(current);
// }
// }
// // Convert the stack to a vector
// vector<vector<ll>> mergedIntervals;
// while (!mergedStack.empty()) {
// mergedIntervals.insert(mergedIntervals.begin(), mergedStack.top());
// mergedStack.pop();
// }
// return mergedIntervals;
// }
struct Interval {
ll s, e;
};
// Function used in sort
bool mycomp(Interval a, Interval b) { return a.s < b.s; }
void mergeIntervals(Interval arr[], ll n)
{
// Sort Intervals in increasing order of
// start time
sort(arr, arr + n, mycomp);
ll index = 0; // Stores index of last element
// in output array (modified arr[])
// Traverse all input Intervals
for (ll i = 1; i < n; i++) {
// If this is not first Interval and overlaps
// with the previous one
if (arr[index].e >= arr[i].s) {
// Merge previous and current Intervals
arr[index].e = max(arr[index].e, arr[i].e);
}
else {
index++;
arr[index] = arr[i];
}
}
// Now arr[0..index-1] stores the merged Intervals
//cout << "\n The Merged Intervals are: ";
ll ans =0;
for (ll i = 0; i <= index; i++) ans+= arr[i].e-arr[i].s+1;
cout<<ans<<"\n";
//cout << "[" << arr[i].s << ", " << arr[i].e << "] ";
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
set<pair<ll, ll>> s;
ll n, A, B; cin>>n>>A>>B;
ll period;
ll X = A/(gcd(B+1, A));
int powX=0, powB=0;
ll xxx = X;
while(xxx>0){
xxx/=2;
powX+=1;
}
xxx=B;
while(xxx>0){
xxx/=2;
powB+=1;
}
if(powB+powX>61) period=4e18;
else period = B*X;
//cout<<period<<"\n";
vector<pair<ll, ll>> v;
for(int i=0; i<n; i++){
ll x, y; cin>>x>>y;
if(y-x<period){
ll XX = x%period;
ll YY = y%period;
if(XX<=YY) v.push_back({XX,YY});
else{
v.push_back({XX, period-1});
v.push_back({0, YY});
}
}
else{
v.push_back({0, period-1});
}
}
Interval arr[int(v.size())];
for(int i=0; i<int(v.size()); i++){
arr[i].s = v[i].first;
arr[i].e = v[i].second;
}
//for(auto p: v) cout<<p.first<<" "<<p.second<<"\n";
// vector<vector<ll>> merged = overlappedInterval(v);
// ll ans =0;
// for(auto vv: merged){
// ans += vv[1]-vv[0]+1;
// }
// cout<<ans<<"\n";
mergeIntervals(arr, int(v.size()));
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
504 KB |
Output is correct |
2 |
Correct |
4 ms |
1248 KB |
Output is correct |
3 |
Correct |
5 ms |
1244 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
352 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
4 ms |
1132 KB |
Output is correct |
17 |
Correct |
42 ms |
6000 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
247 ms |
44876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
373 ms |
50840 KB |
Output is correct |
3 |
Correct |
350 ms |
50744 KB |
Output is correct |
4 |
Correct |
340 ms |
50720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
373 ms |
50840 KB |
Output is correct |
3 |
Correct |
350 ms |
50744 KB |
Output is correct |
4 |
Correct |
340 ms |
50720 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
365 ms |
50924 KB |
Output is correct |
7 |
Correct |
357 ms |
50764 KB |
Output is correct |
8 |
Correct |
348 ms |
50904 KB |
Output is correct |
9 |
Correct |
400 ms |
51716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
373 ms |
50840 KB |
Output is correct |
3 |
Correct |
350 ms |
50744 KB |
Output is correct |
4 |
Correct |
340 ms |
50720 KB |
Output is correct |
5 |
Correct |
0 ms |
600 KB |
Output is correct |
6 |
Correct |
34 ms |
5836 KB |
Output is correct |
7 |
Correct |
47 ms |
5836 KB |
Output is correct |
8 |
Correct |
38 ms |
5836 KB |
Output is correct |
9 |
Correct |
38 ms |
6016 KB |
Output is correct |
10 |
Correct |
34 ms |
5832 KB |
Output is correct |
11 |
Correct |
36 ms |
5828 KB |
Output is correct |
12 |
Correct |
33 ms |
5992 KB |
Output is correct |
13 |
Correct |
44 ms |
5784 KB |
Output is correct |
14 |
Correct |
33 ms |
5836 KB |
Output is correct |
15 |
Correct |
41 ms |
5888 KB |
Output is correct |
16 |
Correct |
38 ms |
5832 KB |
Output is correct |
17 |
Correct |
35 ms |
5828 KB |
Output is correct |
18 |
Correct |
370 ms |
50800 KB |
Output is correct |
19 |
Correct |
345 ms |
50832 KB |
Output is correct |
20 |
Correct |
405 ms |
51316 KB |
Output is correct |
21 |
Correct |
42 ms |
5832 KB |
Output is correct |
22 |
Correct |
33 ms |
5760 KB |
Output is correct |
23 |
Correct |
103 ms |
18104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
42 ms |
5776 KB |
Output is correct |
3 |
Correct |
39 ms |
5824 KB |
Output is correct |
4 |
Correct |
417 ms |
50856 KB |
Output is correct |
5 |
Correct |
42 ms |
5988 KB |
Output is correct |
6 |
Correct |
39 ms |
5784 KB |
Output is correct |
7 |
Correct |
39 ms |
5832 KB |
Output is correct |
8 |
Correct |
40 ms |
5832 KB |
Output is correct |
9 |
Correct |
39 ms |
5832 KB |
Output is correct |
10 |
Correct |
42 ms |
5944 KB |
Output is correct |
11 |
Correct |
43 ms |
5868 KB |
Output is correct |
12 |
Correct |
32 ms |
5788 KB |
Output is correct |
13 |
Correct |
44 ms |
5840 KB |
Output is correct |
14 |
Correct |
405 ms |
50756 KB |
Output is correct |
15 |
Correct |
40 ms |
5836 KB |
Output is correct |
16 |
Correct |
359 ms |
50632 KB |
Output is correct |
17 |
Correct |
345 ms |
50640 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
504 KB |
Output is correct |
2 |
Correct |
4 ms |
1248 KB |
Output is correct |
3 |
Correct |
5 ms |
1244 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
352 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
4 ms |
1132 KB |
Output is correct |
17 |
Correct |
42 ms |
6000 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
460 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
247 ms |
44876 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
373 ms |
50840 KB |
Output is correct |
31 |
Correct |
350 ms |
50744 KB |
Output is correct |
32 |
Correct |
340 ms |
50720 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
365 ms |
50924 KB |
Output is correct |
35 |
Correct |
357 ms |
50764 KB |
Output is correct |
36 |
Correct |
348 ms |
50904 KB |
Output is correct |
37 |
Correct |
400 ms |
51716 KB |
Output is correct |
38 |
Correct |
0 ms |
600 KB |
Output is correct |
39 |
Correct |
34 ms |
5836 KB |
Output is correct |
40 |
Correct |
47 ms |
5836 KB |
Output is correct |
41 |
Correct |
38 ms |
5836 KB |
Output is correct |
42 |
Correct |
38 ms |
6016 KB |
Output is correct |
43 |
Correct |
34 ms |
5832 KB |
Output is correct |
44 |
Correct |
36 ms |
5828 KB |
Output is correct |
45 |
Correct |
33 ms |
5992 KB |
Output is correct |
46 |
Correct |
44 ms |
5784 KB |
Output is correct |
47 |
Correct |
33 ms |
5836 KB |
Output is correct |
48 |
Correct |
41 ms |
5888 KB |
Output is correct |
49 |
Correct |
38 ms |
5832 KB |
Output is correct |
50 |
Correct |
35 ms |
5828 KB |
Output is correct |
51 |
Correct |
370 ms |
50800 KB |
Output is correct |
52 |
Correct |
345 ms |
50832 KB |
Output is correct |
53 |
Correct |
405 ms |
51316 KB |
Output is correct |
54 |
Correct |
42 ms |
5832 KB |
Output is correct |
55 |
Correct |
33 ms |
5760 KB |
Output is correct |
56 |
Correct |
103 ms |
18104 KB |
Output is correct |
57 |
Correct |
1 ms |
344 KB |
Output is correct |
58 |
Correct |
42 ms |
5776 KB |
Output is correct |
59 |
Correct |
39 ms |
5824 KB |
Output is correct |
60 |
Correct |
417 ms |
50856 KB |
Output is correct |
61 |
Correct |
42 ms |
5988 KB |
Output is correct |
62 |
Correct |
39 ms |
5784 KB |
Output is correct |
63 |
Correct |
39 ms |
5832 KB |
Output is correct |
64 |
Correct |
40 ms |
5832 KB |
Output is correct |
65 |
Correct |
39 ms |
5832 KB |
Output is correct |
66 |
Correct |
42 ms |
5944 KB |
Output is correct |
67 |
Correct |
43 ms |
5868 KB |
Output is correct |
68 |
Correct |
32 ms |
5788 KB |
Output is correct |
69 |
Correct |
44 ms |
5840 KB |
Output is correct |
70 |
Correct |
405 ms |
50756 KB |
Output is correct |
71 |
Correct |
40 ms |
5836 KB |
Output is correct |
72 |
Correct |
359 ms |
50632 KB |
Output is correct |
73 |
Correct |
345 ms |
50640 KB |
Output is correct |
74 |
Correct |
0 ms |
348 KB |
Output is correct |
75 |
Correct |
0 ms |
344 KB |
Output is correct |
76 |
Correct |
0 ms |
348 KB |
Output is correct |
77 |
Correct |
0 ms |
348 KB |
Output is correct |
78 |
Correct |
0 ms |
348 KB |
Output is correct |
79 |
Correct |
3 ms |
1196 KB |
Output is correct |
80 |
Correct |
427 ms |
59972 KB |
Output is correct |
81 |
Correct |
424 ms |
59940 KB |
Output is correct |
82 |
Correct |
404 ms |
60240 KB |
Output is correct |
83 |
Correct |
411 ms |
59936 KB |
Output is correct |
84 |
Correct |
397 ms |
59816 KB |
Output is correct |
85 |
Correct |
398 ms |
59824 KB |
Output is correct |
86 |
Correct |
99 ms |
22200 KB |
Output is correct |
87 |
Correct |
1 ms |
348 KB |
Output is correct |