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>
#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)
{
if (l == r)
{
st[id].maxx = 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].maxx = 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].lazy);
maximize(st[id << 1 | 1].lazy, st[id].lazy);
maximize(st[id << 1 | 1].maxx, st[id << 1 | 1].val + st[id].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].lazy, val);
maximize(st[id].maxx, st[id].val + 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 - 2, 1)
{
for (int j : ke[i])
{
int k = 2 * j - i;
if (k <= n)
up(1, 1, n, k, n, a[i] + a[j]);
}
for (auto [j, id] : qry[i])
{
res[id] = get(1, 1, n, i + 2, j);
}
}
fo(i, 1, q) cout << res[i] << "\n";
}
Compilation message (stderr)
jumps.cpp: In function 'void build(long long int, long long int, long long int)':
jumps.cpp:54:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
54 | 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: In function 'long long int get(long long int, long long int, long long int, long long int, long long int)':
jumps.cpp:92:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
92 | int mid = l + r >> 1;
| ~~^~~
jumps.cpp: At global scope:
jumps.cpp:120:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
120 | main()
| ^~~~
jumps.cpp: In function 'int main()':
jumps.cpp:125:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
125 | freopen(name ".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
jumps.cpp:126:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
126 | freopen(name ".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |