#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
struct SegTree {
int n;
vector<int> tree;
SegTree(vector<int>& a) {
n = a.size();
tree.assign(2*n, 0);
for(int i = n; i < 2*n; i++) {
tree[i] = a[i-n];
}
for(int idx = n-1; idx > 1; idx--) {
tree[idx] = max(tree[2*idx], tree[2*idx+1]);
}
}
void update(int idx, int val) {
idx += n;
tree[idx] = val;
while(idx > 1) {
idx /= 2;
tree[idx] = max(tree[2*idx], tree[2*idx+1]);
}
}
int getmax(int l, int r) {
l += n, r += n;
int mx = 0;
while(l < r) {
if (l % 2) {
mx = max(mx, tree[l]);
l++;
}
if (r % 2) {
r--;
mx = max(mx, tree[r]);
}
l /= 2, r /= 2;
}
return mx;
}
};
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> a(n);
for(int i = 0; i < n; i++) {
cin >> a[i];
}
vector<int> p(n), r(n, -1);
// iota(r.begin(), r.end(), 0);
p[n-1] = -1;
for(int i = 0; i < n-1; i++) {
cin >> p[i];
p[i]--;
r[p[i]] = i;
}
if (n <= 1000) {
int q;
cin >> q;
for(int i = 0; i < q; i++) {
char type;
cin >> type;
if (type == 'I') {
int idx;
cin >> idx;
idx--;
cout << a[idx] << "\n";
} else if (type == 'P') {
int idx, by;
cin >> idx >> by;
idx--;
a[idx] += by;
vector<bool> vis(n+1, false);
while(1) {
int par = p[idx];
if (par == -1 || vis[par]) break;
a[par] = max(a[par], a[idx]);
idx = par;
}
}
}
} else {
SegTree tree(a);
int q;
cin >> q;
while(q--) {
char type;
cin >> type;
if (type == 'I') {
int idx;
cin >> idx;
idx--;
int ans;
if (r[idx] == -1) {
ans = tree.getmax(idx, idx+1);
} else {
ans = max(tree.getmax(0, r[idx]+1), tree.getmax(idx, idx+1));
}
cout << ans << "\n";
} else {
int idx, val;
cin >> idx >> val;
idx--;
tree.update(idx, max(tree.getmax(0, r[idx]+1), tree.getmax(idx, idx+1)) + val);
}
}
}
// for(int i = 0; i < n; i++) {
// cout << r[i] << " ";
// }
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
2 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
2 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
2 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
36 ms |
4844 KB |
Output is correct |
13 |
Correct |
70 ms |
8300 KB |
Output is correct |
14 |
Correct |
91 ms |
8300 KB |
Output is correct |
15 |
Correct |
86 ms |
8300 KB |
Output is correct |
16 |
Correct |
84 ms |
8556 KB |
Output is correct |
17 |
Correct |
68 ms |
8556 KB |
Output is correct |
18 |
Correct |
71 ms |
8556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
0 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
2 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
36 ms |
4844 KB |
Output is correct |
25 |
Correct |
70 ms |
8300 KB |
Output is correct |
26 |
Correct |
91 ms |
8300 KB |
Output is correct |
27 |
Correct |
86 ms |
8300 KB |
Output is correct |
28 |
Correct |
84 ms |
8556 KB |
Output is correct |
29 |
Correct |
68 ms |
8556 KB |
Output is correct |
30 |
Correct |
71 ms |
8556 KB |
Output is correct |
31 |
Correct |
45 ms |
7660 KB |
Output is correct |
32 |
Correct |
64 ms |
10988 KB |
Output is correct |
33 |
Correct |
82 ms |
12780 KB |
Output is correct |
34 |
Correct |
64 ms |
10988 KB |
Output is correct |
35 |
Correct |
63 ms |
10988 KB |
Output is correct |
36 |
Incorrect |
82 ms |
12268 KB |
Output isn't correct |
37 |
Halted |
0 ms |
0 KB |
- |