#include <iostream>
#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 2e5+5;
long long seg[4*MAXN];
long long temph[4*MAXN];
long long n,q;
long long lazy[4*MAXN];
long long h[MAXN];
long long a[MAXN];
long long ans[MAXN];
long long b[MAXN];
vector<int> add[MAXN];
vector<int> rem[MAXN];
vector<pair<int,int>> v1[MAXN];
bool nxt = false;
void build(long long curr,long long l,long long r){
if(l==r){
seg[curr] =-987654321987654321;
lazy[curr] =-987654321987654321;
temph[curr] = 987654321987654321;
return;
}
long long mid = (l+r)/2;
build(2*curr,l,mid);
build(2*curr+1,mid+1,r);
seg[curr] = max(seg[2*curr],seg[2*curr+1]);
lazy[curr] = max(lazy[2*curr],lazy[2*curr+1]);
temph[curr] = min(temph[2*curr],temph[2*curr+1]);
}
void pushdown(long long curr,long long l,long long r){
lazy[2*curr] = max(lazy[curr],lazy[2*curr]);
lazy[2*curr+1] = max(lazy[curr],lazy[2*curr+1]);
seg[2*curr] = max(seg[2*curr],lazy[2*curr]-temph[2*curr]);
seg[2*curr+1] = max(seg[2*curr+1],lazy[2*curr+1]-temph[2*curr+1]);
lazy[curr] = -987654321987654321;
}
void pointupdate(long long curr,long long l,long long r,long long ind,long long x){
if(l==r){
temph[curr] = x;
lazy[curr] = -987654321987654321;
return;
}
pushdown(curr,l,r);
long long mid = (l+r)/2;
if(ind<=mid){
pointupdate(2*curr,l,mid,ind,x);
}else{
pointupdate(2*curr+1,mid+1,r,ind,x);
}
seg[curr] = max(seg[2*curr],seg[2*curr+1]);
temph[curr] = min(temph[2*curr],temph[2*curr+1]);
if(curr == 4){
//cout<<ind<<" "<<x<<" "<<temph[curr]<<" "<<temph[2*curr]<<" "<<temph[2*curr+1]<<endl;
}
}
void rangeupdate(long long curr,long long l,long long r,long long tl,long long tr,long long val){
if(l>tr||r<tl){
return;
}
if(tl<=l && r<=tr){
lazy[curr] = max(lazy[curr],val);
seg[curr] = max(seg[curr],val-temph[curr]);
// cout<<curr<<" "<<123<<" "<<l<<" "<<r<<" "<<temph[2*2*curr]<<endl;
return;
}
long long mid =(l+r)/2;
pushdown(curr,l,r);
rangeupdate(2*curr,l,mid,tl,tr,val);
rangeupdate(2*curr+1,mid+1,r,tl,tr,val);
seg[curr] = max(seg[2*curr],seg[2*curr+1]);
}
long long query(long long curr,long long l,long long r,long long tl,long long tr){
// cout<<l<<" "<<r<<endl;
if(l>tr||r<tl){
return -987654321987654321;
}
if(tl<=l &&r<=tr){
return seg[curr];
}
pushdown(curr,l,r);
long long mid = (l+r)/2;
return max(query(2*curr,l,mid,tl,tr),query(2*curr+1,mid+1,r,tl,tr));
}
int main(){
cin>>n;
for(long long i=1;i<=n;i++){
cin>>h[i]>>a[i]>>b[i];
if(a[i]+i<MAXN){
add[a[i]+i].push_back(i);
}
if(b[i]+i+1<MAXN){
rem[b[i]+i+1].push_back(i);
}
//cout<<a[i]+i<<" "<<b[i]+i+1<<endl;
}
cin>>q;
for(long long i=1;i<=q;i++){
ans[i] = -1;
long long l,r;
cin>>l>>r;
v1[r].push_back(make_pair(l,i));
}
build(1,1,n);
// cout<<query(1,1,n,1,2)<<endl;
for(long long i=1;i<=n;i++){
for(long long x:add[i]){
pointupdate(1,1,n,x,h[x]);
}
for(long long x:rem[i]){
pointupdate(1,1,n,x,987654321987654321);
}
//cout<<123<<endl;
if(i-a[i]>=1){
rangeupdate(1,1,n,max((long long)1,i-b[i]),i-a[i],h[i]);
// cout<<" "<<max((long long)1,i-b[i])<<" "<<i-a[i]<<" "<<h[i]<<endl;
}
for(auto x:v1[i]){
//
ans[x.second] = max(ans[x.second],query(1,1,n,x.first,i));
// cout<<x.first<<" "<<i<<" "<<query(1,1,n,x.first,i)<<endl;
}
// cout<<query(1,1,n,1,20)<<endl;
}
//reverse
//cout<<ans[1]<<endl;
build(1,1,n);
// cout<<query(1,1,n,1,2)<<endl;
for(long long i=1;i<=n;i++){
h[i] = 1000000000-h[i];
}
nxt = true;
for(long long i=1;i<=n;i++){
for(long long x:add[i]){
pointupdate(1,1,n,x,h[x]);
}
for(long long x:rem[i]){
pointupdate(1,1,n,x,987654321987654321);
}
//cout<<123<<endl;
if(i-a[i]>=1){
rangeupdate(1,1,n,max((long long)1,i-b[i]),i-a[i],h[i]);
// cout<<" "<<max((long long)1,i-b[i])<<" "<<i-a[i]<<" "<<h[i]<<endl;
}
for(auto x:v1[i]){
//
ans[x.second] = max(ans[x.second],query(1,1,n,x.first,i));
// cout<<x.first<<" "<<i<<" "<<query(1,1,n,x.first,i)<<endl;
}
// cout<<query(1,1,n,1,20)<<endl;
}
for(long long i=1;i<=q;i++){
cout<<ans[i]<<endl;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
14464 KB |
Output is correct |
2 |
Correct |
14 ms |
14464 KB |
Output is correct |
3 |
Correct |
14 ms |
14464 KB |
Output is correct |
4 |
Correct |
15 ms |
14464 KB |
Output is correct |
5 |
Correct |
13 ms |
14464 KB |
Output is correct |
6 |
Correct |
14 ms |
14592 KB |
Output is correct |
7 |
Correct |
15 ms |
14464 KB |
Output is correct |
8 |
Correct |
15 ms |
14464 KB |
Output is correct |
9 |
Correct |
16 ms |
14464 KB |
Output is correct |
10 |
Correct |
14 ms |
14464 KB |
Output is correct |
11 |
Correct |
14 ms |
14464 KB |
Output is correct |
12 |
Correct |
14 ms |
14464 KB |
Output is correct |
13 |
Correct |
14 ms |
14464 KB |
Output is correct |
14 |
Correct |
15 ms |
14464 KB |
Output is correct |
15 |
Correct |
14 ms |
14464 KB |
Output is correct |
16 |
Correct |
15 ms |
14464 KB |
Output is correct |
17 |
Correct |
15 ms |
14464 KB |
Output is correct |
18 |
Correct |
15 ms |
14464 KB |
Output is correct |
19 |
Correct |
14 ms |
14464 KB |
Output is correct |
20 |
Correct |
13 ms |
14464 KB |
Output is correct |
21 |
Correct |
14 ms |
14464 KB |
Output is correct |
22 |
Correct |
16 ms |
14464 KB |
Output is correct |
23 |
Correct |
14 ms |
14464 KB |
Output is correct |
24 |
Correct |
14 ms |
14464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
14464 KB |
Output is correct |
2 |
Correct |
14 ms |
14464 KB |
Output is correct |
3 |
Correct |
14 ms |
14464 KB |
Output is correct |
4 |
Correct |
15 ms |
14464 KB |
Output is correct |
5 |
Correct |
13 ms |
14464 KB |
Output is correct |
6 |
Correct |
14 ms |
14592 KB |
Output is correct |
7 |
Correct |
15 ms |
14464 KB |
Output is correct |
8 |
Correct |
15 ms |
14464 KB |
Output is correct |
9 |
Correct |
16 ms |
14464 KB |
Output is correct |
10 |
Correct |
14 ms |
14464 KB |
Output is correct |
11 |
Correct |
14 ms |
14464 KB |
Output is correct |
12 |
Correct |
14 ms |
14464 KB |
Output is correct |
13 |
Correct |
14 ms |
14464 KB |
Output is correct |
14 |
Correct |
15 ms |
14464 KB |
Output is correct |
15 |
Correct |
14 ms |
14464 KB |
Output is correct |
16 |
Correct |
15 ms |
14464 KB |
Output is correct |
17 |
Correct |
15 ms |
14464 KB |
Output is correct |
18 |
Correct |
15 ms |
14464 KB |
Output is correct |
19 |
Correct |
14 ms |
14464 KB |
Output is correct |
20 |
Correct |
13 ms |
14464 KB |
Output is correct |
21 |
Correct |
14 ms |
14464 KB |
Output is correct |
22 |
Correct |
16 ms |
14464 KB |
Output is correct |
23 |
Correct |
14 ms |
14464 KB |
Output is correct |
24 |
Correct |
14 ms |
14464 KB |
Output is correct |
25 |
Correct |
474 ms |
19868 KB |
Output is correct |
26 |
Correct |
64 ms |
15352 KB |
Output is correct |
27 |
Correct |
689 ms |
22216 KB |
Output is correct |
28 |
Correct |
656 ms |
22392 KB |
Output is correct |
29 |
Correct |
456 ms |
19960 KB |
Output is correct |
30 |
Correct |
432 ms |
19648 KB |
Output is correct |
31 |
Correct |
606 ms |
21368 KB |
Output is correct |
32 |
Correct |
636 ms |
22344 KB |
Output is correct |
33 |
Correct |
619 ms |
21752 KB |
Output is correct |
34 |
Correct |
327 ms |
18296 KB |
Output is correct |
35 |
Correct |
641 ms |
22136 KB |
Output is correct |
36 |
Correct |
640 ms |
22264 KB |
Output is correct |
37 |
Correct |
377 ms |
18556 KB |
Output is correct |
38 |
Correct |
639 ms |
21496 KB |
Output is correct |
39 |
Correct |
100 ms |
15736 KB |
Output is correct |
40 |
Correct |
654 ms |
21240 KB |
Output is correct |
41 |
Correct |
461 ms |
19576 KB |
Output is correct |
42 |
Correct |
637 ms |
21240 KB |
Output is correct |
43 |
Correct |
227 ms |
16836 KB |
Output is correct |
44 |
Correct |
649 ms |
21436 KB |
Output is correct |
45 |
Correct |
120 ms |
15896 KB |
Output is correct |
46 |
Correct |
660 ms |
21368 KB |
Output is correct |
47 |
Correct |
171 ms |
16376 KB |
Output is correct |
48 |
Correct |
644 ms |
21368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
587 ms |
35960 KB |
Output is correct |
2 |
Correct |
704 ms |
36080 KB |
Output is correct |
3 |
Correct |
468 ms |
35576 KB |
Output is correct |
4 |
Correct |
654 ms |
36088 KB |
Output is correct |
5 |
Correct |
299 ms |
28024 KB |
Output is correct |
6 |
Correct |
673 ms |
35956 KB |
Output is correct |
7 |
Correct |
571 ms |
35832 KB |
Output is correct |
8 |
Correct |
666 ms |
35832 KB |
Output is correct |
9 |
Correct |
95 ms |
18808 KB |
Output is correct |
10 |
Correct |
661 ms |
36088 KB |
Output is correct |
11 |
Correct |
422 ms |
29176 KB |
Output is correct |
12 |
Correct |
652 ms |
35960 KB |
Output is correct |
13 |
Correct |
488 ms |
33480 KB |
Output is correct |
14 |
Correct |
455 ms |
33652 KB |
Output is correct |
15 |
Correct |
460 ms |
33660 KB |
Output is correct |
16 |
Correct |
448 ms |
34040 KB |
Output is correct |
17 |
Correct |
481 ms |
33696 KB |
Output is correct |
18 |
Correct |
464 ms |
34128 KB |
Output is correct |
19 |
Correct |
472 ms |
33376 KB |
Output is correct |
20 |
Correct |
451 ms |
33788 KB |
Output is correct |
21 |
Correct |
445 ms |
33376 KB |
Output is correct |
22 |
Correct |
473 ms |
33748 KB |
Output is correct |
23 |
Correct |
466 ms |
33652 KB |
Output is correct |
24 |
Correct |
438 ms |
33784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
14464 KB |
Output is correct |
2 |
Correct |
14 ms |
14464 KB |
Output is correct |
3 |
Correct |
14 ms |
14464 KB |
Output is correct |
4 |
Correct |
15 ms |
14464 KB |
Output is correct |
5 |
Correct |
13 ms |
14464 KB |
Output is correct |
6 |
Correct |
14 ms |
14592 KB |
Output is correct |
7 |
Correct |
15 ms |
14464 KB |
Output is correct |
8 |
Correct |
15 ms |
14464 KB |
Output is correct |
9 |
Correct |
16 ms |
14464 KB |
Output is correct |
10 |
Correct |
14 ms |
14464 KB |
Output is correct |
11 |
Correct |
14 ms |
14464 KB |
Output is correct |
12 |
Correct |
14 ms |
14464 KB |
Output is correct |
13 |
Correct |
14 ms |
14464 KB |
Output is correct |
14 |
Correct |
15 ms |
14464 KB |
Output is correct |
15 |
Correct |
14 ms |
14464 KB |
Output is correct |
16 |
Correct |
15 ms |
14464 KB |
Output is correct |
17 |
Correct |
15 ms |
14464 KB |
Output is correct |
18 |
Correct |
15 ms |
14464 KB |
Output is correct |
19 |
Correct |
14 ms |
14464 KB |
Output is correct |
20 |
Correct |
13 ms |
14464 KB |
Output is correct |
21 |
Correct |
14 ms |
14464 KB |
Output is correct |
22 |
Correct |
16 ms |
14464 KB |
Output is correct |
23 |
Correct |
14 ms |
14464 KB |
Output is correct |
24 |
Correct |
14 ms |
14464 KB |
Output is correct |
25 |
Correct |
474 ms |
19868 KB |
Output is correct |
26 |
Correct |
64 ms |
15352 KB |
Output is correct |
27 |
Correct |
689 ms |
22216 KB |
Output is correct |
28 |
Correct |
656 ms |
22392 KB |
Output is correct |
29 |
Correct |
456 ms |
19960 KB |
Output is correct |
30 |
Correct |
432 ms |
19648 KB |
Output is correct |
31 |
Correct |
606 ms |
21368 KB |
Output is correct |
32 |
Correct |
636 ms |
22344 KB |
Output is correct |
33 |
Correct |
619 ms |
21752 KB |
Output is correct |
34 |
Correct |
327 ms |
18296 KB |
Output is correct |
35 |
Correct |
641 ms |
22136 KB |
Output is correct |
36 |
Correct |
640 ms |
22264 KB |
Output is correct |
37 |
Correct |
377 ms |
18556 KB |
Output is correct |
38 |
Correct |
639 ms |
21496 KB |
Output is correct |
39 |
Correct |
100 ms |
15736 KB |
Output is correct |
40 |
Correct |
654 ms |
21240 KB |
Output is correct |
41 |
Correct |
461 ms |
19576 KB |
Output is correct |
42 |
Correct |
637 ms |
21240 KB |
Output is correct |
43 |
Correct |
227 ms |
16836 KB |
Output is correct |
44 |
Correct |
649 ms |
21436 KB |
Output is correct |
45 |
Correct |
120 ms |
15896 KB |
Output is correct |
46 |
Correct |
660 ms |
21368 KB |
Output is correct |
47 |
Correct |
171 ms |
16376 KB |
Output is correct |
48 |
Correct |
644 ms |
21368 KB |
Output is correct |
49 |
Correct |
587 ms |
35960 KB |
Output is correct |
50 |
Correct |
704 ms |
36080 KB |
Output is correct |
51 |
Correct |
468 ms |
35576 KB |
Output is correct |
52 |
Correct |
654 ms |
36088 KB |
Output is correct |
53 |
Correct |
299 ms |
28024 KB |
Output is correct |
54 |
Correct |
673 ms |
35956 KB |
Output is correct |
55 |
Correct |
571 ms |
35832 KB |
Output is correct |
56 |
Correct |
666 ms |
35832 KB |
Output is correct |
57 |
Correct |
95 ms |
18808 KB |
Output is correct |
58 |
Correct |
661 ms |
36088 KB |
Output is correct |
59 |
Correct |
422 ms |
29176 KB |
Output is correct |
60 |
Correct |
652 ms |
35960 KB |
Output is correct |
61 |
Correct |
488 ms |
33480 KB |
Output is correct |
62 |
Correct |
455 ms |
33652 KB |
Output is correct |
63 |
Correct |
460 ms |
33660 KB |
Output is correct |
64 |
Correct |
448 ms |
34040 KB |
Output is correct |
65 |
Correct |
481 ms |
33696 KB |
Output is correct |
66 |
Correct |
464 ms |
34128 KB |
Output is correct |
67 |
Correct |
472 ms |
33376 KB |
Output is correct |
68 |
Correct |
451 ms |
33788 KB |
Output is correct |
69 |
Correct |
445 ms |
33376 KB |
Output is correct |
70 |
Correct |
473 ms |
33748 KB |
Output is correct |
71 |
Correct |
466 ms |
33652 KB |
Output is correct |
72 |
Correct |
438 ms |
33784 KB |
Output is correct |
73 |
Correct |
1294 ms |
46680 KB |
Output is correct |
74 |
Correct |
712 ms |
40440 KB |
Output is correct |
75 |
Correct |
1367 ms |
47572 KB |
Output is correct |
76 |
Correct |
1587 ms |
49528 KB |
Output is correct |
77 |
Correct |
916 ms |
35828 KB |
Output is correct |
78 |
Correct |
1240 ms |
46472 KB |
Output is correct |
79 |
Correct |
1478 ms |
48508 KB |
Output is correct |
80 |
Correct |
1533 ms |
49504 KB |
Output is correct |
81 |
Correct |
810 ms |
27056 KB |
Output is correct |
82 |
Correct |
1155 ms |
44876 KB |
Output is correct |
83 |
Correct |
1234 ms |
40440 KB |
Output is correct |
84 |
Correct |
1543 ms |
49424 KB |
Output is correct |
85 |
Correct |
984 ms |
42320 KB |
Output is correct |
86 |
Correct |
1373 ms |
46048 KB |
Output is correct |
87 |
Correct |
583 ms |
38776 KB |
Output is correct |
88 |
Correct |
1396 ms |
46520 KB |
Output is correct |
89 |
Correct |
1186 ms |
43896 KB |
Output is correct |
90 |
Correct |
1418 ms |
46584 KB |
Output is correct |
91 |
Correct |
816 ms |
40404 KB |
Output is correct |
92 |
Correct |
1407 ms |
46020 KB |
Output is correct |
93 |
Correct |
618 ms |
38752 KB |
Output is correct |
94 |
Correct |
1405 ms |
45936 KB |
Output is correct |
95 |
Correct |
732 ms |
39672 KB |
Output is correct |
96 |
Correct |
1399 ms |
45880 KB |
Output is correct |