#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fi first
#define se second
#define pb push_back
#define ALL(n) n.begin(), n.end()
#define mask(n) (1ll << (n))
#define ck(n, k) (((n) >> (k))&1)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int a[500005], nxt[500005];
vector<pair<int,int>> query[500005];
ll st[2000005], laz[2000005];
int s[5000005];
ll ans[500005];
void down(int id)
{
ll &val = laz[id];
if (val == 0) return;
st[id * 2] = max(st[id * 2], s[id * 2] + val);
laz[id * 2] = max(laz[id * 2], val);
st[id * 2 + 1] = max(st[id * 2 + 1], s[id * 2 + 1] + val);
laz[id * 2 + 1] = max(laz[id * 2 + 1], val);
val = 0;
}
void update(int id, int l, int r, int u, int v, ll val)
{
if (r < u || l > v) return;
if (u <= l && r <= v)
{
st[id] = max(st[id], s[id] + val);
laz[id] = max(laz[id], val);
return;
}
int mid = (l + r)/2;
down(id);
update(id * 2, l, mid, u, v, val); update(id * 2 + 1, mid + 1, r, u, v, val);
st[id] = max(st[id * 2], st[id * 2 + 1]);
// cout << l << " " << r << " " << st[id] << endl;
}
ll get(int id, int l, int r, int u, int v)
{
if (r < u || l > v) return 0;
if (u <= l && r <= v)
{
return st[id];
}
int mid = (l + r)/2;
down(id);
return max(get(id * 2, l, mid, u, v), get(id * 2 + 1, mid + 1, r, u, v));
// cout << "wtf " << " " << l << " " << r << endl;
}
void build(int id, int l, int r)
{
if (l > r) return;
if (l == r)
{
s[id] = a[l];
return;
}
int mid = (l + r) / 2;
build(id * 2, l, mid); build(id * 2 + 1, mid + 1, r);
s[id] = max(s[id * 2], s[id * 2 + 1]);
// cout << l << " " << r << " " << s[id] << endl;
}
void solution()
{
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
}
build(1, 1, n);
int q;
cin >> q;
for (int i = 1; i <= q; i++)
{
int l, r;
cin >> l >> r;
query[l].pb({r, i});
}
stack<int> st;
for (int i = n; i; i--)
{
while(!st.empty() && a[st.top()] <= a[i]) st.pop();
nxt[i] = (st.empty() ? -1 : st.top());
// cout << nxt[i] << endl;
st.push(i);
if (i == n) continue;
int j = i + 1;
while(j != -1)
{
if (2 * j - i <= n)
{
update(1, 1, n, 2 * j - i, n, a[i] + a[j]);
// cout << i << " " << j << endl;
}
if (a[j] >= a[i]) break;
j = nxt[j];
}
for (auto v : query[i])
{
// cout << i << " " << v.fi << endl;
ans[v.se] = get(1, 1, n, i, v.fi);
}
}
for (int i = 1; i <= q; i++) cout << ans[i] << "\n";
}
int main()
{
//freopen("test.inp", "r", stdin);
//freopen("test.out", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
solution();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
12376 KB |
Output is correct |
2 |
Correct |
5 ms |
12120 KB |
Output is correct |
3 |
Correct |
6 ms |
12124 KB |
Output is correct |
4 |
Correct |
5 ms |
12124 KB |
Output is correct |
5 |
Correct |
6 ms |
12124 KB |
Output is correct |
6 |
Correct |
5 ms |
12212 KB |
Output is correct |
7 |
Correct |
6 ms |
12124 KB |
Output is correct |
8 |
Correct |
5 ms |
12124 KB |
Output is correct |
9 |
Correct |
5 ms |
12220 KB |
Output is correct |
10 |
Correct |
5 ms |
12124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
12376 KB |
Output is correct |
2 |
Correct |
5 ms |
12120 KB |
Output is correct |
3 |
Correct |
6 ms |
12124 KB |
Output is correct |
4 |
Correct |
5 ms |
12124 KB |
Output is correct |
5 |
Correct |
6 ms |
12124 KB |
Output is correct |
6 |
Correct |
5 ms |
12212 KB |
Output is correct |
7 |
Correct |
6 ms |
12124 KB |
Output is correct |
8 |
Correct |
5 ms |
12124 KB |
Output is correct |
9 |
Correct |
5 ms |
12220 KB |
Output is correct |
10 |
Correct |
5 ms |
12124 KB |
Output is correct |
11 |
Correct |
193 ms |
33128 KB |
Output is correct |
12 |
Correct |
196 ms |
32868 KB |
Output is correct |
13 |
Correct |
191 ms |
32912 KB |
Output is correct |
14 |
Correct |
208 ms |
33108 KB |
Output is correct |
15 |
Correct |
205 ms |
33108 KB |
Output is correct |
16 |
Correct |
211 ms |
32340 KB |
Output is correct |
17 |
Correct |
193 ms |
32340 KB |
Output is correct |
18 |
Correct |
192 ms |
32336 KB |
Output is correct |
19 |
Correct |
206 ms |
32904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
95 ms |
25684 KB |
Output is correct |
2 |
Correct |
58 ms |
26448 KB |
Output is correct |
3 |
Correct |
60 ms |
25680 KB |
Output is correct |
4 |
Correct |
96 ms |
25680 KB |
Output is correct |
5 |
Correct |
94 ms |
25784 KB |
Output is correct |
6 |
Correct |
91 ms |
24912 KB |
Output is correct |
7 |
Correct |
99 ms |
24912 KB |
Output is correct |
8 |
Correct |
91 ms |
24876 KB |
Output is correct |
9 |
Correct |
91 ms |
25268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
12376 KB |
Output is correct |
2 |
Correct |
5 ms |
12120 KB |
Output is correct |
3 |
Correct |
6 ms |
12124 KB |
Output is correct |
4 |
Correct |
5 ms |
12124 KB |
Output is correct |
5 |
Correct |
6 ms |
12124 KB |
Output is correct |
6 |
Correct |
5 ms |
12212 KB |
Output is correct |
7 |
Correct |
6 ms |
12124 KB |
Output is correct |
8 |
Correct |
5 ms |
12124 KB |
Output is correct |
9 |
Correct |
5 ms |
12220 KB |
Output is correct |
10 |
Correct |
5 ms |
12124 KB |
Output is correct |
11 |
Correct |
193 ms |
33128 KB |
Output is correct |
12 |
Correct |
196 ms |
32868 KB |
Output is correct |
13 |
Correct |
191 ms |
32912 KB |
Output is correct |
14 |
Correct |
208 ms |
33108 KB |
Output is correct |
15 |
Correct |
205 ms |
33108 KB |
Output is correct |
16 |
Correct |
211 ms |
32340 KB |
Output is correct |
17 |
Correct |
193 ms |
32340 KB |
Output is correct |
18 |
Correct |
192 ms |
32336 KB |
Output is correct |
19 |
Correct |
206 ms |
32904 KB |
Output is correct |
20 |
Correct |
95 ms |
25684 KB |
Output is correct |
21 |
Correct |
58 ms |
26448 KB |
Output is correct |
22 |
Correct |
60 ms |
25680 KB |
Output is correct |
23 |
Correct |
96 ms |
25680 KB |
Output is correct |
24 |
Correct |
94 ms |
25784 KB |
Output is correct |
25 |
Correct |
91 ms |
24912 KB |
Output is correct |
26 |
Correct |
99 ms |
24912 KB |
Output is correct |
27 |
Correct |
91 ms |
24876 KB |
Output is correct |
28 |
Correct |
91 ms |
25268 KB |
Output is correct |
29 |
Correct |
657 ms |
66388 KB |
Output is correct |
30 |
Correct |
563 ms |
68232 KB |
Output is correct |
31 |
Correct |
542 ms |
66164 KB |
Output is correct |
32 |
Correct |
629 ms |
66384 KB |
Output is correct |
33 |
Correct |
641 ms |
66228 KB |
Output is correct |
34 |
Correct |
645 ms |
63968 KB |
Output is correct |
35 |
Correct |
653 ms |
63868 KB |
Output is correct |
36 |
Correct |
650 ms |
63764 KB |
Output is correct |
37 |
Correct |
645 ms |
65168 KB |
Output is correct |
38 |
Correct |
461 ms |
72132 KB |
Output is correct |
39 |
Correct |
467 ms |
72080 KB |
Output is correct |
40 |
Correct |
456 ms |
68600 KB |
Output is correct |
41 |
Correct |
449 ms |
68184 KB |
Output is correct |
42 |
Correct |
442 ms |
68176 KB |
Output is correct |
43 |
Correct |
468 ms |
69804 KB |
Output is correct |
44 |
Correct |
500 ms |
71504 KB |
Output is correct |
45 |
Correct |
492 ms |
71448 KB |
Output is correct |
46 |
Correct |
474 ms |
68176 KB |
Output is correct |
47 |
Correct |
498 ms |
67888 KB |
Output is correct |
48 |
Correct |
484 ms |
67920 KB |
Output is correct |
49 |
Correct |
479 ms |
69972 KB |
Output is correct |
50 |
Correct |
542 ms |
69560 KB |
Output is correct |
51 |
Correct |
554 ms |
69456 KB |
Output is correct |
52 |
Correct |
545 ms |
67120 KB |
Output is correct |
53 |
Correct |
548 ms |
66800 KB |
Output is correct |
54 |
Correct |
528 ms |
66740 KB |
Output is correct |
55 |
Correct |
558 ms |
68500 KB |
Output is correct |