#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
using namespace std;
const ll N=5e6+6;
struct Node{
ll l;
ll r;
ll vl=-1;
ll vr=-1;
ll al=0;
ll ar=0;
ll val=0;
};
Node tree[N];
ll K;
ll GCD(ll x, ll y){
while(x*y!=0){
if(x>y) x%=y;
else y%=x;
}
return x+y;
}
void update(ll v, ll ql, ll qr){
if(tree[v].val==tree[v].r-tree[v].l+1){
return;
}
if(qr<tree[v].l && tree[v].r<ql){
return;
}
if(ql<=tree[v].l && tree[v].r<=qr){
tree[v].val=tree[v].r-tree[v].l+1;
return;
}
ll mid=(tree[v].l+tree[v].r)/2;
if(qr<=mid){
if(!tree[v].al){
if(tree[v].vl==-1){
K++;
tree[v].vl=K;
tree[tree[v].vl].l=tree[v].l;
tree[tree[v].vl].r=mid;
update(tree[v].vl, ql, qr);
}
else if(tree[tree[v].vl].val!=mid-tree[v].l+1){
update(tree[v].vl, ql, qr);
}
}
}
else if(mid+1<=ql){
if(!tree[v].ar){
if(tree[v].vr==-1){
K++;
tree[v].vr=K;
tree[tree[v].vr].l=mid+1;
tree[tree[v].vr].r=tree[v].r;
update(tree[v].vr, ql, qr);
}
else if(tree[tree[v].vr].val!=tree[v].r-mid){
update(tree[v].vr, ql, qr);
}
}
}
else if(ql<=tree[v].l){
tree[v].al=1;
if(!tree[v].ar){
if(tree[v].vr==-1){
K++;
tree[v].vr=K;
tree[tree[v].vr].l=mid+1;
tree[tree[v].vr].r=tree[v].r;
update(tree[v].vr, ql, qr);
}
else if(tree[tree[v].vr].val!=tree[v].r-mid){
update(tree[v].vr, ql, qr);
}
}
}
else if(qr>=tree[v].r){
tree[v].ar=1;
if(!tree[v].al){
if(tree[v].vl==-1){
K++;
tree[v].vl=K;
tree[tree[v].vl].l=tree[v].l;
tree[tree[v].vl].r=mid;
update(tree[v].vl, ql, qr);
}
else if(tree[tree[v].vl].val!=mid-tree[v].l+1){
update(tree[v].vl, ql, qr);
}
}
}
else{
if(tree[v].vl==-1){
K++;
tree[v].vl=K;
tree[tree[v].vl].l=tree[v].l;
tree[tree[v].vl].r=mid;
update(tree[v].vl, ql, qr);
}
else if(tree[tree[v].vl].val!=mid-tree[v].l+1){
update(tree[v].vl, ql, qr);
}
if(tree[v].vr==-1){
K++;
tree[v].vr=K;
tree[tree[v].vr].l=mid+1;
tree[tree[v].vr].r=tree[v].r;
update(tree[v].vr, ql, qr);
}
else if(tree[tree[v].vr].val!=tree[v].r-mid){
update(tree[v].vr, ql, qr);
}
}
tree[v].val=0;
if(tree[v].al==1){
tree[v].val+=(mid-tree[v].l+1);
}
else tree[v].val+=tree[tree[v].vl].val;
if(tree[v].ar==1){
tree[v].val+=(tree[v].r-mid);
}
else tree[v].val+=tree[tree[v].vr].val;
}
ll query(ll v, ll l, ll r){
return tree[v].val;
}
int main(){
ll n, i, A, B, l, r, k, m, ma;
scanf("%lld",&n);
scanf("%lld",&A);
scanf("%lld",&B);
m=GCD(B+1, A);
A/=m;
ma=1000000000000000002;
if(ma/A<B){
k=ma;
}
else{
k=A*B;
}
// cout<<k<<endl;
tree[0].l=0;
tree[0].r=k-1;
K=0;
for(i=1; i<=n; i++){
scanf("%lld",&l);
scanf("%lld",&r);
if(r-l+1>=k) update(0, 0, k-1);
else{
if(l%k<=r%k){
update(0, l%k, r%k);
}
else{
update(0, l%k, k-1);
update(0, 0, r%k);
}
}
}
printf("%lld\n", query(0, 0, k-1));
return 0;
}
Compilation message
strange_device.cpp: In function 'int main()':
strange_device.cpp:141:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
141 | scanf("%lld",&n);
| ~~~~~^~~~~~~~~~~
strange_device.cpp:142:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
142 | scanf("%lld",&A);
| ~~~~~^~~~~~~~~~~
strange_device.cpp:143:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
143 | scanf("%lld",&B);
| ~~~~~^~~~~~~~~~~
strange_device.cpp:160:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
160 | scanf("%lld",&l);
| ~~~~~^~~~~~~~~~~
strange_device.cpp:161:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
161 | scanf("%lld",&r);
| ~~~~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
139 ms |
274524 KB |
Output is correct |
2 |
Correct |
77 ms |
274512 KB |
Output is correct |
3 |
Correct |
75 ms |
274764 KB |
Output is correct |
4 |
Correct |
66 ms |
274256 KB |
Output is correct |
5 |
Correct |
68 ms |
274252 KB |
Output is correct |
6 |
Correct |
73 ms |
274228 KB |
Output is correct |
7 |
Correct |
67 ms |
274300 KB |
Output is correct |
8 |
Correct |
69 ms |
274256 KB |
Output is correct |
9 |
Correct |
68 ms |
274200 KB |
Output is correct |
10 |
Correct |
71 ms |
274352 KB |
Output is correct |
11 |
Correct |
67 ms |
274352 KB |
Output is correct |
12 |
Correct |
72 ms |
274384 KB |
Output is correct |
13 |
Correct |
75 ms |
274256 KB |
Output is correct |
14 |
Correct |
68 ms |
274260 KB |
Output is correct |
15 |
Correct |
70 ms |
274260 KB |
Output is correct |
16 |
Correct |
82 ms |
274640 KB |
Output is correct |
17 |
Correct |
114 ms |
278100 KB |
Output is correct |
18 |
Correct |
67 ms |
274388 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
71 ms |
274224 KB |
Output is correct |
2 |
Correct |
68 ms |
274260 KB |
Output is correct |
3 |
Correct |
76 ms |
274260 KB |
Output is correct |
4 |
Correct |
66 ms |
274156 KB |
Output is correct |
5 |
Correct |
66 ms |
274296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
73 ms |
274204 KB |
Output is correct |
2 |
Correct |
71 ms |
274260 KB |
Output is correct |
3 |
Correct |
79 ms |
274364 KB |
Output is correct |
4 |
Correct |
70 ms |
274260 KB |
Output is correct |
5 |
Correct |
434 ms |
297224 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
69 ms |
274284 KB |
Output is correct |
2 |
Correct |
544 ms |
307744 KB |
Output is correct |
3 |
Runtime error |
2364 ms |
524288 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
69 ms |
274284 KB |
Output is correct |
2 |
Correct |
544 ms |
307744 KB |
Output is correct |
3 |
Runtime error |
2364 ms |
524288 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
69 ms |
274284 KB |
Output is correct |
2 |
Correct |
544 ms |
307744 KB |
Output is correct |
3 |
Runtime error |
2364 ms |
524288 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
68 ms |
274256 KB |
Output is correct |
2 |
Correct |
149 ms |
278080 KB |
Output is correct |
3 |
Correct |
170 ms |
277996 KB |
Output is correct |
4 |
Runtime error |
2810 ms |
524288 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
139 ms |
274524 KB |
Output is correct |
2 |
Correct |
77 ms |
274512 KB |
Output is correct |
3 |
Correct |
75 ms |
274764 KB |
Output is correct |
4 |
Correct |
66 ms |
274256 KB |
Output is correct |
5 |
Correct |
68 ms |
274252 KB |
Output is correct |
6 |
Correct |
73 ms |
274228 KB |
Output is correct |
7 |
Correct |
67 ms |
274300 KB |
Output is correct |
8 |
Correct |
69 ms |
274256 KB |
Output is correct |
9 |
Correct |
68 ms |
274200 KB |
Output is correct |
10 |
Correct |
71 ms |
274352 KB |
Output is correct |
11 |
Correct |
67 ms |
274352 KB |
Output is correct |
12 |
Correct |
72 ms |
274384 KB |
Output is correct |
13 |
Correct |
75 ms |
274256 KB |
Output is correct |
14 |
Correct |
68 ms |
274260 KB |
Output is correct |
15 |
Correct |
70 ms |
274260 KB |
Output is correct |
16 |
Correct |
82 ms |
274640 KB |
Output is correct |
17 |
Correct |
114 ms |
278100 KB |
Output is correct |
18 |
Correct |
67 ms |
274388 KB |
Output is correct |
19 |
Correct |
71 ms |
274224 KB |
Output is correct |
20 |
Correct |
68 ms |
274260 KB |
Output is correct |
21 |
Correct |
76 ms |
274260 KB |
Output is correct |
22 |
Correct |
66 ms |
274156 KB |
Output is correct |
23 |
Correct |
66 ms |
274296 KB |
Output is correct |
24 |
Correct |
73 ms |
274204 KB |
Output is correct |
25 |
Correct |
71 ms |
274260 KB |
Output is correct |
26 |
Correct |
79 ms |
274364 KB |
Output is correct |
27 |
Correct |
70 ms |
274260 KB |
Output is correct |
28 |
Correct |
434 ms |
297224 KB |
Output is correct |
29 |
Correct |
69 ms |
274284 KB |
Output is correct |
30 |
Correct |
544 ms |
307744 KB |
Output is correct |
31 |
Runtime error |
2364 ms |
524288 KB |
Execution killed with signal 9 |
32 |
Halted |
0 ms |
0 KB |
- |