// #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
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 |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
600 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
600 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
186 ms |
23480 KB |
Output is correct |
12 |
Correct |
195 ms |
23340 KB |
Output is correct |
13 |
Correct |
196 ms |
23280 KB |
Output is correct |
14 |
Correct |
195 ms |
23340 KB |
Output is correct |
15 |
Correct |
187 ms |
23376 KB |
Output is correct |
16 |
Correct |
188 ms |
22868 KB |
Output is correct |
17 |
Correct |
202 ms |
22748 KB |
Output is correct |
18 |
Correct |
199 ms |
22628 KB |
Output is correct |
19 |
Correct |
197 ms |
23408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
216 ms |
29888 KB |
Output is correct |
2 |
Correct |
143 ms |
38224 KB |
Output is correct |
3 |
Correct |
143 ms |
27480 KB |
Output is correct |
4 |
Correct |
249 ms |
29776 KB |
Output is correct |
5 |
Correct |
215 ms |
29880 KB |
Output is correct |
6 |
Correct |
209 ms |
29784 KB |
Output is correct |
7 |
Correct |
224 ms |
29776 KB |
Output is correct |
8 |
Correct |
215 ms |
30036 KB |
Output is correct |
9 |
Correct |
213 ms |
29892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
600 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
186 ms |
23480 KB |
Output is correct |
12 |
Correct |
195 ms |
23340 KB |
Output is correct |
13 |
Correct |
196 ms |
23280 KB |
Output is correct |
14 |
Correct |
195 ms |
23340 KB |
Output is correct |
15 |
Correct |
187 ms |
23376 KB |
Output is correct |
16 |
Correct |
188 ms |
22868 KB |
Output is correct |
17 |
Correct |
202 ms |
22748 KB |
Output is correct |
18 |
Correct |
199 ms |
22628 KB |
Output is correct |
19 |
Correct |
197 ms |
23408 KB |
Output is correct |
20 |
Correct |
216 ms |
29888 KB |
Output is correct |
21 |
Correct |
143 ms |
38224 KB |
Output is correct |
22 |
Correct |
143 ms |
27480 KB |
Output is correct |
23 |
Correct |
249 ms |
29776 KB |
Output is correct |
24 |
Correct |
215 ms |
29880 KB |
Output is correct |
25 |
Correct |
209 ms |
29784 KB |
Output is correct |
26 |
Correct |
224 ms |
29776 KB |
Output is correct |
27 |
Correct |
215 ms |
30036 KB |
Output is correct |
28 |
Correct |
213 ms |
29892 KB |
Output is correct |
29 |
Correct |
1004 ms |
98204 KB |
Output is correct |
30 |
Correct |
882 ms |
124092 KB |
Output is correct |
31 |
Correct |
831 ms |
92824 KB |
Output is correct |
32 |
Correct |
1002 ms |
102920 KB |
Output is correct |
33 |
Correct |
1052 ms |
102800 KB |
Output is correct |
34 |
Correct |
981 ms |
100688 KB |
Output is correct |
35 |
Correct |
1010 ms |
100160 KB |
Output is correct |
36 |
Correct |
1065 ms |
100144 KB |
Output is correct |
37 |
Correct |
1002 ms |
101660 KB |
Output is correct |
38 |
Correct |
809 ms |
105420 KB |
Output is correct |
39 |
Correct |
847 ms |
105428 KB |
Output is correct |
40 |
Correct |
814 ms |
102236 KB |
Output is correct |
41 |
Correct |
756 ms |
101732 KB |
Output is correct |
42 |
Correct |
764 ms |
101972 KB |
Output is correct |
43 |
Correct |
800 ms |
103320 KB |
Output is correct |
44 |
Correct |
793 ms |
105712 KB |
Output is correct |
45 |
Correct |
791 ms |
105808 KB |
Output is correct |
46 |
Correct |
842 ms |
102484 KB |
Output is correct |
47 |
Correct |
785 ms |
102128 KB |
Output is correct |
48 |
Correct |
786 ms |
102228 KB |
Output is correct |
49 |
Correct |
798 ms |
104336 KB |
Output is correct |
50 |
Correct |
862 ms |
105920 KB |
Output is correct |
51 |
Correct |
881 ms |
106064 KB |
Output is correct |
52 |
Correct |
866 ms |
103528 KB |
Output is correct |
53 |
Correct |
843 ms |
103264 KB |
Output is correct |
54 |
Correct |
853 ms |
103360 KB |
Output is correct |
55 |
Correct |
863 ms |
104840 KB |
Output is correct |