#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#pragma region dalykai
using p32 = pair<int, int>;
using p32u = pair<uint32_t, uint32_t>;
using p64 = pair<int64_t, int64_t>;
using p64u = pair<uint64_t, uint64_t>;
using vi16 = vector<int16_t>;
using vi16u = vector<uint16_t>;
using vi32 = vector<int>;
using vi32u = vector<uint32_t>;
using vi64 = vector<int64_t>;
using vi64u = vector<uint64_t>;
using vp32 = vector<p32>;
using vp32u = vector<p32u>;
using vp64 = vector<p64>;
using vp64u = vector<p64u>;
using vvi32 = vector<vi32>;
using vvi32u = vector<vi32u>;
using vvi64 = vector<vi64>;
using vvi64u = vector<vi64u>;
using vvp32 = vector<vp32>;
using vvp32u = vector<vp32u>;
using vvp64 = vector<vp64>;
using vvp64u = vector<vp64u>;
#pragma endregion
void dfs(int v, int &count, vvi32 &adj, vp32 &range)
{
range[v].first = count;
count++;
for (auto &next : adj[v])
{
dfs(next, count, adj, range);
}
range[v].second = count;
count++;
}
struct node_t;
deque<node_t> cache;
node_t *get(p32 range)
{
cache.emplace_back(range);
return &cache.back();
}
struct node_t
{
int val;
p32 range;
node_t *left, *right;
node_t(p32 r)
{
val = 0;
range = r;
left = nullptr;
right = nullptr;
}
void expand()
{
if (right || range.second - range.first == 1)
{
return;
}
int mid = (range.second + range.first) / 2;
left = get({range.first, mid});
right = get({mid, range.second});
}
void fix()
{
if (!right || range.second - range.first == 1)
{
return;
}
val = max(left->val, right->val);
}
int intersect(p32 dest)
{
if (dest.first >= range.second || dest.second <= range.first)
{
return 0;
}
if (dest.first <= range.first && range.second <= dest.second)
{
return 1;
}
return 2;
}
};
void update(node_t *node, p32 range, int val)
{
if (!node || !node->intersect(range))
{
return;
}
if (node->intersect(range) == 1)
{
node->val = val;
return;
}
node->expand();
update(node->left, range, val);
update(node->right, range, val);
node->fix();
}
int query(node_t *node, p32 range)
{
if (!node || !node->intersect(range))
{
return 0;
}
if (node->intersect(range) == 1)
{
return node->val;
}
return max(query(node->left, range), query(node->right, range));
}
int main()
{
#ifndef _AAAAAAAAA
//freopen("lmio_2017_3e2_lektuvai_vyr.in", "r", stdin);
//freopen("lmio_2017_3e2_lektuvai_vyr.out", "w", stdout);
#else
freopen("lektuvai.in", "r", stdin);
#ifndef __linux__
atexit([]()
{
freopen("con", "r", stdin);
system("pause"); });
#endif
#endif
int n;
cin >> n;
vi32 time(n);
for (auto &i : time)
{
cin >> i;
}
vvi32 adj(n);
for (int i = 0; i < n - 1; i++)
{
int j;
cin >> j;
j--;
adj[j].push_back(i);
}
vp32 range(n);
int count = 0;
dfs(n - 1, count, adj, range);
node_t *node = get({0, 2 * n});
for (int i = 0; i < n; i++)
{
update(node, {range[i].first, range[i].first + 1}, time[i]);
update(node, {range[i].second, range[i].second + 1}, time[i]);
}
int q;
cin >> q;
for (int i = 0; i < q; i++)
{
char type;
cin >> type;
if (type == 'I')
{
int index;
cin >> index;
index--;
cout << query(node, {range[index].first, range[index].second + 1}) << '\n';
}
else
{
int index, val;
cin >> index >> val;
index--;
p32 r1 = {range[index].first, range[index].first + 1};
p32 r2 = {range[index].second, range[index].second + 1};
int subtree = (int)query(node, {range[index].first, range[index].second + 1});
update(node, r1, subtree + val);
update(node, r2, subtree + val);
}
}
return 0;
}
Compilation message
lektuvai.cpp:8: warning: ignoring '#pragma region dalykai' [-Wunknown-pragmas]
8 | #pragma region dalykai
|
lektuvai.cpp:31: warning: ignoring '#pragma endregion ' [-Wunknown-pragmas]
31 | #pragma endregion
|
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
304 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
440 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
436 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
304 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
440 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
436 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
432 KB |
Output is correct |
17 |
Correct |
2 ms |
312 KB |
Output is correct |
18 |
Correct |
2 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
296 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
304 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
440 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
436 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
198 ms |
31412 KB |
Output is correct |
13 |
Correct |
301 ms |
55132 KB |
Output is correct |
14 |
Correct |
370 ms |
56708 KB |
Output is correct |
15 |
Correct |
421 ms |
56340 KB |
Output is correct |
16 |
Correct |
451 ms |
56436 KB |
Output is correct |
17 |
Correct |
339 ms |
56148 KB |
Output is correct |
18 |
Correct |
356 ms |
56152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
432 KB |
Output is correct |
6 |
Correct |
2 ms |
312 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
296 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
304 KB |
Output is correct |
16 |
Correct |
2 ms |
256 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
2 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
440 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
436 KB |
Output is correct |
23 |
Correct |
2 ms |
340 KB |
Output is correct |
24 |
Correct |
198 ms |
31412 KB |
Output is correct |
25 |
Correct |
301 ms |
55132 KB |
Output is correct |
26 |
Correct |
370 ms |
56708 KB |
Output is correct |
27 |
Correct |
421 ms |
56340 KB |
Output is correct |
28 |
Correct |
451 ms |
56436 KB |
Output is correct |
29 |
Correct |
339 ms |
56148 KB |
Output is correct |
30 |
Correct |
356 ms |
56152 KB |
Output is correct |
31 |
Correct |
232 ms |
28288 KB |
Output is correct |
32 |
Correct |
297 ms |
43212 KB |
Output is correct |
33 |
Correct |
372 ms |
56748 KB |
Output is correct |
34 |
Correct |
300 ms |
55048 KB |
Output is correct |
35 |
Correct |
296 ms |
48972 KB |
Output is correct |
36 |
Correct |
401 ms |
40808 KB |
Output is correct |
37 |
Correct |
421 ms |
44416 KB |
Output is correct |
38 |
Correct |
256 ms |
24796 KB |
Output is correct |
39 |
Correct |
338 ms |
43748 KB |
Output is correct |
40 |
Correct |
333 ms |
37536 KB |
Output is correct |