#include <bits/stdc++.h>
using namespace std;
int C[300005];
long long int D[300005];
long long int DP[1005][1005];
const long long int INF = 1e18;
struct SegTree {
vector<long long int> seg;
int MAX;
SegTree(int N) {
int i;
for(i=1;i<2*N;i*=2) {}
seg.resize(i);
MAX = i;
}
void update(int n) {
n += MAX/2;
n /= 2;
while(n) {
seg[n] = max(seg[2*n], seg[2*n+1]);
n /= 2;
}
}
void Edit(int n, long long int a) {
seg[n+MAX/2] = max(seg[n+MAX/2], a);
update(n);
}
long long int sum(int s, int e, int n, int ns, int ne) {
if(e<=ns||ne<=s) return 0;
if(s<=ns&&ne<=e) return seg[n];
int mid = ns + ne >> 1;
return max(sum(s,e,2*n,ns,mid),sum(s,e,2*n+1,mid,ne));
}
};
typedef pair<long long int, long long int> P;
vector<long long int> N2logN_Solution(int N) {
int i, j;
vector<long long int> ans;
for(i=0;i<N;i++) {
vector<vector<P>> V1, V2;
V1.resize(N);
V2.resize(N);
for(j=0;j<N;j++) {
if(i<j) {
if(0<=j-C[j]+1&&j-C[j]+1<=i) {
V2[j-C[j]+1].push_back(P(j,D[j]));
}
}
else if(j<i) {
if(j+C[j]-1<N&&i<=j+C[j]-1) {
V1[j].push_back(P(j+C[j]-1,D[j]));
}
}
}
SegTree tree1(N+5);
SegTree tree2(N+5);
int MAX = tree1.MAX;
if(C[i]==1) tree1.Edit(i, D[i]);
for(j=i;j>=0;j--) {
for(P k : V1[j]) {
long long int val1 = tree1.sum(0, k.first + 1, 1, 0, MAX/2);
long long int val2 = tree2.sum(0, k.first + 1, 1, 0, MAX/2);
tree1.Edit(k.first, max(val1, val2) + k.second);
}
for(P k : V2[j]) {
long long int val1 = tree1.sum(0, k.first, 1, 0, MAX/2);
long long int val2 = tree2.sum(0, k.first, 1, 0, MAX/2);
tree2.Edit(k.first, max(val1, val2) + k.second);
}
}
ans.push_back(max(tree1.sum(0, MAX/2, 1, 0, MAX/2), tree2.sum(0, MAX/2, 1, 0, MAX/2)));
}
return ans;
}
vector<long long int> Naive(int N) {
int i, j;
vector<long long int> ans;
for(i=0;i<N;i++) {
for(j=0;j<N;j++) {
for(int k = 0; k <= N; k++) DP[j][k] = -INF;
}
DP[i][i+1] = (C[i]==1 ? D[i] : 0);
for(int len = 1; len < N; len++) {
for(j=0;j+len<=N;j++) {
if(j+len+1<=N) {
DP[j][j+len+1] = max(DP[j][j+len+1], DP[j][j+len] + (C[j+len] == len + 1 ? D[j + len] : 0));
}
if(j >= 1) {
DP[j-1][j+len] = max(DP[j-1][j+len], DP[j][j+len] + (C[j-1] == len + 1 ? D[j-1] : 0));
}
}
}
ans.push_back(DP[0][N]);
}
return ans;
}
signed main() {
cin.sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int N;
cin >> N;
int i, j;
for(i=0;i<N;i++) cin >> C[i];
for(i=0;i<N;i++) cin >> D[i];
/*for(i=0;i<N;i++) {
C[i] = rand() % (N/4) + 1;
}
for(i=0;i<N;i++) D[i] = rand() + 1;*/
vector<long long int> ans1 = N2logN_Solution(N);
for(i=0;i<N;i++) cout << ans1[i] << ' ';
}
Compilation message
gorgeous.cpp: In member function 'long long int SegTree::sum(int, int, int, int, int)':
gorgeous.cpp:31:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
31 | int mid = ns + ne >> 1;
| ~~~^~~~
gorgeous.cpp: In function 'int main()':
gorgeous.cpp:103:12: warning: unused variable 'j' [-Wunused-variable]
103 | int i, j;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
9 ms |
392 KB |
Output is correct |
11 |
Correct |
42 ms |
340 KB |
Output is correct |
12 |
Correct |
64 ms |
340 KB |
Output is correct |
13 |
Correct |
71 ms |
472 KB |
Output is correct |
14 |
Correct |
88 ms |
468 KB |
Output is correct |
15 |
Correct |
83 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
9 ms |
392 KB |
Output is correct |
11 |
Correct |
42 ms |
340 KB |
Output is correct |
12 |
Correct |
64 ms |
340 KB |
Output is correct |
13 |
Correct |
71 ms |
472 KB |
Output is correct |
14 |
Correct |
88 ms |
468 KB |
Output is correct |
15 |
Correct |
83 ms |
596 KB |
Output is correct |
16 |
Execution timed out |
1584 ms |
1196 KB |
Time limit exceeded |
17 |
Halted |
0 ms |
0 KB |
- |