#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) (x).begin(), (x).end()
template<template<typename> class Container, typename G>
ostream& operator<<(ostream& os, Container<G> x) {
int f = 0; os << '{'; for (auto &i: x) os << (f++ ? ", " : ""), os << i; os << "}";
return os;
}
void _print() {cerr << "]\n";}
template<typename T, typename... V>
void _print(T t, V... v) {cerr << t; if (sizeof...(v)) cerr <<", "; _print(v...);}
#ifdef DEBUG
#define dbg(x...) cerr << "\e[91m"<<__func__<<":"<<__LINE__<<" [" << #x << "] = ["; _print(x); cerr << "\e[39m" << endl;
#else
#define dbg(x...)
#endif
#define pii pair<int, int>
#define f first
#define s second
int scan() {
int x; cin >> x; return x;
}
struct node {
int l, r, val;
node() {
this->l = 0;
this->r = 0;
this->val = 0;
}
node(int a, int b, int v) {
this->l = a; this->r = b; this->val = v;
}
};
node merge(node a, node b) {
return node(max(a.l, b.l), max(b.r, a.r), max({a.val, b.val, a.l+b.r}));
}
int n, q;
int value[5000005];
struct seg {
vector<node> T;
int n;
seg(int n) {
this->n = n;
T.resize(2*n+5);
for (int i = 0; i < 2*n; i++) T[i] = node(0, 0, 0);
for (int i = 0; i <= n; i++) {
T[i|n].r = T[i|n].val = value[i];
}
for (int i = n-1; i; i--) {
T[i] = merge(T[i<<1], T[i<<1|1]);
}
}
void upd(int x, int v) {
x |= n;
T[x].val = max(T[x].l, v)+T[x].r;
T[x].l = max(T[x].l, v);
while (x >>= 1) T[x] = merge(T[x<<1], T[x<<1|1]);
}
int query(int ql, int qr) {
node l= node(0, 0, 0);
node r = node(0, 0, 0);
ql |= n; qr |= n;
while (ql <= qr) {
if (ql&1) l = merge(l, T[ql++]);
if (~qr &1) r = merge(T[qr--], r);
ql>>=1;
qr>>=1;
}
return (merge(l, r)).val;
}
};
seg *segtree;
vector<pii> queries[5000005];
vector<pii> updates[5000005];
int32_t main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> value[i];
cin >> q;
for (int i = 1; i <= q; i++) {
int l, r; cin >> l >> r;
queries[l].emplace_back(r, i);
}
segtree = new seg(1048576);
stack<int> stck;
dbg("...");
for (int i = 1; i <= n; i++) {
while (stck.size()) {
int j = stck.top();
if (value[i] > value[j]) {
stck.pop();
updates[j].emplace_back(i, value[i]+value[j]);
} else {
updates[j].emplace_back(i, value[i]+value[j]);
break;
}
}
stck.push(i);
}
vector<int> ans(n+3, 0);
for (int i = n; i > 0; i--) {
for (auto j : updates[i]) {
if ((int)2*j.f - i <= n) segtree->upd((int)2*j.f-i, j.s);
}
for (auto j : queries[i]) {
ans[j.s] = segtree->query(i, j.f);
}
}
for (int i = 1; i <= q; i++) cout << ans[i] << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
109 ms |
285824 KB |
Output is correct |
2 |
Correct |
85 ms |
285868 KB |
Output is correct |
3 |
Correct |
86 ms |
285816 KB |
Output is correct |
4 |
Correct |
89 ms |
285856 KB |
Output is correct |
5 |
Correct |
89 ms |
285780 KB |
Output is correct |
6 |
Correct |
86 ms |
285708 KB |
Output is correct |
7 |
Correct |
85 ms |
285868 KB |
Output is correct |
8 |
Correct |
88 ms |
285864 KB |
Output is correct |
9 |
Correct |
89 ms |
285852 KB |
Output is correct |
10 |
Correct |
85 ms |
285812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
109 ms |
285824 KB |
Output is correct |
2 |
Correct |
85 ms |
285868 KB |
Output is correct |
3 |
Correct |
86 ms |
285816 KB |
Output is correct |
4 |
Correct |
89 ms |
285856 KB |
Output is correct |
5 |
Correct |
89 ms |
285780 KB |
Output is correct |
6 |
Correct |
86 ms |
285708 KB |
Output is correct |
7 |
Correct |
85 ms |
285868 KB |
Output is correct |
8 |
Correct |
88 ms |
285864 KB |
Output is correct |
9 |
Correct |
89 ms |
285852 KB |
Output is correct |
10 |
Correct |
85 ms |
285812 KB |
Output is correct |
11 |
Runtime error |
2780 ms |
524292 KB |
Execution killed with signal 9 |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
254 ms |
301392 KB |
Output is correct |
2 |
Correct |
186 ms |
297548 KB |
Output is correct |
3 |
Correct |
181 ms |
298920 KB |
Output is correct |
4 |
Correct |
239 ms |
301392 KB |
Output is correct |
5 |
Correct |
239 ms |
301460 KB |
Output is correct |
6 |
Correct |
226 ms |
300624 KB |
Output is correct |
7 |
Correct |
224 ms |
300720 KB |
Output is correct |
8 |
Correct |
221 ms |
300624 KB |
Output is correct |
9 |
Correct |
231 ms |
300944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
109 ms |
285824 KB |
Output is correct |
2 |
Correct |
85 ms |
285868 KB |
Output is correct |
3 |
Correct |
86 ms |
285816 KB |
Output is correct |
4 |
Correct |
89 ms |
285856 KB |
Output is correct |
5 |
Correct |
89 ms |
285780 KB |
Output is correct |
6 |
Correct |
86 ms |
285708 KB |
Output is correct |
7 |
Correct |
85 ms |
285868 KB |
Output is correct |
8 |
Correct |
88 ms |
285864 KB |
Output is correct |
9 |
Correct |
89 ms |
285852 KB |
Output is correct |
10 |
Correct |
85 ms |
285812 KB |
Output is correct |
11 |
Runtime error |
2780 ms |
524292 KB |
Execution killed with signal 9 |
12 |
Halted |
0 ms |
0 KB |
- |