#include <bits/stdc++.h>
#define taskname "test"
#define fi first
#define se second
#define pb push_back
#define int ll
#define faster ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
using ll = long long;
using pii = pair <int, int>;
using pil = pair <int, ll>;
using pli = pair <ll, int>;
using pll = pair <ll, ll>;
using ull = unsigned ll;
mt19937 Rand(chrono::steady_clock::now().time_since_epoch().count());
ll min(const ll &a, const ll &b){
return (a < b) ? a : b;
}
ll max(const ll &a, const ll &b){
return (a > b) ? a : b;
}
//const ll Mod = 1000000009;
//const ll Mod2 = 999999999989;
//only use when required
const int maxN = 1e5 + 1;
int n;
int a[maxN];
vector <int> Q;
vector <pii> p;
int ans[maxN];
pii st[maxN * 4];
int lazy[maxN * 4];
struct TQuery{
int l, r, idx;
};
TQuery q[maxN];
void build(int id = 1, int l = 1, int r = n){
if (l == r){
st[id].fi = st[id].se = a[l];
return;
}
int mid = (l + r) / 2;
build(id * 2, l, mid);
build(id * 2 + 1, mid + 1, r);
st[id].fi = st[id].se = max(st[id * 2].se, st[id * 2 + 1].se);
}
void pull(int id){
if (lazy[id]){
st[id * 2].fi = max(st[id * 2].fi, st[id * 2].se + lazy[id]);
st[id * 2 + 1].fi = max(st[id * 2 + 1].fi, st[id * 2 + 1].se + lazy[id]);
lazy[id * 2] = max(lazy[id * 2], lazy[id]);
lazy[id * 2 + 1] = max(lazy[id * 2 + 1], lazy[id]);
lazy[id] = 0;
}
}
void update(int id, int i, int j, int val, int l = 1, int r = n){
if (r < i || l > j) return;
if (i <= l && r <= j){
st[id].fi = max(st[id].fi, st[id].se + val);
lazy[id] = max(lazy[id], val);
return;
}
pull(id);
int mid = (l + r) / 2;
update(id * 2, i, j, val, l, mid);
update(id * 2 + 1, i, j, val, mid + 1, r);
st[id].fi = max(st[id * 2].fi, st[id * 2 + 1].fi);
}
int get(int id, int i, int j, int l = 1, int r = n){
if (r < i || l > j) return 0;
if (i <= l && r <= j){
return st[id].fi;
}
pull(id);
int mid = (l + r) / 2;
return max(get(id * 2, i, j, l, mid), get(id * 2 + 1, i, j, mid + 1, r));
}
void create(){
for (int i = n; i >= 1; --i){
while (!Q.empty() && a[i] >= a[Q.back()]){
p.pb({i, Q.back()});
Q.pop_back();
}
if (!Q.empty()) p.pb({i, Q.back()});
Q.pb(i);
}
sort(p.begin(), p.end(), greater<pair<int, int>>());
}
void Init(){
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
create();
build();
int qq; cin >> qq;
for (int i = 1; i <= qq; ++i){
cin >> q[i].l >> q[i].r;
q[i].idx = i;
}
sort(q + 1, q + qq + 1,[](const auto &i, const auto &j){
return i.l > j.l;
});
int j = 0;
for (int i = 1; i <= qq; ++i){
while (j < p.size() && p[j].fi >= q[i].l){
update(1, 2 * p[j].se - p[j].fi, n, a[p[j].se] + a[p[j].fi]);
++j;
}
ans[q[i].idx] = get(1, q[i].l, q[i].r);
}
for (int i = 1; i <= qq; ++i) cout << ans[i] << "\n";
}
signed main(){
if (fopen(taskname".inp", "r")){
freopen(taskname".inp", "r", stdin);
//freopen(taskname".out", "w", stdout);
}
faster;
ll tt = 1;
//cin >> tt;
while (tt--){
Init();
}
}
Compilation message
jumps.cpp: In function 'void Init()':
jumps.cpp:115:18: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
115 | while (j < p.size() && p[j].fi >= q[i].l){
| ~~^~~~~~~~~~
jumps.cpp: In function 'int main()':
jumps.cpp:127:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
127 | freopen(taskname".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Runtime error |
120 ms |
29660 KB |
Execution killed with signal 11 |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
16 ms |
3132 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Runtime error |
120 ms |
29660 KB |
Execution killed with signal 11 |
12 |
Halted |
0 ms |
0 KB |
- |