#include<bits/stdc++.h>
using namespace std;
//Let's do sub case 2 first
struct segmentTree
{
vector<int> val;
segmentTree(int n)
{
for(int i=0;i<2*n;i++)
val.push_back(0);
}
void update(int idx, int v)
{
idx = idx + (val.size())/2 - 1;
val[idx] = v;
idx /= 2;
while(idx != 0)
{
val[idx] = max(val[2*idx],val[2*idx+1]);
idx /= 2;
}
}
int ans(int idx, int tl, int tr, int l, int r)
{
if(l > tr || r < tl)
return 0;
if(l <= tl && r >= tr)
return val[idx];
int can1 = ans(2*idx,tl,(tl+tr)/2,l,r);
int can2 = ans(2*idx+1,(tl+tr)/2 +1,tr,l,r);
//cout<<"ACCESS: "<<idx<<' '<<tl<<' '<<tr<<' '<<l<<' '<<r<<' '<<max(can1,can2)<<endl;
return max(can1,can2);
}
};
const int N = 8192;
int dp[N][N];
int v[N];
bool found[N][N];
segmentTree tree = segmentTree(N);
void DP(int l, int r)
{
found[l-1][r-1] = true;
if(r - l < 2)
return;
if(!found[l][r-1])
DP(l+1,r);
if(!found[l-1][r-2])
DP(l,r-1);
dp[l-1][r-1] = max(max(dp[l][r-1],dp[l-1][r-2]),v[l-1]+v[r-1]+tree.ans(1,1,8192,l+1,(l+r)/2));
}
int main()
{
int n;
cin>>n;
if(n > N)
return 0;
for(int i=0;i<n;i++)
{
int a;
cin>>a;
tree.update(i+1,a);
v[i] = a;
}
DP(1,n);
int q;
cin>>q;
for(int i=0;i<q;i++)
{
int l,r;
cin>>l>>r;
if(!found[l-1][r-1])
DP(l,r);
cout<<dp[l-1][r-1]<<endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
1228 KB |
Output is correct |
3 |
Correct |
2 ms |
1228 KB |
Output is correct |
4 |
Correct |
3 ms |
1228 KB |
Output is correct |
5 |
Correct |
2 ms |
1228 KB |
Output is correct |
6 |
Correct |
2 ms |
1228 KB |
Output is correct |
7 |
Correct |
2 ms |
1228 KB |
Output is correct |
8 |
Correct |
2 ms |
1228 KB |
Output is correct |
9 |
Correct |
2 ms |
1148 KB |
Output is correct |
10 |
Correct |
2 ms |
1228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
1228 KB |
Output is correct |
3 |
Correct |
2 ms |
1228 KB |
Output is correct |
4 |
Correct |
3 ms |
1228 KB |
Output is correct |
5 |
Correct |
2 ms |
1228 KB |
Output is correct |
6 |
Correct |
2 ms |
1228 KB |
Output is correct |
7 |
Correct |
2 ms |
1228 KB |
Output is correct |
8 |
Correct |
2 ms |
1228 KB |
Output is correct |
9 |
Correct |
2 ms |
1148 KB |
Output is correct |
10 |
Correct |
2 ms |
1228 KB |
Output is correct |
11 |
Correct |
2972 ms |
115068 KB |
Output is correct |
12 |
Correct |
2973 ms |
115252 KB |
Output is correct |
13 |
Correct |
2995 ms |
115228 KB |
Output is correct |
14 |
Correct |
2975 ms |
115148 KB |
Output is correct |
15 |
Correct |
3122 ms |
115096 KB |
Output is correct |
16 |
Correct |
3186 ms |
114388 KB |
Output is correct |
17 |
Correct |
3161 ms |
114412 KB |
Output is correct |
18 |
Correct |
3190 ms |
114472 KB |
Output is correct |
19 |
Correct |
3044 ms |
114996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
1228 KB |
Output is correct |
3 |
Correct |
2 ms |
1228 KB |
Output is correct |
4 |
Correct |
3 ms |
1228 KB |
Output is correct |
5 |
Correct |
2 ms |
1228 KB |
Output is correct |
6 |
Correct |
2 ms |
1228 KB |
Output is correct |
7 |
Correct |
2 ms |
1228 KB |
Output is correct |
8 |
Correct |
2 ms |
1228 KB |
Output is correct |
9 |
Correct |
2 ms |
1148 KB |
Output is correct |
10 |
Correct |
2 ms |
1228 KB |
Output is correct |
11 |
Correct |
2972 ms |
115068 KB |
Output is correct |
12 |
Correct |
2973 ms |
115252 KB |
Output is correct |
13 |
Correct |
2995 ms |
115228 KB |
Output is correct |
14 |
Correct |
2975 ms |
115148 KB |
Output is correct |
15 |
Correct |
3122 ms |
115096 KB |
Output is correct |
16 |
Correct |
3186 ms |
114388 KB |
Output is correct |
17 |
Correct |
3161 ms |
114412 KB |
Output is correct |
18 |
Correct |
3190 ms |
114472 KB |
Output is correct |
19 |
Correct |
3044 ms |
114996 KB |
Output is correct |
20 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |