RiseOJ는 solved.ac와 제휴 관계가 없습니다. 티어 아이콘 © solved.ac. solved.ac
포럼
문제 COCI00692

Stablo

설명

Toni decided to create a task for HONI (and COCI). Since he doesn’t like kids, he decided to make the
task as difficult as possible. He came up with a complex problem involving a tree that constantly changes,
solely to make contestants suffer as much as possible.
You are given a weightless tree with \(N\) nodes, where the root of the tree is node 1. Each node has an
associated value \(v\)[\(i\)]. The structure of the tree is defined using an array \(p\)[\(i\)], where for each \(i\) from 1 to
\(N - 1\), \(p\)[\(i\)] denotes the parent of \(i + 1\).
A function \(f\)(\(y\)) is defined for a node \(y\) in the tree as:
\(f\)(\(y\)) =
X
\(x\)\(S_{y}\)
\(d\)(x, y) · \(v\)[\(x\)]
where \(d\)(x, y) denotes the distance between nodes \(x\) and \(y\), while \(S_{y}\) contains all nodes for which \(y\) is an
ancestor.
You are given \(Q\) queries with two nodes \(x\) and \(y\). For each query, the following transformation must be
simulated in the tree, and the function \(f\)(\(y\)) needs to be calculated:
1. Attach all nodes for which \(x\) is the parent to the parent of \(x\)
2. Remove \(x\) from the tree
3. Insert node \(x\) back into the tree, between \(y\) and the descendant of \(y\) from whose subtree \(x\) was
removed.
If \(y\) is the parent of \(x\), the tree remains unchanged. It is always true that \(x\) is in the subtree of \(y\). For
each query, the value of \(f\)(\(y\)) must be calculated after the tree is temporarily modified according to the
procedure described above. The tree modifications are not permanent, i.e. after each query, the tree
returns to its original state.

제약

Subtask 1 (21 points): \(1 \le N\), \(Q \le 1000\)

Subtask 2 (37 points): Tree is a chain, \(p\)[\(i\)] = \(i\) for every \(i\) from 1 to \(N - 1\)

Subtask 3 (22 points): Each node will be the parent of at most 20 nodes

Subtask 4 (40 points): No additional constraints.

입력 형식

The first line contains two integers \(N\) and \(Q\) (\(1 \le N\), \(Q \le 5 \cdot 10^{5}\)), the number of nodes in the tree and
the number of queries, respectively.
The second line contains \(N\) integers \(v\)[\(i\)] (\(1 \le v\)[\(i\)] ≤\(10^{6}\)), representing the value of each node.
The third line contains \(N - 1\) integers \(p\)[\(i\)] (\(1 \le p\)[\(i\)] ≤\(i\)), where \(p\)[\(i\)] denotes the parent of node \(i + 1\).
Each of the next \(Q\) lines contains two integers \(x\) and \(y\) (\(1 \le x\), \(y \le N\)), denoting the nodes involved in the
operation described above.

출력 형식

In the next \(Q\) lines output the value of the function \(f\)(\(y\)) on the modified tree.

예제 1
입력
3 1
1 2 3
1 2
3 1
출력
7
예제 2
입력
3 2
4 5 6
1 1
2 1
3 1
출력
11
11
예제 3
입력
5 3
2 5 2 2 2
1 2 3 2
4 3
3 2
5 1
출력
2
8
26
설명

Clarification of the first example: After applying the operation on a tree, node 3 is at a distance of 1
from node 1, and node 2 is at a distance of 2 from node 1. The result is 3 + 2 · 2 = 7.

문제 정보

생성자가 기록되지 않았습니다.

출처 COCI 2024/2025 Contest 3

평가 및 의견

Stablo

개요
출제자 난이도 Unrated 레이팅 미적용 의견 0 / 50 공개 집계 (커뮤니티 난이도, 주요 주제, 품질)는 의견이 충분히 모이면 공개됩니다.

Log in to rate problems.

개별 의견

아직 의견이 없습니다. 자격이 된다면 위 양식에서 가장 먼저 평가해 보세요.

풀이 제출

Stablo

게스트로 둘러보고 있습니다. 로그인하면 풀이를 제출하고 진행 상황을 확인할 수 있습니다. 로그인하고 제출하기
공개
C++20 Tab 들여쓰기 · Ctrl+/ 주석 토글 · Enter 자동 들여쓰기
1 1 1 0 공백: 4 · UTF-8