#include <bits/stdc++.h>
/*
"Tree segtree" (HLD)
Backwards?
Intervals overlap -> fine
Intervals do not overlap -> single point
*/
struct node{
long long delay,min,max,start,back;//max(min(x+delay,max),min) (max is when num. leaps start increasing)
node(long long a=0,long long b=-1e18,long long c=1e18,long long d=1e18,long long e=0){
delay=a,min=b,max=c,start=d,back=e;
}
node combine(node oth){
node undel=node(delay+oth.delay,min+oth.delay,max+oth.delay,start+oth.delay,back+oth.back);//put oth's delay at start
long long fmin=std::max(std::min(undel.min,oth.max),oth.min);//clamp to next one
long long fmax=std::max(std::min(undel.max,oth.max),oth.min);//clamp to next one
long long fstart=min==max?undel.start:std::min(std::max(oth.start,undel.min),undel.max);//clamp to this one if there is a range, otherwise just use this start
long long fback=std::min((long long)1e18,back+oth.back+std::max(0ll,undel.min-oth.start));//cap for overflow
return node(delay+oth.delay,fmin,fmax,fstart,fback);
}
long long nleaps(long long st,long long et){
long long stdest=std::max(std::min(st+delay,max),min);
return std::max((st+delay)-start,0ll)+std::max(stdest-et,0ll)+back;
}
};
struct segtree{
int size;
std::vector<node> nodes,rnodes;//one forward, one reverse
segtree(int n){
size=n;
nodes.resize(2*n-1);
rnodes.resize(2*n-1);
}
void update(int p,node val,int nl,int nr,int ni){
if(p<nl||p>=nr)return;
if(nl+1>=nr){
nodes[ni]=val;
rnodes[ni]=val;
return;
}
int nm=(nl+nr)/2;
update(p,val,nl,nm,ni+1);
update(p,val,nm,nr,ni+2*(nm-nl));
nodes[ni]=nodes[ni+1].combine(nodes[ni+2*(nm-nl)]);
rnodes[ni]=rnodes[ni+2*(nm-nl)].combine(rnodes[ni+1]);
}
void update(int p,node val){update(p,val,0,size,0);}
node query(int l,int r,int nl,int nr,int ni){
if(l>=nr||r<=nl)return node();
if(l<=nl&&r>=nr)return nodes[ni];
int nm=(nl+nr)/2;
node res1=query(l,r,nl,nm,ni+1);
node res2=query(l,r,nm,nr,ni+2*(nm-nl));
return res1.combine(res2);
}
node query(int l,int r){return query(l,r,0,size,0);}
node rquery(int l,int r,int nl,int nr,int ni){
if(l>=nr||r<=nl)return node();
if(l<=nl&&r>=nr)return rnodes[ni];
int nm=(nl+nr)/2;
node res1=rquery(l,r,nl,nm,ni+1);
node res2=rquery(l,r,nm,nr,ni+2*(nm-nl));
return res2.combine(res1);
}
node rquery(int l,int r){return rquery(l,r,0,size,0);}
};
int main(){
std::ios::sync_with_stdio(0);
std::cin.tie(0);
int n,q;
std::cin>>n>>q;
segtree seg(n);
for(int i=0;i<n-1;i++){
int a,b;
std::cin>>a>>b;
seg.update(i,node(1,a+1,b,b));
}
for(int i=0;i<q;i++){
int x;
std::cin>>x;
if(x==1){
int p,s,e;
std::cin>>p>>s>>e;
p--;
seg.update(p,node(1,s+1,e,e));
}else{
int a,b,c,d;
std::cin>>a>>b>>c>>d;
a--,c--;
if(a<c){
std::cout<<seg.query(a,c).nleaps(b,d)<<'\n';
}else{
std::cout<<seg.rquery(c,a).nleaps(b,d)<<'\n';
}
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
460 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
600 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
660 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
2 ms |
860 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
472 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
640 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
2 ms |
600 KB |
Output is correct |
27 |
Correct |
1 ms |
464 KB |
Output is correct |
28 |
Correct |
1 ms |
656 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
2 ms |
472 KB |
Output is correct |
35 |
Correct |
1 ms |
604 KB |
Output is correct |
36 |
Correct |
1 ms |
604 KB |
Output is correct |
37 |
Correct |
1 ms |
604 KB |
Output is correct |
38 |
Correct |
1 ms |
604 KB |
Output is correct |
39 |
Correct |
1 ms |
604 KB |
Output is correct |
40 |
Correct |
1 ms |
604 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
480 ms |
63064 KB |
Output is correct |
2 |
Correct |
424 ms |
60156 KB |
Output is correct |
3 |
Correct |
418 ms |
60756 KB |
Output is correct |
4 |
Correct |
406 ms |
59220 KB |
Output is correct |
5 |
Correct |
428 ms |
63056 KB |
Output is correct |
6 |
Correct |
417 ms |
62036 KB |
Output is correct |
7 |
Correct |
424 ms |
63572 KB |
Output is correct |
8 |
Correct |
451 ms |
66020 KB |
Output is correct |
9 |
Correct |
404 ms |
60024 KB |
Output is correct |
10 |
Correct |
463 ms |
63440 KB |
Output is correct |
11 |
Correct |
430 ms |
63060 KB |
Output is correct |
12 |
Correct |
442 ms |
66744 KB |
Output is correct |
13 |
Correct |
455 ms |
67664 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
460 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
600 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
660 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
2 ms |
860 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
472 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
640 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
2 ms |
600 KB |
Output is correct |
27 |
Correct |
1 ms |
464 KB |
Output is correct |
28 |
Correct |
1 ms |
656 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
2 ms |
472 KB |
Output is correct |
35 |
Correct |
1 ms |
604 KB |
Output is correct |
36 |
Correct |
1 ms |
604 KB |
Output is correct |
37 |
Correct |
1 ms |
604 KB |
Output is correct |
38 |
Correct |
1 ms |
604 KB |
Output is correct |
39 |
Correct |
1 ms |
604 KB |
Output is correct |
40 |
Correct |
1 ms |
604 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
480 ms |
63064 KB |
Output is correct |
43 |
Correct |
424 ms |
60156 KB |
Output is correct |
44 |
Correct |
418 ms |
60756 KB |
Output is correct |
45 |
Correct |
406 ms |
59220 KB |
Output is correct |
46 |
Correct |
428 ms |
63056 KB |
Output is correct |
47 |
Correct |
417 ms |
62036 KB |
Output is correct |
48 |
Correct |
424 ms |
63572 KB |
Output is correct |
49 |
Correct |
451 ms |
66020 KB |
Output is correct |
50 |
Correct |
404 ms |
60024 KB |
Output is correct |
51 |
Correct |
463 ms |
63440 KB |
Output is correct |
52 |
Correct |
430 ms |
63060 KB |
Output is correct |
53 |
Correct |
442 ms |
66744 KB |
Output is correct |
54 |
Correct |
455 ms |
67664 KB |
Output is correct |
55 |
Correct |
0 ms |
344 KB |
Output is correct |
56 |
Correct |
446 ms |
59456 KB |
Output is correct |
57 |
Correct |
447 ms |
55892 KB |
Output is correct |
58 |
Correct |
446 ms |
60784 KB |
Output is correct |
59 |
Correct |
406 ms |
61528 KB |
Output is correct |
60 |
Correct |
392 ms |
57604 KB |
Output is correct |
61 |
Correct |
402 ms |
62804 KB |
Output is correct |
62 |
Correct |
431 ms |
62544 KB |
Output is correct |
63 |
Correct |
451 ms |
62420 KB |
Output is correct |
64 |
Correct |
438 ms |
63016 KB |
Output is correct |
65 |
Correct |
452 ms |
60120 KB |
Output is correct |
66 |
Correct |
399 ms |
60348 KB |
Output is correct |
67 |
Correct |
406 ms |
62544 KB |
Output is correct |
68 |
Correct |
396 ms |
57988 KB |
Output is correct |
69 |
Correct |
413 ms |
64108 KB |
Output is correct |
70 |
Correct |
402 ms |
57836 KB |
Output is correct |
71 |
Correct |
386 ms |
55536 KB |
Output is correct |
72 |
Correct |
454 ms |
57432 KB |
Output is correct |
73 |
Correct |
436 ms |
62688 KB |
Output is correct |
74 |
Correct |
482 ms |
62896 KB |
Output is correct |
75 |
Correct |
416 ms |
63904 KB |
Output is correct |
76 |
Correct |
413 ms |
64472 KB |
Output is correct |
77 |
Correct |
0 ms |
348 KB |
Output is correct |