#include <bits/stdc++.h>
#define fo(i, d, c) for (int i = d; i <= c; i++)
#define fod(i, c, d) for (int i = c; i >= d; i--)
#define maxn 1000010
#define N 1010
#define fi first
#define se second
#define pb emplace_back
#define en cout << "\n";
#define int long long
#define inf (int)1e18
#define pii pair<int, int>
#define vii vector<pii>
#define lb(x) x & -x
#define bit(i, j) ((i >> j) & 1)
#define offbit(i, j) (i ^ (1LL << j))
#define onbit(i, j) (i | (1LL << j))
#define vi vector<int>
template <typename T1, typename T2>
bool minimize(T1 &a, T2 b)
{
if (a > b)
{
a = b;
return true;
}
return false;
}
template <typename T1, typename T2>
bool maximize(T1 &a, T2 b)
{
if (a < b)
{
a = b;
return true;
}
return false;
}
using namespace std;
const int nsqrt = 450;
const int mod = 1e9 + 7;
int n,a[maxn],l[maxn],r[maxn],q;
struct node
{
int maxx,val,lazy;
}st[4 * maxn];
void build(int id,int l,int r)
{
st[id].lazy = 0;
st[id].maxx = 0;
if(l == r)
{
st[id].val = a[l];
return;
}
int mid = l + r >> 1;
build(id << 1,l,mid);
build(id << 1 | 1,mid + 1,r);
st[id].val = max(st[id << 1].val,st[id << 1 | 1].val);
}
void down(int id)
{
if(st[id].lazy == 0) return;
maximize(st[id << 1].lazy,st[id].lazy);
maximize(st[id << 1].maxx,st[id << 1].val + st[id << 1].lazy);
maximize(st[id << 1 | 1].lazy,st[id].lazy);
maximize(st[id << 1 | 1].val,st[id << 1 | 1].val + st[id << 1 | 1].lazy);
st[id].lazy = 0;
}
void up(int id,int l,int r,int u,int v,int val)
{
if(l > v or r < u) return;
if(u <= l and r <= v)
{
maximize(st[id].maxx,st[id].val + val);
maximize(st[id].lazy,val);
return ;
}
down(id);
int mid = l + r >> 1;
up(id << 1,l,mid,u,v,val);
up(id << 1 | 1,mid + 1,r,u,v,val);
st[id].maxx = max(st[id << 1].maxx,st[id << 1 | 1].maxx);
}
int get(int id,int l,int r,int u,int v)
{
if(l > v or r < u) return 0;
if(u <= l and r <= v) return st[id].maxx;
down(id);
int mid = (l + r) >> 1;
return max(get(id << 1,l,mid,u,v),get(id << 1 | 1,mid + 1,r,u,v));
}
void L(int a[], int n, int ans[])
{
stack<int> st;
fo(i, 1, n)
{
while (st.size() and a[st.top()] < a[i])
st.pop();
ans[i] = (st.empty() ? 0 : st.top());
st.push(i);
}
}
void R(int a[], int n, int ans[])
{
stack<int> st;
fod(i, n,1)
{
while (st.size() and a[st.top()] < a[i])
st.pop();
ans[i] = (st.empty() ? n + 1 : st.top());
st.push(i);
}
}
vi ke[maxn];
int res[maxn];
vii qry[maxn];
main()
{
#define name "TASK"
if (fopen(name ".inp", "r"))
{
freopen(name ".inp", "r", stdin);
freopen(name ".out", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n ;
fo(i,1,n) cin >> a[i];
build(1,1,n);
L(a,n,l);
R(a,n,r);
fo(i,1,n)
{
if(r[i] != n + 1)
ke[i].pb(r[i]);
if(l[i] != 0)
ke[l[i]].pb(i);
}
cin >> q;
fo(i,1,q)
{
int l,r;
cin >> l >> r;
qry[l].pb(r,i);
}
fod(i,n,1)
{
for(int j : ke[i])
{
int k = 2 * j - i;
up(1,1,n,k,n,a[i] + a[j]);
}
for(auto [j,id] : qry[i])
{
res[id] = get(1,1,n,i,j);
}
}
fo(i,1,q) cout << res[i] << "\n";
}
Compilation message
jumps.cpp: In function 'void build(long long int, long long int, long long int)':
jumps.cpp:56:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
56 | int mid = l + r >> 1;
| ~~^~~
jumps.cpp: In function 'void up(long long int, long long int, long long int, long long int, long long int, long long int)':
jumps.cpp:80:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
80 | int mid = l + r >> 1;
| ~~^~~
jumps.cpp: At global scope:
jumps.cpp:118:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
118 | main()
| ^~~~
jumps.cpp: In function 'int main()':
jumps.cpp:123:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
123 | freopen(name ".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
jumps.cpp:124:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
124 | freopen(name ".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
55900 KB |
Output is correct |
2 |
Incorrect |
9 ms |
55900 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
55900 KB |
Output is correct |
2 |
Incorrect |
9 ms |
55900 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
121 ms |
77652 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
55900 KB |
Output is correct |
2 |
Incorrect |
9 ms |
55900 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |