// #pragma GCC target ("avx,avx2,fma")
// #pragma GCC optimize ("Ofast,inline") // O1 - O2 - O3 - Os - Ofast
// #pragma GCC optimize ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define per(i, a, b) for (int i = (b - 1); i >= (a); --i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) x.size()
#define pb push_back
#define debug(x) cout << #x << " = " << x << endl
#define umap unordered_map
#define uset unordered_set
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
const int INF = 1'000'000'007;
struct Node {
ll sum = 0, lo, hi, mset = INF;
Node *l = 0, *r = 0;
Node(ll L, ll R) : lo(L), hi(R) {}
void set(ll L, ll R, ll x) {
if (hi <= L || R <= lo) return;
if (L <= lo && hi <= R) {
mset = x;
sum = (hi - lo) * mset;
} else {
push();
l->set(L, R, x), r->set(L, R, x);
sum = l->sum + r->sum;
}
}
void push() {
if (!l) {
ll mid = (lo + hi) / 2;
l = new Node(lo, mid);
r = new Node(mid, hi);
}
if (mset != INF) {
l->set(lo, hi, mset), r->set(lo, hi, mset);
mset = INF;
}
}
};
int n;
ll A, B, len;
set<pll> segments;
void add(ll start, ll take) {
ll stop = start + take;
auto it = segments.lower_bound(pll(start, 0LL));
vpll rem;
while (it != segments.end()) {
if (stop < it->first) break;
stop = max(stop, it->second);
rem.emplace_back(*it);
++it;
}
it = segments.lower_bound(pll(start, 0LL));
while (it != segments.begin()) {
--it;
if (it->second < start) break;
rem.emplace_back(*it);
start = min(start, it->first);
stop = max(stop, it->second);
}
trav(item,rem)segments.erase(item);
segments.emplace(start,stop);
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
cin >> n >> A >> B;
ll x = A / __gcd(A, B + 1);
if (log10(x) + log10(B) > 18)
len = ll(1e18);
else
len = B * x;
// debug(len);
while (n--) {
ll lo, hi;
cin >> lo >> hi;
++hi;
if (hi - lo >= len)
add(0,len);
else {
ll start = lo % len, take = min(hi - lo, (len - start));
add(start, take);
// cout<<"start = "<<start<<" stop = "<<start+take<<endl;
if (start) {
start = 0;
take = (hi - lo) - take;
add(start, take);
// cout<<"start = "<<start<<" stop = "<<start+take<<endl;
}
}
// cout<<endl;
}
ll ans=0;
trav(segment,segments) ans+=segment.second-segment.first;
cout<<ans<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
7 ms |
708 KB |
Output is correct |
3 |
Correct |
7 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
320 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
7 ms |
712 KB |
Output is correct |
17 |
Correct |
61 ms |
4028 KB |
Output is correct |
18 |
Correct |
1 ms |
316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
336 ms |
3124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
586 ms |
3252 KB |
Output is correct |
3 |
Correct |
608 ms |
37324 KB |
Output is correct |
4 |
Correct |
586 ms |
37416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
586 ms |
3252 KB |
Output is correct |
3 |
Correct |
608 ms |
37324 KB |
Output is correct |
4 |
Correct |
586 ms |
37416 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
610 ms |
37424 KB |
Output is correct |
7 |
Correct |
612 ms |
37432 KB |
Output is correct |
8 |
Correct |
631 ms |
37424 KB |
Output is correct |
9 |
Correct |
630 ms |
37432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
586 ms |
3252 KB |
Output is correct |
3 |
Correct |
608 ms |
37324 KB |
Output is correct |
4 |
Correct |
586 ms |
37416 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
62 ms |
4036 KB |
Output is correct |
7 |
Correct |
60 ms |
4020 KB |
Output is correct |
8 |
Correct |
64 ms |
3920 KB |
Output is correct |
9 |
Correct |
64 ms |
4008 KB |
Output is correct |
10 |
Correct |
62 ms |
4016 KB |
Output is correct |
11 |
Correct |
61 ms |
4040 KB |
Output is correct |
12 |
Correct |
59 ms |
3916 KB |
Output is correct |
13 |
Correct |
61 ms |
4004 KB |
Output is correct |
14 |
Correct |
62 ms |
4032 KB |
Output is correct |
15 |
Correct |
64 ms |
4012 KB |
Output is correct |
16 |
Correct |
63 ms |
3916 KB |
Output is correct |
17 |
Correct |
59 ms |
4016 KB |
Output is correct |
18 |
Correct |
621 ms |
37444 KB |
Output is correct |
19 |
Correct |
627 ms |
37424 KB |
Output is correct |
20 |
Correct |
594 ms |
37416 KB |
Output is correct |
21 |
Correct |
64 ms |
4032 KB |
Output is correct |
22 |
Correct |
59 ms |
4004 KB |
Output is correct |
23 |
Correct |
139 ms |
12832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
61 ms |
320 KB |
Output is correct |
3 |
Correct |
61 ms |
316 KB |
Output is correct |
4 |
Correct |
620 ms |
37428 KB |
Output is correct |
5 |
Correct |
62 ms |
4008 KB |
Output is correct |
6 |
Correct |
60 ms |
4020 KB |
Output is correct |
7 |
Correct |
64 ms |
4008 KB |
Output is correct |
8 |
Correct |
63 ms |
4012 KB |
Output is correct |
9 |
Correct |
62 ms |
4044 KB |
Output is correct |
10 |
Correct |
62 ms |
4012 KB |
Output is correct |
11 |
Correct |
62 ms |
3916 KB |
Output is correct |
12 |
Correct |
66 ms |
4004 KB |
Output is correct |
13 |
Correct |
64 ms |
4020 KB |
Output is correct |
14 |
Correct |
615 ms |
37436 KB |
Output is correct |
15 |
Correct |
65 ms |
4016 KB |
Output is correct |
16 |
Correct |
615 ms |
37308 KB |
Output is correct |
17 |
Correct |
614 ms |
37440 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
7 ms |
708 KB |
Output is correct |
3 |
Correct |
7 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
320 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
7 ms |
712 KB |
Output is correct |
17 |
Correct |
61 ms |
4028 KB |
Output is correct |
18 |
Correct |
1 ms |
316 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
316 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
2 ms |
340 KB |
Output is correct |
28 |
Correct |
336 ms |
3124 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
586 ms |
3252 KB |
Output is correct |
31 |
Correct |
608 ms |
37324 KB |
Output is correct |
32 |
Correct |
586 ms |
37416 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
610 ms |
37424 KB |
Output is correct |
35 |
Correct |
612 ms |
37432 KB |
Output is correct |
36 |
Correct |
631 ms |
37424 KB |
Output is correct |
37 |
Correct |
630 ms |
37432 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
62 ms |
4036 KB |
Output is correct |
40 |
Correct |
60 ms |
4020 KB |
Output is correct |
41 |
Correct |
64 ms |
3920 KB |
Output is correct |
42 |
Correct |
64 ms |
4008 KB |
Output is correct |
43 |
Correct |
62 ms |
4016 KB |
Output is correct |
44 |
Correct |
61 ms |
4040 KB |
Output is correct |
45 |
Correct |
59 ms |
3916 KB |
Output is correct |
46 |
Correct |
61 ms |
4004 KB |
Output is correct |
47 |
Correct |
62 ms |
4032 KB |
Output is correct |
48 |
Correct |
64 ms |
4012 KB |
Output is correct |
49 |
Correct |
63 ms |
3916 KB |
Output is correct |
50 |
Correct |
59 ms |
4016 KB |
Output is correct |
51 |
Correct |
621 ms |
37444 KB |
Output is correct |
52 |
Correct |
627 ms |
37424 KB |
Output is correct |
53 |
Correct |
594 ms |
37416 KB |
Output is correct |
54 |
Correct |
64 ms |
4032 KB |
Output is correct |
55 |
Correct |
59 ms |
4004 KB |
Output is correct |
56 |
Correct |
139 ms |
12832 KB |
Output is correct |
57 |
Correct |
0 ms |
212 KB |
Output is correct |
58 |
Correct |
61 ms |
320 KB |
Output is correct |
59 |
Correct |
61 ms |
316 KB |
Output is correct |
60 |
Correct |
620 ms |
37428 KB |
Output is correct |
61 |
Correct |
62 ms |
4008 KB |
Output is correct |
62 |
Correct |
60 ms |
4020 KB |
Output is correct |
63 |
Correct |
64 ms |
4008 KB |
Output is correct |
64 |
Correct |
63 ms |
4012 KB |
Output is correct |
65 |
Correct |
62 ms |
4044 KB |
Output is correct |
66 |
Correct |
62 ms |
4012 KB |
Output is correct |
67 |
Correct |
62 ms |
3916 KB |
Output is correct |
68 |
Correct |
66 ms |
4004 KB |
Output is correct |
69 |
Correct |
64 ms |
4020 KB |
Output is correct |
70 |
Correct |
615 ms |
37436 KB |
Output is correct |
71 |
Correct |
65 ms |
4016 KB |
Output is correct |
72 |
Correct |
615 ms |
37308 KB |
Output is correct |
73 |
Correct |
614 ms |
37440 KB |
Output is correct |
74 |
Correct |
1 ms |
212 KB |
Output is correct |
75 |
Correct |
1 ms |
212 KB |
Output is correct |
76 |
Correct |
1 ms |
212 KB |
Output is correct |
77 |
Correct |
1 ms |
212 KB |
Output is correct |
78 |
Correct |
1 ms |
212 KB |
Output is correct |
79 |
Correct |
7 ms |
596 KB |
Output is correct |
80 |
Correct |
614 ms |
37416 KB |
Output is correct |
81 |
Correct |
623 ms |
37424 KB |
Output is correct |
82 |
Correct |
644 ms |
37452 KB |
Output is correct |
83 |
Correct |
626 ms |
37424 KB |
Output is correct |
84 |
Correct |
606 ms |
37416 KB |
Output is correct |
85 |
Correct |
664 ms |
37436 KB |
Output is correct |
86 |
Correct |
146 ms |
12828 KB |
Output is correct |
87 |
Correct |
1 ms |
212 KB |
Output is correct |