This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define read() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define day() time_t now = time(0);char* x = ctime(&now);cerr<<"Right Now Is : "<<x<<"\n"
#define int long long
#define ii pair<int,int>
#define X first
#define Y second
const long long MAX = (int)5e5 + 5;
const long long INF = (int)1e9;
const long long MOD = (int)1e9 + 7;
int n,a[MAX];
int q;
struct node{
int l,r,id;
}qry[MAX];
ii st[MAX];
int lazy[MAX];
ii operator + (const ii &a,const ii &b){
return {max(a.X,b.X),max(a.Y,b.Y)};
}
void lazyupdate(int id,int l,int r){
if(lazy[id] == 0)return;
st[id].X = max(st[id].X,st[id].Y + lazy[id]);
if(l != r){
lazy[id << 1] = max(lazy[id << 1],lazy[id]);
lazy[id << 1 | 1] = max(lazy[id << 1 | 1],lazy[id]);
}
lazy[id] = 0;
}
void build(int id = 1,int l = 1,int r = n){
if(l == r){
st[id] = {a[l],a[l]};
return;
}
int m = l + r >> 1;
build(id << 1,l,m);
build(id << 1 | 1,m + 1,r);
st[id] = st[id << 1] + st[id << 1 | 1];
}
void update(int u,int v,int val,int id = 1,int l = 1,int r = n){
//cout << u << " " << v << " " << val << "\n";
lazyupdate(id,l,r);
if(u > r || v < l)return;
if(u <= l && r <= v){
lazy[id] = val;
lazyupdate(id,l,r);
return;
}
int m = l + r >> 1;
update(u,v,val,id << 1,l,m);
update(u,v,val,id << 1 | 1,m + 1,r);
st[id] = st[id << 1] + st[id << 1 | 1];
}
int get(int u,int v,int id = 1,int l = 1,int r = n){
lazyupdate(id,l,r);
if(u > r || v < l)return 0;
if(u <= l && r <= v)return st[id].X;
int m = l + r >> 1;
return max(get(u,v,id << 1,l,m),get(u,v,id << 1 | 1,m + 1,r));
}
signed main(){
read();
cin >> n;
for(int i = 1;i <= n;i++)cin >> a[i];
build();
cin >> q;
for(int i = 1,l,r;i <= q;i++){
cin >> l >> r;
qry[i] = {l,r,i};
}
sort(qry + 1,qry + 1 + q,[&](const node &a,const node &b){
return a.l > b.l;
});
//cout << "hi\n";
stack<int> stt;
int ri = n;
for(int i = 1;i <= q;i++){
int &l = qry[i].l;
int r = qry[i].r;
int id = qry[i].id;
//cout << l << " " << r << " " << id << "\n";
while(ri >= l){
//cout << ri << "\n";
while(!stt.empty()){
int u = stt.top();
//cout << u << " " << ri << ' ' << (u + u - ri) << " " << n << " " << a[u] + a[ri] << "\n";
update(u + (u - ri),n,a[u] + a[ri]);
//cout << get(5,5) << '\n';
if(a[u] >= a[ri])break;
stt.pop();
}
stt.push(ri--);
// cout << l << " " << ri << '\n';
// break;
}
// cout << l << " " << ri << '\n';
// break;
//cout << get(l,r) << '\n';
l = get(l,r);
}
sort(qry + 1,qry + 1 + q,[&](const node &a,const node &b){
return a.id < b.id;
});
for(int i = 1;i <= q;i++){
cout << qry[i].l << "\n";
}
}
Compilation message (stderr)
jumps.cpp: In function 'void build(long long int, long long int, long long int)':
jumps.cpp:45:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
45 | int m = l + r >> 1;
| ~~^~~
jumps.cpp: In function 'void update(long long int, long long int, long long int, long long int, long long int, long long int)':
jumps.cpp:63:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
63 | int m = l + r >> 1;
| ~~^~~
jumps.cpp: In function 'long long int get(long long int, long long int, long long int, long long int, long long int)':
jumps.cpp:72:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
72 | int m = l + r >> 1;
| ~~^~~
jumps.cpp: In function 'int main()':
jumps.cpp:101:7: warning: unused variable 'id' [-Wunused-variable]
101 | int id = qry[i].id;
| ^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |