/*
Code for problem A by cookiedoth
Generated 09 Aug 2019 at 01.49 P
,##. ,==.
,# #. \ o ',
# # _ _ \ \
# # (_) (_) / ;
`# #' / .'
`##' "=="
¯\_(ツ)_/¯
o_O
-_-
*/
#include <iostream>
#include <fstream>
#include <vector>
#include <set>
#include <map>
#include <bitset>
#include <algorithm>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <functional>
#include <unordered_set>
#include <unordered_map>
#include <string>
#include <queue>
#include <deque>
#include <stack>
#include <complex>
#include <cassert>
#include <random>
#include <cstring>
#include <numeric>
#define ll long long
#define ld long double
#define null NULL
#define all(a) a.begin(), a.end()
#define debug(a) cerr << #a << " = " << a << endl
#define forn(i, n) for (int i = 0; i < n; ++i)
#define sz(a) (int)a.size()
using namespace std;
template<class T> int chkmax(T &a, T b) {
if (b > a) {
a = b;
return 1;
}
return 0;
}
template<class T> int chkmin(T &a, T b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
template<class iterator> void output(iterator begin, iterator end, ostream& out = cerr) {
while (begin != end) {
out << (*begin) << " ";
begin++;
}
out << endl;
}
template<class T> void output(T x, ostream& out = cerr) {
output(x.begin(), x.end(), out);
}
void fast_io() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
const int INF = 1e9;
struct node {
int value, max_arr, max_sum;
node(): value(-INF), max_arr(0), max_sum(-INF) {}
node(int _value, int _max_arr, int _max_sum): value(_value), max_arr(_max_arr), max_sum(_max_sum) {}
};
node operator + (node a, node b) {
return node(-INF, max(a.max_arr, b.max_arr), max(a.max_sum, b.max_sum));
}
void update(node &a) {
chkmax(a.max_sum, a.value + a.max_arr);
}
struct st {
vector<node> t;
int n;
st() {}
void build(int *arr, int v, int tl, int tr) {
if (tl == tr) {
t[v].max_arr = arr[tl];
}
else {
int tm = (tl + tr) >> 1;
build(arr, v * 2, tl, tm);
build(arr, v * 2 + 1, tm + 1, tr);
t[v].max_arr = max(t[v * 2].max_arr, t[v * 2 + 1].max_arr);
}
}
void build(int *arr, int _n) {
n = _n;
t.resize(4 * n);
build(arr, 1, 0, n - 1);
}
void push(int v) {
chkmax(t[v * 2].value, t[v].value);
chkmax(t[v * 2 + 1].value, t[v].value);
::update(t[v * 2]);
::update(t[v * 2 + 1]);
t[v].value = -INF;
}
void update(int l, int r, int value, int v, int tl, int tr) {
if (l > r) {
return;
}
if (l == tl && r == tr) {
chkmax(t[v].value, value);
::update(t[v]);
}
else {
int tm = (tl + tr) >> 1;
push(v);
update(l, min(r, tm), value, v * 2, tl, tm);
update(max(l, tm + 1), r, value, v * 2 + 1, tm + 1, tr);
t[v] = t[v * 2] + t[v * 2 + 1];
}
}
int get(int l, int r, int v, int tl, int tr) {
if (l > r) {
return -INF;
}
if (l == tl && r == tr) {
return t[v].max_sum;
}
else {
int tm = (tl + tr) >> 1;
push(v);
int res_l = get(l, min(r, tm), v * 2, tl, tm);
int res_r = get(max(l, tm + 1), r, v * 2 + 1, tm + 1, tr);
return max(res_l, res_r);
}
}
void update(int l, int r, int val) {
update(l, r, val, 1, 0, n - 1);
}
int get(int l, int r) {
int res = get(l, r, 1, 0, n - 1);
return res;
}
};
const int mx = 5e5 + 228;
int n, q, arr[mx], ans[mx];
vector<vector<pair<int, int> > > c_data, queries;
void read_a() {
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> arr[i];
}
queries.resize(n);
cin >> q;
for (int i = 0; i < q; ++i) {
int l, r;
cin >> l >> r;
l--;
r--;
queries[l].emplace_back(r, i);
}
}
void add_ab(int a, int b) {
int c = b + (b - a);
if (c < n) {
c_data[a].emplace_back(c, arr[a] + arr[b]);
}
}
void build_c_data() {
c_data.resize(n);
vector<int> st;
for (int i = 0; i < n; ++i) {
while (!st.empty()) {
int pos = st.back();
add_ab(pos, i);
if (arr[pos] <= arr[i]) {
st.pop_back();
}
else {
break;
}
}
st.push_back(i);
}
}
//int value[mx];
st t;
void scanline() {
t.build(arr, n);
for (int i = n - 1; i >= 0; --i) {
for (auto pp : c_data[i]) {
t.update(pp.first, n - 1, pp.second);
}
for (auto pp : queries[i]) {
ans[pp.second] = t.get(i, pp.first);
}
}
}
void print_ans() {
for (int i = 0; i < q; ++i) {
cout << ans[i] << '\n';
}
}
signed main() {
fast_io();
read_a();
build_c_data();
scanline();
print_ans();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
380 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
3 ms |
380 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
3 ms |
524 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
380 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
3 ms |
380 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
3 ms |
524 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
351 ms |
18844 KB |
Output is correct |
12 |
Correct |
340 ms |
18860 KB |
Output is correct |
13 |
Correct |
405 ms |
18924 KB |
Output is correct |
14 |
Correct |
349 ms |
19040 KB |
Output is correct |
15 |
Correct |
341 ms |
18936 KB |
Output is correct |
16 |
Correct |
365 ms |
18500 KB |
Output is correct |
17 |
Correct |
346 ms |
18456 KB |
Output is correct |
18 |
Correct |
348 ms |
18192 KB |
Output is correct |
19 |
Correct |
348 ms |
18796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
189 ms |
29176 KB |
Output is correct |
2 |
Correct |
110 ms |
28084 KB |
Output is correct |
3 |
Correct |
113 ms |
27992 KB |
Output is correct |
4 |
Correct |
189 ms |
29304 KB |
Output is correct |
5 |
Correct |
189 ms |
29176 KB |
Output is correct |
6 |
Correct |
184 ms |
28552 KB |
Output is correct |
7 |
Correct |
184 ms |
28536 KB |
Output is correct |
8 |
Correct |
183 ms |
28408 KB |
Output is correct |
9 |
Correct |
186 ms |
28728 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
380 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
3 ms |
380 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
3 ms |
524 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
351 ms |
18844 KB |
Output is correct |
12 |
Correct |
340 ms |
18860 KB |
Output is correct |
13 |
Correct |
405 ms |
18924 KB |
Output is correct |
14 |
Correct |
349 ms |
19040 KB |
Output is correct |
15 |
Correct |
341 ms |
18936 KB |
Output is correct |
16 |
Correct |
365 ms |
18500 KB |
Output is correct |
17 |
Correct |
346 ms |
18456 KB |
Output is correct |
18 |
Correct |
348 ms |
18192 KB |
Output is correct |
19 |
Correct |
348 ms |
18796 KB |
Output is correct |
20 |
Correct |
189 ms |
29176 KB |
Output is correct |
21 |
Correct |
110 ms |
28084 KB |
Output is correct |
22 |
Correct |
113 ms |
27992 KB |
Output is correct |
23 |
Correct |
189 ms |
29304 KB |
Output is correct |
24 |
Correct |
189 ms |
29176 KB |
Output is correct |
25 |
Correct |
184 ms |
28552 KB |
Output is correct |
26 |
Correct |
184 ms |
28536 KB |
Output is correct |
27 |
Correct |
183 ms |
28408 KB |
Output is correct |
28 |
Correct |
186 ms |
28728 KB |
Output is correct |
29 |
Correct |
1359 ms |
95976 KB |
Output is correct |
30 |
Correct |
1054 ms |
92716 KB |
Output is correct |
31 |
Correct |
983 ms |
92724 KB |
Output is correct |
32 |
Correct |
1350 ms |
95864 KB |
Output is correct |
33 |
Correct |
1302 ms |
95924 KB |
Output is correct |
34 |
Correct |
1290 ms |
93560 KB |
Output is correct |
35 |
Correct |
1298 ms |
93336 KB |
Output is correct |
36 |
Correct |
1301 ms |
93304 KB |
Output is correct |
37 |
Correct |
1289 ms |
94748 KB |
Output is correct |
38 |
Correct |
874 ms |
101692 KB |
Output is correct |
39 |
Correct |
869 ms |
101624 KB |
Output is correct |
40 |
Correct |
843 ms |
98356 KB |
Output is correct |
41 |
Correct |
842 ms |
97772 KB |
Output is correct |
42 |
Correct |
841 ms |
97620 KB |
Output is correct |
43 |
Correct |
867 ms |
99448 KB |
Output is correct |
44 |
Correct |
961 ms |
100912 KB |
Output is correct |
45 |
Correct |
942 ms |
101024 KB |
Output is correct |
46 |
Correct |
914 ms |
97868 KB |
Output is correct |
47 |
Correct |
915 ms |
97352 KB |
Output is correct |
48 |
Correct |
915 ms |
97544 KB |
Output is correct |
49 |
Correct |
951 ms |
99636 KB |
Output is correct |
50 |
Correct |
1046 ms |
98872 KB |
Output is correct |
51 |
Correct |
1115 ms |
98952 KB |
Output is correct |
52 |
Correct |
1050 ms |
96240 KB |
Output is correct |
53 |
Correct |
1032 ms |
96120 KB |
Output is correct |
54 |
Correct |
1026 ms |
96120 KB |
Output is correct |
55 |
Correct |
1098 ms |
97732 KB |
Output is correct |