#include<bits/stdc++.h>
#define ll long long
#define ldb long double
#define fi first
#define se second
#define sza(a) (int)a.size()
#define pir pair<int,int>
#define pirll pair<ll,ll>
#define usa unsigned int
#define pirusa pair <unsigned int,unsigned int>
using namespace std;
const int maxn = 5e5 + 5;
template <typename T> void maxi(T &x,T y){x = max(x,y);}
struct segment_tree{
unsigned int seg[4*maxn],lazy[4*maxn],base[4*maxn];
void downlazy(int v,int l,int r){
if (!lazy[v]) return;
seg[v] = max(seg[v],base[v] + lazy[v]);
if (l != r){
maxi(lazy[2*v + 1],lazy[v]);
maxi(lazy[2*v + 2],lazy[v]);
}
lazy[v] = 0;
}
void base_update(int l,int r,int v,int p,int val){
if (l > p || r < p) return;
if (l == r){
base[v] = val;
return;
}
int w = (l + r)/2;
base_update(l,w,2*v + 1,p,val);
base_update(w + 1,r,2*v + 2,p,val);
base[v] = max(base[2*v + 1],base[2*v + 2]);
}
void update(int l,int r,int v,int lx,int rx,unsigned int val){
downlazy(v,l,r);
if (l > rx || r < lx) return;
if (l >= lx && r <= rx){
lazy[v] = max(lazy[v],val);
downlazy(v,l,r);
return;
}
int w = (l + r)/2;
update(l,w,2*v + 1,lx,rx,val);
update(w + 1,r,2*v + 2,lx,rx,val);
seg[v] = max(seg[2*v + 1],seg[2*v + 2]);
}
unsigned int calc(int l,int r,int v,int lx,int rx){
downlazy(v,l,r);
if (l > rx || r < lx) return 0;
if (l >= lx && r <= rx) return seg[v];
int w = (l + r)/2;
return max(calc(l,w,2*v + 1,lx,rx),calc(w + 1,r,2*v + 2,lx,rx));
}
} seg;
vector <vector<pirusa>> vec(maxn);
usa res[maxn];
int a[maxn],L[maxn],R[maxn];
vector <vector<pir>> Q(maxn);
////////
int input(int n){
int q;
for (int i = 1 ; i <= n ; i++) cin >> a[i];
cin >> q;
for (int i = 1 ; i <= q ; i++){
cin >> L[i] >> R[i];
}
return q;
}
namespace sub1{
const int N = 5e3 + 5;
bool check(int n,int q){
return (n <= 5000);
}
unsigned int f[N][N];
void prepare(int n){
for (int l = 1 ; l < n - 1 ; l++){
int x = a[l + 1];
for (int r = l + 2 ; r <= n ; r++){
x = max(x,a[(l + r)/2]);
f[l][r] = max(f[l][r],(unsigned int)(a[l] + x + a[r]));
}
}
for (int len = 3 ; len <= n ; len++)
for (int i = 1 ; i <= n - len + 1 ; i++){
int l = i,r = i + len - 1;
f[l][r] = max(f[l][r],max(f[l + 1][r],f[l][r - 1]));
}
}
void solve(int n,int q){
prepare(n);
for (int i = 1 ; i <= q ; i++)
cout << f[L[i]][R[i]] << "\n";
}
}
///////
void prepare(int n){
deque <int> dq;
for (int i = n ; i > 0 ; i--){
while (dq.size()){
int l = i,r = dq.back();
if (2*r - l <= n)
vec[l].push_back({2*r - l,(usa)a[i] + (usa)a[dq.back()]});
if (a[dq.back()] <= a[i])
dq.pop_back();
else break;
}
dq.push_back(i);
}
for (int i = 1 ; i <= n ; i++)
seg.base_update(1,n,0,i,a[i]);
}
void prepare_query(int q){
for (int i = 1 ; i <= q ; i++)
Q[L[i]].push_back({R[i],i});
}
void solve(int n,int q){
for (int i = n ; i > 0 ; i--){
for (pirusa P : vec[i]){
int l = P.fi;usa val = P.se;
seg.update(1,n,0,l,n,val);
}
for (pir query : Q[i]){
int l = i,r = query.fi,id = query.se;
res[id] = seg.calc(1,n,0,l,r);
}
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
//freopen("JUMP.inp","r",stdin);
// freopen("JUMP.out","w",stdout);
int n,q;
cin >> n;
q = input(n);
if (sub1::check(n,q)){
sub1::solve(n,q);
return 0;
}
prepare(n);
prepare_query(q);
solve(n,q);
for (int i = 1 ; i <= q ; i++) cout << res[i] << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23976 KB |
Output is correct |
2 |
Correct |
10 ms |
24156 KB |
Output is correct |
3 |
Correct |
10 ms |
24360 KB |
Output is correct |
4 |
Correct |
9 ms |
24156 KB |
Output is correct |
5 |
Correct |
11 ms |
24156 KB |
Output is correct |
6 |
Correct |
11 ms |
24240 KB |
Output is correct |
7 |
Correct |
10 ms |
24156 KB |
Output is correct |
8 |
Correct |
10 ms |
24156 KB |
Output is correct |
9 |
Correct |
10 ms |
24156 KB |
Output is correct |
10 |
Correct |
11 ms |
24188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23976 KB |
Output is correct |
2 |
Correct |
10 ms |
24156 KB |
Output is correct |
3 |
Correct |
10 ms |
24360 KB |
Output is correct |
4 |
Correct |
9 ms |
24156 KB |
Output is correct |
5 |
Correct |
11 ms |
24156 KB |
Output is correct |
6 |
Correct |
11 ms |
24240 KB |
Output is correct |
7 |
Correct |
10 ms |
24156 KB |
Output is correct |
8 |
Correct |
10 ms |
24156 KB |
Output is correct |
9 |
Correct |
10 ms |
24156 KB |
Output is correct |
10 |
Correct |
11 ms |
24188 KB |
Output is correct |
11 |
Correct |
233 ms |
104532 KB |
Output is correct |
12 |
Correct |
218 ms |
104280 KB |
Output is correct |
13 |
Correct |
225 ms |
104272 KB |
Output is correct |
14 |
Correct |
218 ms |
104532 KB |
Output is correct |
15 |
Correct |
211 ms |
104480 KB |
Output is correct |
16 |
Correct |
224 ms |
103788 KB |
Output is correct |
17 |
Correct |
222 ms |
103760 KB |
Output is correct |
18 |
Correct |
220 ms |
103760 KB |
Output is correct |
19 |
Correct |
214 ms |
104272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
117 ms |
39964 KB |
Output is correct |
2 |
Correct |
79 ms |
39752 KB |
Output is correct |
3 |
Correct |
78 ms |
38892 KB |
Output is correct |
4 |
Correct |
120 ms |
39980 KB |
Output is correct |
5 |
Correct |
138 ms |
40020 KB |
Output is correct |
6 |
Correct |
115 ms |
39260 KB |
Output is correct |
7 |
Correct |
113 ms |
39248 KB |
Output is correct |
8 |
Correct |
114 ms |
39248 KB |
Output is correct |
9 |
Correct |
118 ms |
39508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23976 KB |
Output is correct |
2 |
Correct |
10 ms |
24156 KB |
Output is correct |
3 |
Correct |
10 ms |
24360 KB |
Output is correct |
4 |
Correct |
9 ms |
24156 KB |
Output is correct |
5 |
Correct |
11 ms |
24156 KB |
Output is correct |
6 |
Correct |
11 ms |
24240 KB |
Output is correct |
7 |
Correct |
10 ms |
24156 KB |
Output is correct |
8 |
Correct |
10 ms |
24156 KB |
Output is correct |
9 |
Correct |
10 ms |
24156 KB |
Output is correct |
10 |
Correct |
11 ms |
24188 KB |
Output is correct |
11 |
Correct |
233 ms |
104532 KB |
Output is correct |
12 |
Correct |
218 ms |
104280 KB |
Output is correct |
13 |
Correct |
225 ms |
104272 KB |
Output is correct |
14 |
Correct |
218 ms |
104532 KB |
Output is correct |
15 |
Correct |
211 ms |
104480 KB |
Output is correct |
16 |
Correct |
224 ms |
103788 KB |
Output is correct |
17 |
Correct |
222 ms |
103760 KB |
Output is correct |
18 |
Correct |
220 ms |
103760 KB |
Output is correct |
19 |
Correct |
214 ms |
104272 KB |
Output is correct |
20 |
Correct |
117 ms |
39964 KB |
Output is correct |
21 |
Correct |
79 ms |
39752 KB |
Output is correct |
22 |
Correct |
78 ms |
38892 KB |
Output is correct |
23 |
Correct |
120 ms |
39980 KB |
Output is correct |
24 |
Correct |
138 ms |
40020 KB |
Output is correct |
25 |
Correct |
115 ms |
39260 KB |
Output is correct |
26 |
Correct |
113 ms |
39248 KB |
Output is correct |
27 |
Correct |
114 ms |
39248 KB |
Output is correct |
28 |
Correct |
118 ms |
39508 KB |
Output is correct |
29 |
Correct |
645 ms |
88660 KB |
Output is correct |
30 |
Correct |
553 ms |
85424 KB |
Output is correct |
31 |
Correct |
510 ms |
85588 KB |
Output is correct |
32 |
Correct |
658 ms |
88660 KB |
Output is correct |
33 |
Correct |
623 ms |
88660 KB |
Output is correct |
34 |
Correct |
644 ms |
86360 KB |
Output is correct |
35 |
Correct |
626 ms |
86100 KB |
Output is correct |
36 |
Correct |
659 ms |
86148 KB |
Output is correct |
37 |
Correct |
617 ms |
87400 KB |
Output is correct |
38 |
Correct |
503 ms |
94288 KB |
Output is correct |
39 |
Correct |
489 ms |
94544 KB |
Output is correct |
40 |
Correct |
478 ms |
90964 KB |
Output is correct |
41 |
Correct |
489 ms |
90388 KB |
Output is correct |
42 |
Correct |
483 ms |
90456 KB |
Output is correct |
43 |
Correct |
488 ms |
92344 KB |
Output is correct |
44 |
Correct |
507 ms |
93560 KB |
Output is correct |
45 |
Correct |
533 ms |
93820 KB |
Output is correct |
46 |
Correct |
507 ms |
90452 KB |
Output is correct |
47 |
Correct |
509 ms |
90192 KB |
Output is correct |
48 |
Correct |
509 ms |
90104 KB |
Output is correct |
49 |
Correct |
549 ms |
92244 KB |
Output is correct |
50 |
Correct |
553 ms |
91920 KB |
Output is correct |
51 |
Correct |
579 ms |
91728 KB |
Output is correct |
52 |
Correct |
555 ms |
89316 KB |
Output is correct |
53 |
Correct |
556 ms |
89080 KB |
Output is correct |
54 |
Correct |
549 ms |
88916 KB |
Output is correct |
55 |
Correct |
544 ms |
90708 KB |
Output is correct |