#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define fi first
#define se second
#define vi vector<int>
#define vll vector<long long>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng64(chrono::steady_clock::now().time_since_epoch().count());
inline int rand(int l,int r){return uniform_int_distribution<int>(l, r)(rng);}
inline ll rand(ll l,ll r){return uniform_int_distribution<ll>(l, r)(rng64);}
#ifdef DEBUG
auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";}
auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";}
#define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X)
#else
#define debug(...){}
#endif
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
struct Node{
ll r[3];
Node(vll nr = {0LL, 0LL, 0LL}){
r[0] = nr[0];
r[1] = nr[1];
r[2] = nr[2];
}
};
const int MAXN = 5e5 + 7;
const ll MIN = -1000000000;
const int BASE = (1 << 19);
ll tab[MAXN];
Node tree[2 * BASE + 7];
ll lazy[2 * BASE + 7];
vector<pii> queries[MAXN];
ll ans[MAXN];
vi nxt[MAXN];
int n, q;
vll worst;
Node get(Node x, Node y){
Node res = Node(worst);
res.r[0] = max(x.r[0], y.r[0]);
res.r[1] = max(x.r[1], y.r[1]);
res.r[2] = max({x.r[2], y.r[2], (ll)x.r[0] + y.r[1]});
return res;
}
void Push(int v){
tree[2 * v].r[0] = max(tree[2 * v].r[0], lazy[v]);
tree[2 * v + 1].r[0] = max(tree[2 * v + 1].r[0], lazy[v]);
lazy[2 * v] = max(lazy[2 * v], lazy[v]);
lazy[2 * v + 1] = max(lazy[2 * v + 1], lazy[v]);
tree[2 * v].r[2] = max(tree[2 * v].r[2], lazy[v] + tree[2 * v].r[1]);
tree[2 * v + 1].r[2] = max(tree[2 * v + 1].r[2], lazy[v] + tree[2 * v + 1].r[1]);
}
void upd(int v, int l, int p, int a, int b, ll val){
if(p < a || b < l){
return;
}
if(a <= l && p <= b){
lazy[v] = max(lazy[v], val);
tree[v].r[0] = max(tree[v].r[0], val);
tree[v].r[2] = max(tree[v].r[2], val + tree[v].r[1]);
return;
}
Push(v);
int mid = (l + p) / 2;
upd(2 * v, l, mid, a, b, val);
upd(2 * v + 1, mid + 1, p, a, b, val);
tree[v] = get(tree[2 * v], tree[2 * v + 1]);
}
Node query(int v, int l, int p, int a, int b){
if(p < a || b < l){
return worst;
}
if(a <= l && p <= b){
return tree[v];
}
Push(v);
int mid = (l + p) / 2;
Node ret = query(2 * v, l, mid, a, b);
ret = get(ret, query(2 * v + 1, mid + 1, p, a, b));
tree[v] = get(tree[2 * v], tree[2 * v + 1]);
return ret;
}
void init(){
worst = {MIN, MIN, MIN};
for(int i = 1; i < 2 * BASE + 7; i++){
tree[i] = Node(worst);
}
for(int i = 1; i <= n; i++){
tree[i + BASE].r[1] = tab[i];
}
for(int i = BASE - 1; i >= 1; i--){
tree[i] = get(tree[2 *i], tree[2 * i + 1]);
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++){
cin >> tab[i];
}
init();
vi xd;
for(int i = 1; i <= n; i++){
while(sz(xd) && tab[xd.back()] <= tab[i]){
nxt[xd.back()].pb(i);
xd.pop_back();
}
if(sz(xd)){
nxt[xd.back()].pb(i);
}
xd.pb(i);
}
cin >> q;
for(int i = 1; i <= q; i++){
int l, p;
cin >> l >> p;
queries[l].pb({p, i});
}
for(int i = n; i >= 1; i--){
for(auto ele : nxt[i]){
upd(1, 0, BASE - 1, 2 * ele - i, n, tab[i] + tab[ele]);
}
for(auto ele : queries[i]){
ans[ele.se] = query(1, 0, BASE - 1, i, ele.fi).r[2];
}
}
for(int i = 1; i <= q; i++){
cout << ans[i] << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
48588 KB |
Output is correct |
2 |
Correct |
56 ms |
48624 KB |
Output is correct |
3 |
Correct |
62 ms |
48472 KB |
Output is correct |
4 |
Correct |
56 ms |
48576 KB |
Output is correct |
5 |
Correct |
59 ms |
48464 KB |
Output is correct |
6 |
Correct |
54 ms |
48476 KB |
Output is correct |
7 |
Correct |
56 ms |
48464 KB |
Output is correct |
8 |
Correct |
52 ms |
48476 KB |
Output is correct |
9 |
Correct |
55 ms |
48576 KB |
Output is correct |
10 |
Correct |
53 ms |
48464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
48588 KB |
Output is correct |
2 |
Correct |
56 ms |
48624 KB |
Output is correct |
3 |
Correct |
62 ms |
48472 KB |
Output is correct |
4 |
Correct |
56 ms |
48576 KB |
Output is correct |
5 |
Correct |
59 ms |
48464 KB |
Output is correct |
6 |
Correct |
54 ms |
48476 KB |
Output is correct |
7 |
Correct |
56 ms |
48464 KB |
Output is correct |
8 |
Correct |
52 ms |
48476 KB |
Output is correct |
9 |
Correct |
55 ms |
48576 KB |
Output is correct |
10 |
Correct |
53 ms |
48464 KB |
Output is correct |
11 |
Correct |
940 ms |
69376 KB |
Output is correct |
12 |
Correct |
923 ms |
69220 KB |
Output is correct |
13 |
Correct |
936 ms |
69392 KB |
Output is correct |
14 |
Correct |
927 ms |
69360 KB |
Output is correct |
15 |
Correct |
910 ms |
69456 KB |
Output is correct |
16 |
Correct |
959 ms |
68688 KB |
Output is correct |
17 |
Correct |
910 ms |
68688 KB |
Output is correct |
18 |
Correct |
920 ms |
68696 KB |
Output is correct |
19 |
Correct |
915 ms |
69180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
504 ms |
61308 KB |
Output is correct |
2 |
Correct |
275 ms |
61264 KB |
Output is correct |
3 |
Correct |
278 ms |
62148 KB |
Output is correct |
4 |
Correct |
496 ms |
61544 KB |
Output is correct |
5 |
Correct |
462 ms |
61520 KB |
Output is correct |
6 |
Correct |
480 ms |
60756 KB |
Output is correct |
7 |
Correct |
490 ms |
60756 KB |
Output is correct |
8 |
Correct |
469 ms |
60752 KB |
Output is correct |
9 |
Correct |
489 ms |
60924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
48588 KB |
Output is correct |
2 |
Correct |
56 ms |
48624 KB |
Output is correct |
3 |
Correct |
62 ms |
48472 KB |
Output is correct |
4 |
Correct |
56 ms |
48576 KB |
Output is correct |
5 |
Correct |
59 ms |
48464 KB |
Output is correct |
6 |
Correct |
54 ms |
48476 KB |
Output is correct |
7 |
Correct |
56 ms |
48464 KB |
Output is correct |
8 |
Correct |
52 ms |
48476 KB |
Output is correct |
9 |
Correct |
55 ms |
48576 KB |
Output is correct |
10 |
Correct |
53 ms |
48464 KB |
Output is correct |
11 |
Correct |
940 ms |
69376 KB |
Output is correct |
12 |
Correct |
923 ms |
69220 KB |
Output is correct |
13 |
Correct |
936 ms |
69392 KB |
Output is correct |
14 |
Correct |
927 ms |
69360 KB |
Output is correct |
15 |
Correct |
910 ms |
69456 KB |
Output is correct |
16 |
Correct |
959 ms |
68688 KB |
Output is correct |
17 |
Correct |
910 ms |
68688 KB |
Output is correct |
18 |
Correct |
920 ms |
68696 KB |
Output is correct |
19 |
Correct |
915 ms |
69180 KB |
Output is correct |
20 |
Correct |
504 ms |
61308 KB |
Output is correct |
21 |
Correct |
275 ms |
61264 KB |
Output is correct |
22 |
Correct |
278 ms |
62148 KB |
Output is correct |
23 |
Correct |
496 ms |
61544 KB |
Output is correct |
24 |
Correct |
462 ms |
61520 KB |
Output is correct |
25 |
Correct |
480 ms |
60756 KB |
Output is correct |
26 |
Correct |
490 ms |
60756 KB |
Output is correct |
27 |
Correct |
469 ms |
60752 KB |
Output is correct |
28 |
Correct |
489 ms |
60924 KB |
Output is correct |
29 |
Correct |
2402 ms |
106324 KB |
Output is correct |
30 |
Correct |
1782 ms |
105552 KB |
Output is correct |
31 |
Correct |
1856 ms |
107720 KB |
Output is correct |
32 |
Correct |
2377 ms |
106300 KB |
Output is correct |
33 |
Correct |
2413 ms |
106272 KB |
Output is correct |
34 |
Correct |
2476 ms |
103956 KB |
Output is correct |
35 |
Correct |
2379 ms |
103764 KB |
Output is correct |
36 |
Correct |
2408 ms |
103740 KB |
Output is correct |
37 |
Correct |
2360 ms |
105044 KB |
Output is correct |
38 |
Correct |
2164 ms |
111872 KB |
Output is correct |
39 |
Correct |
2100 ms |
111956 KB |
Output is correct |
40 |
Correct |
2082 ms |
108628 KB |
Output is correct |
41 |
Correct |
2115 ms |
107976 KB |
Output is correct |
42 |
Correct |
2121 ms |
108112 KB |
Output is correct |
43 |
Correct |
2121 ms |
109832 KB |
Output is correct |
44 |
Correct |
2199 ms |
111280 KB |
Output is correct |
45 |
Correct |
2272 ms |
111444 KB |
Output is correct |
46 |
Correct |
2157 ms |
108116 KB |
Output is correct |
47 |
Correct |
2221 ms |
107856 KB |
Output is correct |
48 |
Correct |
2172 ms |
107748 KB |
Output is correct |
49 |
Correct |
2267 ms |
109904 KB |
Output is correct |
50 |
Correct |
2229 ms |
109396 KB |
Output is correct |
51 |
Correct |
2194 ms |
109300 KB |
Output is correct |
52 |
Correct |
2250 ms |
106832 KB |
Output is correct |
53 |
Correct |
2302 ms |
106568 KB |
Output is correct |
54 |
Correct |
2292 ms |
106580 KB |
Output is correct |
55 |
Correct |
2265 ms |
108220 KB |
Output is correct |