#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
#define all(x) (x).begin(),(x).end()
#define X first
#define Y second
#define sep ' '
#define endl '\n'
#define debug(x) cerr << #x << ": " << x << endl;
const ll MAXN = 5e5 + 10;
const ll INF = 8e18;
int FL[MAXN], FR[MAXN], A[MAXN], n, q, ans[MAXN];
vector<int> L_vec[MAXN];
vector<pll> QL[MAXN];
namespace segment {
ll lz[MAXN << 2], sg[MAXN << 2], tof[MAXN << 2];
void build(int l = 1, int r = n, int v = 1) {
if (l == r) sg[v] = A[l];
else {
int mid = (l + r) >> 1;
build(l, mid, v << 1);
build(mid + 1, r, v << 1 | 1);
sg[v] = max(sg[v << 1], sg[v << 1 | 1]);
}
}
inline void push(int v) { // be sure that tof[v] != -1
if (lz[v] == 0) return;
if (lz[v] > tof[v]) {
sg[v] += lz[v] - tof[v];
tof[v] = lz[v];
lz[v << 1] = max(lz[v << 1], lz[v]);
lz[v << 1 | 1] = max(lz[v << 1 | 1], lz[v]);
}
lz[v] = 0;
}
void update(int ql, int qr, ll val, int l = 1, int r = n, int v = 1) {
push(v);
if (ql > r || qr < l) return;
if (ql <= l && qr >= r && tof[v] != -1) {
lz[v] += val;
push(v);
return;
}
int mid = (l + r) >> 1;
update(ql, qr, val, l, mid, v << 1);
update(ql, qr, val, mid + 1, r, v << 1 | 1);
tof[v] = (tof[v << 1] == tof[v << 1 | 1] ? tof[v << 1] : -1);
sg[v] = max(sg[v << 1], sg[v << 1 | 1]);
}
ll query(int ql, int qr, int l = 1, int r = n, int v = 1) {
push(v);
if (ql > r || qr < l) return -INF;
if (ql <= l && qr >= r) return sg[v];
int mid = (l + r) >> 1;
return max(query(ql, qr, l, mid, v << 1),
query(ql, qr, mid + 1, r, v << 1 | 1));
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) cin >> A[i];
for (int i = 1; i <= n; i++)
for (FL[i] = i - 1; FL[i] && A[FL[i]] < A[i]; FL[i] = FL[FL[i]]);
for (int i = n; i > 0; i--)
for (FR[i] = i + 1; FR[i] <= n && A[FR[i]] < A[i]; FR[i] = FR[FR[i]]);
for (int i = 1; i <= n; i++) {
if (FL[i] > 0) L_vec[FL[i]].push_back(i);
if (FR[i] <= n) L_vec[i].push_back(FR[i]);
}
segment::build();
cin >> q;
for (int i = 1; i <= q; i++) {
int l, r;
cin >> l >> r;
QL[l].push_back({i, r});
}
for (int l = n; l > 0; l--) {
for (int mid : L_vec[l]) {
int val = A[l] + A[mid];
int r = mid + mid - l;
if (r <= n) segment::update(r, n, val);
}
for (auto [q, r] : QL[l])
ans[q] = segment::query(l + 2, r);
}
for (int i = 1; i <= q; i++)
cout << ans[i] << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23776 KB |
Output is correct |
2 |
Correct |
13 ms |
23820 KB |
Output is correct |
3 |
Correct |
13 ms |
23840 KB |
Output is correct |
4 |
Correct |
16 ms |
23932 KB |
Output is correct |
5 |
Correct |
13 ms |
23776 KB |
Output is correct |
6 |
Correct |
12 ms |
23772 KB |
Output is correct |
7 |
Correct |
13 ms |
23764 KB |
Output is correct |
8 |
Correct |
13 ms |
23816 KB |
Output is correct |
9 |
Correct |
12 ms |
23768 KB |
Output is correct |
10 |
Correct |
13 ms |
23840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23776 KB |
Output is correct |
2 |
Correct |
13 ms |
23820 KB |
Output is correct |
3 |
Correct |
13 ms |
23840 KB |
Output is correct |
4 |
Correct |
16 ms |
23932 KB |
Output is correct |
5 |
Correct |
13 ms |
23776 KB |
Output is correct |
6 |
Correct |
12 ms |
23772 KB |
Output is correct |
7 |
Correct |
13 ms |
23764 KB |
Output is correct |
8 |
Correct |
13 ms |
23816 KB |
Output is correct |
9 |
Correct |
12 ms |
23768 KB |
Output is correct |
10 |
Correct |
13 ms |
23840 KB |
Output is correct |
11 |
Correct |
231 ms |
49552 KB |
Output is correct |
12 |
Correct |
331 ms |
49560 KB |
Output is correct |
13 |
Correct |
238 ms |
49520 KB |
Output is correct |
14 |
Correct |
220 ms |
49580 KB |
Output is correct |
15 |
Correct |
223 ms |
49728 KB |
Output is correct |
16 |
Correct |
226 ms |
49000 KB |
Output is correct |
17 |
Correct |
220 ms |
48952 KB |
Output is correct |
18 |
Correct |
229 ms |
48876 KB |
Output is correct |
19 |
Correct |
220 ms |
49472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
702 ms |
50708 KB |
Output is correct |
2 |
Execution timed out |
4065 ms |
40044 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23776 KB |
Output is correct |
2 |
Correct |
13 ms |
23820 KB |
Output is correct |
3 |
Correct |
13 ms |
23840 KB |
Output is correct |
4 |
Correct |
16 ms |
23932 KB |
Output is correct |
5 |
Correct |
13 ms |
23776 KB |
Output is correct |
6 |
Correct |
12 ms |
23772 KB |
Output is correct |
7 |
Correct |
13 ms |
23764 KB |
Output is correct |
8 |
Correct |
13 ms |
23816 KB |
Output is correct |
9 |
Correct |
12 ms |
23768 KB |
Output is correct |
10 |
Correct |
13 ms |
23840 KB |
Output is correct |
11 |
Correct |
231 ms |
49552 KB |
Output is correct |
12 |
Correct |
331 ms |
49560 KB |
Output is correct |
13 |
Correct |
238 ms |
49520 KB |
Output is correct |
14 |
Correct |
220 ms |
49580 KB |
Output is correct |
15 |
Correct |
223 ms |
49728 KB |
Output is correct |
16 |
Correct |
226 ms |
49000 KB |
Output is correct |
17 |
Correct |
220 ms |
48952 KB |
Output is correct |
18 |
Correct |
229 ms |
48876 KB |
Output is correct |
19 |
Correct |
220 ms |
49472 KB |
Output is correct |
20 |
Correct |
702 ms |
50708 KB |
Output is correct |
21 |
Execution timed out |
4065 ms |
40044 KB |
Time limit exceeded |
22 |
Halted |
0 ms |
0 KB |
- |