#include<bits/stdc++.h>
#define tm (tl + tr)/2
#define inf 1100000000000000000
#define ll long long
using namespace std;
struct tre{
bool lazy;
ll sum;
ll L,R;
tre() {
lazy = sum = L = R = 0;
}
};
vector<tre>t(1);
void push(ll v,ll tl,ll tr,ll L,ll R) {
if (t[v].lazy) {
t[L].lazy = t[R].lazy = 1;
t[L].sum = tm - tl+1;
t[R].sum = tr - tm;
t[v].lazy = 0;
}
}
void upd(ll v,ll tl,ll tr,ll l,ll r) {
if (l > tr || r < tl || t[v].lazy) return;
if (l <= tl && r >= tr) {
t[v].lazy = 1;
t[v].sum = tr - tl + 1;
return;
}
if (t[v].L == 0) {
t[v].L = t.size();
t.push_back(tre());
}
if (t[v].R == 0) {
t[v].R = t.size();
t.push_back(tre());
}
push(v,tl,tr,t[v].L,t[v].R);
upd(t[v].L,tl,tm,l,r);
upd(t[v].R,tm+1,tr,l,r);
t[v].sum = t[ t[v].L ].sum + t[ t[v].R ].sum;
}
main() {
ios::sync_with_stdio(false);
ll n,a,b;
cin >> n >> a >> b;
ll m = a/(__gcd(a,b+1));
if (m > inf/b) m = inf;
else m*=b;
vector<pair<ll,ll> > v;
for (ll l,r;n--;) {
cin >> l >> r;
if (r - l + 1 >= m) {
cout << m <<'\n';
exit(0);
}
l%=m;
r%=m;
if (l < 0) l+=m;
if (r < 0) r+=m;
if ( l <= r) {
v.push_back({l,r});
// upd(0,0,m-1,l,r);
}
else {
v.push_back({0,r});
v.push_back({l,m-1});
// upd(0,0,m-1,l,m-1);
// upd(0,0,m-1,0,r);
}
}
sort(v.begin(),v.end());
ll st = 0, en = -1,ans = 0;
for (auto i : v) {
ll x = i.first, y =i.second;
if (y <= en) continue;
if (x > en) {
ans += y - x + 1;
st = x;
en = y;
}
else {
ans += y - en;
st = x;
en = y;
}
}
cout << ans;
}
Compilation message
strange_device.cpp: In constructor 'tre::tre()':
strange_device.cpp:12:14: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
lazy = sum = L = R = 0;
~~~~^~~~~~~~~~~
strange_device.cpp: At global scope:
strange_device.cpp:48:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main() {
^
strange_device.cpp: In function 'int main()':
strange_device.cpp:79:5: warning: variable 'st' set but not used [-Wunused-but-set-variable]
ll st = 0, en = -1,ans = 0;
^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
768 KB |
Output is correct |
3 |
Correct |
5 ms |
768 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
768 KB |
Output is correct |
17 |
Correct |
55 ms |
2544 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
372 ms |
16952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
521 ms |
16876 KB |
Output is correct |
3 |
Correct |
512 ms |
22228 KB |
Output is correct |
4 |
Correct |
512 ms |
23948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
521 ms |
16876 KB |
Output is correct |
3 |
Correct |
512 ms |
22228 KB |
Output is correct |
4 |
Correct |
512 ms |
23948 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
502 ms |
23892 KB |
Output is correct |
7 |
Correct |
523 ms |
23960 KB |
Output is correct |
8 |
Correct |
576 ms |
23908 KB |
Output is correct |
9 |
Correct |
633 ms |
23868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
521 ms |
16876 KB |
Output is correct |
3 |
Correct |
512 ms |
22228 KB |
Output is correct |
4 |
Correct |
512 ms |
23948 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
57 ms |
5736 KB |
Output is correct |
7 |
Correct |
80 ms |
5740 KB |
Output is correct |
8 |
Correct |
55 ms |
5740 KB |
Output is correct |
9 |
Correct |
65 ms |
5748 KB |
Output is correct |
10 |
Correct |
54 ms |
5740 KB |
Output is correct |
11 |
Correct |
62 ms |
5740 KB |
Output is correct |
12 |
Correct |
56 ms |
5736 KB |
Output is correct |
13 |
Correct |
64 ms |
5736 KB |
Output is correct |
14 |
Correct |
59 ms |
5760 KB |
Output is correct |
15 |
Correct |
67 ms |
5780 KB |
Output is correct |
16 |
Correct |
59 ms |
5760 KB |
Output is correct |
17 |
Correct |
54 ms |
5740 KB |
Output is correct |
18 |
Correct |
515 ms |
23764 KB |
Output is correct |
19 |
Correct |
508 ms |
23796 KB |
Output is correct |
20 |
Correct |
570 ms |
23796 KB |
Output is correct |
21 |
Correct |
71 ms |
5776 KB |
Output is correct |
22 |
Correct |
51 ms |
5744 KB |
Output is correct |
23 |
Correct |
186 ms |
18656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
56 ms |
2544 KB |
Output is correct |
3 |
Correct |
53 ms |
2544 KB |
Output is correct |
4 |
Correct |
574 ms |
23764 KB |
Output is correct |
5 |
Correct |
54 ms |
5740 KB |
Output is correct |
6 |
Correct |
59 ms |
5740 KB |
Output is correct |
7 |
Correct |
55 ms |
5736 KB |
Output is correct |
8 |
Correct |
56 ms |
5740 KB |
Output is correct |
9 |
Correct |
65 ms |
5884 KB |
Output is correct |
10 |
Correct |
54 ms |
5736 KB |
Output is correct |
11 |
Correct |
54 ms |
5740 KB |
Output is correct |
12 |
Correct |
50 ms |
5740 KB |
Output is correct |
13 |
Correct |
55 ms |
5740 KB |
Output is correct |
14 |
Correct |
575 ms |
23768 KB |
Output is correct |
15 |
Correct |
58 ms |
5736 KB |
Output is correct |
16 |
Correct |
519 ms |
23640 KB |
Output is correct |
17 |
Correct |
501 ms |
23636 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
768 KB |
Output is correct |
3 |
Correct |
5 ms |
768 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
768 KB |
Output is correct |
17 |
Correct |
55 ms |
2544 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
0 ms |
384 KB |
Output is correct |
20 |
Correct |
0 ms |
384 KB |
Output is correct |
21 |
Correct |
0 ms |
384 KB |
Output is correct |
22 |
Correct |
0 ms |
384 KB |
Output is correct |
23 |
Correct |
0 ms |
384 KB |
Output is correct |
24 |
Correct |
0 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
372 ms |
16952 KB |
Output is correct |
29 |
Correct |
0 ms |
384 KB |
Output is correct |
30 |
Correct |
521 ms |
16876 KB |
Output is correct |
31 |
Correct |
512 ms |
22228 KB |
Output is correct |
32 |
Correct |
512 ms |
23948 KB |
Output is correct |
33 |
Correct |
0 ms |
384 KB |
Output is correct |
34 |
Correct |
502 ms |
23892 KB |
Output is correct |
35 |
Correct |
523 ms |
23960 KB |
Output is correct |
36 |
Correct |
576 ms |
23908 KB |
Output is correct |
37 |
Correct |
633 ms |
23868 KB |
Output is correct |
38 |
Correct |
0 ms |
384 KB |
Output is correct |
39 |
Correct |
57 ms |
5736 KB |
Output is correct |
40 |
Correct |
80 ms |
5740 KB |
Output is correct |
41 |
Correct |
55 ms |
5740 KB |
Output is correct |
42 |
Correct |
65 ms |
5748 KB |
Output is correct |
43 |
Correct |
54 ms |
5740 KB |
Output is correct |
44 |
Correct |
62 ms |
5740 KB |
Output is correct |
45 |
Correct |
56 ms |
5736 KB |
Output is correct |
46 |
Correct |
64 ms |
5736 KB |
Output is correct |
47 |
Correct |
59 ms |
5760 KB |
Output is correct |
48 |
Correct |
67 ms |
5780 KB |
Output is correct |
49 |
Correct |
59 ms |
5760 KB |
Output is correct |
50 |
Correct |
54 ms |
5740 KB |
Output is correct |
51 |
Correct |
515 ms |
23764 KB |
Output is correct |
52 |
Correct |
508 ms |
23796 KB |
Output is correct |
53 |
Correct |
570 ms |
23796 KB |
Output is correct |
54 |
Correct |
71 ms |
5776 KB |
Output is correct |
55 |
Correct |
51 ms |
5744 KB |
Output is correct |
56 |
Correct |
186 ms |
18656 KB |
Output is correct |
57 |
Correct |
1 ms |
384 KB |
Output is correct |
58 |
Correct |
56 ms |
2544 KB |
Output is correct |
59 |
Correct |
53 ms |
2544 KB |
Output is correct |
60 |
Correct |
574 ms |
23764 KB |
Output is correct |
61 |
Correct |
54 ms |
5740 KB |
Output is correct |
62 |
Correct |
59 ms |
5740 KB |
Output is correct |
63 |
Correct |
55 ms |
5736 KB |
Output is correct |
64 |
Correct |
56 ms |
5740 KB |
Output is correct |
65 |
Correct |
65 ms |
5884 KB |
Output is correct |
66 |
Correct |
54 ms |
5736 KB |
Output is correct |
67 |
Correct |
54 ms |
5740 KB |
Output is correct |
68 |
Correct |
50 ms |
5740 KB |
Output is correct |
69 |
Correct |
55 ms |
5740 KB |
Output is correct |
70 |
Correct |
575 ms |
23768 KB |
Output is correct |
71 |
Correct |
58 ms |
5736 KB |
Output is correct |
72 |
Correct |
519 ms |
23640 KB |
Output is correct |
73 |
Correct |
501 ms |
23636 KB |
Output is correct |
74 |
Correct |
1 ms |
384 KB |
Output is correct |
75 |
Correct |
0 ms |
384 KB |
Output is correct |
76 |
Correct |
1 ms |
384 KB |
Output is correct |
77 |
Correct |
0 ms |
384 KB |
Output is correct |
78 |
Correct |
0 ms |
384 KB |
Output is correct |
79 |
Correct |
5 ms |
1152 KB |
Output is correct |
80 |
Correct |
598 ms |
23636 KB |
Output is correct |
81 |
Correct |
584 ms |
23716 KB |
Output is correct |
82 |
Correct |
567 ms |
23796 KB |
Output is correct |
83 |
Correct |
548 ms |
23660 KB |
Output is correct |
84 |
Correct |
567 ms |
23532 KB |
Output is correct |
85 |
Correct |
555 ms |
23660 KB |
Output is correct |
86 |
Correct |
165 ms |
18484 KB |
Output is correct |
87 |
Correct |
0 ms |
384 KB |
Output is correct |