#include <bits/stdc++.h>
#define int long long
using namespace std;
const int NS = (int)3e5 + 4;
int tr[NS * 4];
void push(int x, int s, int e, int pos, int val){
if(pos < s || pos > e) return;
if(s == e){
tr[x] = max(tr[x], val);
return;
}
int m = s + e >> 1;
push(x * 2, s, m, pos, val);
push(x * 2 + 1, m + 1, e, pos, val);
tr[x] = max(tr[x * 2], tr[x * 2 + 1]);
}
int get(int x, int s, int e, int fs, int fe){
if(fe < s || fs > e) return 0;
if(fs <= s && fe >= e) return tr[x];
int m = s + e >> 1;
return max(get(x * 2, s, m, fs, fe), get(x * 2 + 1, m + 1, e, fs, fe));
}
signed main(){
int n; cin >> n;
vector<int> a(n), b(n);
for(int i = 0; i < n; ++i){
cin >> a[i];
}
for(int i = 0; i < n; ++i){
cin >> b[i];
}
vector<pair<int, int>> ij;
for(int i = 0; i < n; ++i){
if(a[i] > 1 && i + a[i] - 1 < n) ij.push_back({i, i + a[i] - 1});
if(a[i] > 1 && i - a[i] + 1 >= 0) ij.push_back({i - a[i] + 1, i});
}
sort(ij.begin(), ij.end(), [&](pair<int, int>&x, pair<int, int>&y){return x.first < y.first || (x.first == y.first && x.second > y.second);});
int j = 0;
vector<int> ans(n);
for(int i = 0; i < n; ++i){
int k = j;
vector<vector<int>> plazy;
while(k < (int)ij.size() && ij[k].first == i){
int val = get(1, 0, n - 1, ij[k].second, n - 1);
if(ij[k].second - ij[k].first + 1 == a[ij[k].first]){
plazy.push_back({ij[k].second, val + b[ij[k].first]});
}
if(ij[k].second - ij[k].first + 1 == a[ij[k].second]){
push(1, 0, n - 1, ij[k].second - 1, val + b[ij[k].second]);
}
++k;
}
j = k;
ans[i] = get(1, 0, n - 1, i, n - 1) + (a[i] == 1) * b[i];
for(auto&x:plazy){
push(1, 0, n - 1, x[0], x[1]);
}
}
for(int i = 0; i < n; ++i){
cout << ans[i] << ' ';
}
return 0;
}
Compilation message
gorgeous.cpp: In function 'void push(long long int, long long int, long long int, long long int, long long int)':
gorgeous.cpp:14:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
14 | int m = s + e >> 1;
| ~~^~~
gorgeous.cpp: In function 'long long int get(long long int, long long int, long long int, long long int, long long int)':
gorgeous.cpp:23:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
23 | int m = s + e >> 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
304 KB |
Output is correct |
3 |
Correct |
1 ms |
308 KB |
Output is correct |
4 |
Correct |
0 ms |
308 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
304 KB |
Output is correct |
3 |
Correct |
1 ms |
308 KB |
Output is correct |
4 |
Correct |
0 ms |
308 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
308 KB |
Output is correct |
8 |
Correct |
1 ms |
304 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
388 KB |
Output is correct |
13 |
Correct |
1 ms |
312 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
304 KB |
Output is correct |
3 |
Correct |
1 ms |
308 KB |
Output is correct |
4 |
Correct |
0 ms |
308 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
308 KB |
Output is correct |
8 |
Correct |
1 ms |
304 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
388 KB |
Output is correct |
13 |
Correct |
1 ms |
312 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
7 ms |
852 KB |
Output is correct |
17 |
Correct |
30 ms |
2288 KB |
Output is correct |
18 |
Correct |
129 ms |
8636 KB |
Output is correct |
19 |
Correct |
164 ms |
10780 KB |
Output is correct |
20 |
Correct |
492 ms |
27952 KB |
Output is correct |
21 |
Correct |
469 ms |
28880 KB |
Output is correct |
22 |
Correct |
429 ms |
28896 KB |
Output is correct |
23 |
Correct |
471 ms |
28940 KB |
Output is correct |
24 |
Correct |
480 ms |
28892 KB |
Output is correct |
25 |
Correct |
420 ms |
32380 KB |
Output is correct |