This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #include <x86intrin.h>
#include <bits/stdc++.h>
#include <chrono>
#include <random>
// @author: Vlapos
namespace operators
{
template <typename T1, typename T2>
std::istream &operator>>(std::istream &in, std::pair<T1, T2> &x)
{
in >> x.first >> x.second;
return in;
}
template <typename T1, typename T2>
std::ostream &operator<<(std::ostream &out, std::pair<T1, T2> x)
{
out << x.first << " " << x.second;
return out;
}
template <typename T1>
std::istream &operator>>(std::istream &in, std::vector<T1> &x)
{
for (auto &i : x)
in >> i;
return in;
}
template <typename T1>
std::ostream &operator<<(std::ostream &out, std::vector<T1> &x)
{
for (auto &i : x)
out << i << " ";
return out;
}
template <typename T1>
std::ostream &operator<<(std::ostream &out, std::set<T1> &x)
{
for (auto &i : x)
out << i << " ";
return out;
}
template <typename T1>
std::ostream &operator<<(std::ostream &out, std::multiset<T1> &x)
{
for (auto &i : x)
out << i << " ";
return out;
}
}
// name spaces
using namespace std;
using namespace operators;
// end of name spaces
// defines
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define f first
#define s second
#define uint unsigned int
#define all(vc) vc.begin(), vc.end()
// end of defines
// usefull stuff
void boost()
{
ios_base ::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
inline int getbit(int &x, int &bt) { return (x >> bt) & 1; }
const int dx4[4] = {-1, 0, 0, 1};
const int dy4[4] = {0, -1, 1, 0};
const int dx8[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
const int dy8[8] = {-1, -0, 1, -1, 1, -1, 0, 1};
const ll INF = (1e18) + 500;
const int BIG = (1e9) * 2 + 100;
const int MAXN = (1e5) + 5;
const int MOD7 = (1e9) + 7;
const int MOD9 = (1e9) + 9;
const uint MODFFT = 998244353;
#define int ll
struct segTree
{
struct Node
{
int val = 0, op = 0;
};
vector<Node> tree;
int sz = 0;
void init(int n)
{
sz = 1;
while (sz < n)
sz *= 2;
tree.resize(2 * sz);
}
void build(vector<int> &a, int v, int lv, int rv)
{
if (rv - lv == 1)
{
if (lv < a.size())
tree[v].val = a[lv];
tree[v].val -= BIG;
return;
}
int m = (lv + rv) >> 1;
build(a, v * 2 + 1, lv, m);
build(a, v * 2 + 2, m, rv);
tree[v].val = max(tree[v * 2 + 1].val, tree[v * 2 + 2].val);
}
void build(vector<int> &a)
{
init(a.size());
build(a, 0, 0, sz);
}
void push(int v, int lv, int rv)
{
if (rv - lv == 1 or tree[v].op == 0)
return;
tree[v * 2 + 1].val += tree[v].op;
tree[v * 2 + 2].val += tree[v].op;
tree[v * 2 + 1].op += tree[v].op;
tree[v * 2 + 2].op += tree[v].op;
tree[v].op = 0;
}
void upd(int l, int r, int val, int v, int lv, int rv)
{
push(v, lv, rv);
if (l <= lv and rv <= r)
{
tree[v].val += val;
tree[v].op += val;
return;
}
if (rv <= l or r <= lv)
return;
int m = (lv + rv) >> 1;
upd(l, r, val, v * 2 + 1, lv, m);
upd(l, r, val, v * 2 + 2, m, rv);
tree[v].val = max(tree[v * 2 + 1].val, tree[v * 2 + 2].val);
}
void upd(int l, int r, int val)
{
upd(l, r, val, 0, 0, sz);
}
int get(int l, int r, int v, int lv, int rv)
{
push(v, lv, rv);
if (l <= lv and rv <= r)
{
return tree[v].val;
}
if (r <= lv or rv <= l)
return 0;
int m = (lv + rv) >> 1;
return max(get(l, r, v * 2 + 1, lv, m), get(l, r, v * 2 + 2, m, rv));
}
int get(int l, int r)
{
return get(l, r, 0, 0, sz);
}
};
struct test
{
void solve(int testcase)
{
boost();
int n;
cin >> n;
vector<int> a(n);
cin >> a;
vector<vector<pii>> good(n);
{
stack<int> els;
for (int i = 0; i < n; ++i)
{
while (els.size() and a[els.top()] < a[i])
els.pop();
if (els.size())
{
int a = els.top();
int b = i;
if (b + (b - a) < n)
{
good[a].pb({a, b});
}
}
els.push(i);
}
}
{
stack<int> els;
for (int i = n - 1; i >= 0; --i)
{
while (els.size() and a[els.top()] < a[i])
els.pop();
if (els.size())
{
int b = els.top();
int a = i;
if (b + (b - a) < n)
good[a].pb({a, b});
}
els.push(i);
}
}
int q;
cin >> q;
vector<vector<pii>> qrs(n);
for (int i = 0; i < q; ++i)
{
int l, r;
cin >> l >> r;
--l;
qrs[l].pb({r, i});
}
// cout << "!\n";
// exit(0);
vector<int> ans(q);
segTree tree;
tree.build(a);
multiset<pair<pii, int>> vals;
vals.insert({{0, n}, -BIG});
vals.insert({{n, n + 1}, BIG});
for (int l = n - 1; l >= 0; --l)
{
for (auto [p1, p2] : good[l])
{
int cur = a[p1] + a[p2];
int cl = p2 + (p2 - p1);
auto it = vals.upper_bound({{cl, BIG}, cur});
int cr = 0;
while (it->s <= cur)
{
cr = it->f.s;
tree.upd(it->f.f, it->f.s, cur - it->s);
it = vals.erase(it);
}
it = prev(it);
if (it->s <= cur)
{
cr = max(cr, it->f.s);
tree.upd(cl, it->f.s, cur - it->s);
vals.insert({{cl, cr}, cur});
auto V = *it;
vals.erase(it);
V.f.s = cl;
if (V.f.f != V.f.s)
vals.insert(V);
}
}
for (auto [r, id] : qrs[l])
ans[id] = tree.get(l, r);
}
for (int i = 0; i < q; ++i)
cout << ans[i] << "\n";
}
};
main()
{
boost();
int q = 1;
// cin >> q;
for (int i = 0; i < q; i++)
{
test t;
t.solve(i);
}
return 0;
}
//[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]//
// //
// Coded by Der_Vlἀpos //
// //
//[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]//
Compilation message (stderr)
jumps.cpp: In member function 'void segTree::build(std::vector<long long int>&, long long int, long long int, long long int)':
jumps.cpp:124:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
124 | if (lv < a.size())
| ~~~^~~~~~~~~~
jumps.cpp: At global scope:
jumps.cpp:293:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
293 | main()
| ^~~~
# | 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... |