#P10214. [CTS2024] 投票游戏

[CTS2024] 投票游戏

Problem Description

There are nn people, numbered from 11 to nn. Person i (2in)i \ (2 \le i \le n) has a hated person fi (1fi<i)f_i \ (1 \le f_i < i). Person 11 has no hated person.

On this day, the nn people play a voting game. One game consists of nn rounds. At the start of the game, no one has been voted out. In each round of the game, the following process is performed:

  1. For each person ii who has not been voted out, they initially have aia_i votes.
  2. Then, for each person ii who has not been voted out, if they have a hated person and their hated person fif_i has not been voted out, then they cast bib_i votes for fif_i.
  3. Finally, vote out the person (among those not yet voted out) with the highest number of votes. If there are multiple people with the highest number of votes, vote out the one with the largest index.

The votes in the nn rounds of one game are counted independently.

Before the game starts, qq events happen. There are two types of events:

  1. Given p,x,yp, x, y, modify (ap,bp)(a_p, b_p) to (x,y)(x, y).
  2. Xiaoming wants to know: given two people c,dc, d, if a game is played at this moment, which one of the two will be voted out first.

As Xiaoming’s friend, can you help him?

Input Format

Read input from standard input.

The first line contains two positive integers n,qn, q, representing the number of people and the number of events.

The second line contains (n1)(n-1) integers f2,f3,,fnf_2, f_3, \cdots, f_n.

The third line contains nn integers a1,a2,,ana_1, a_2, \cdots, a_n.

The fourth line contains nn integers b1,b2,,bnb_1, b_2, \cdots, b_n.

The next qq lines each contain three or four integers describing an event. The first positive integer opop indicates the type of the event.

  • If op=1op = 1, then the next three integers are p,x,yp, x, y, meaning modifying (ap,bp)(a_p, b_p) to (x,y)(x, y).
  • If op=2op = 2, then the next two positive integers are c,dc, d. You need to determine, if a game is played now, which of cc and dd will be voted out first.

Output Format

Write output to standard output.

For each event with op=2op = 2, output one line containing one character in 01. If cc is voted out first, output 0, otherwise output 1.

5 8
1 2 3 2
1 3 2 1 0
0 4 1 0 0
2 1 3
1 1 0 3
2 2 5
1 1 2 2
2 4 3
2 5 4
2 5 1
2 2 1

0
0
1
1
1
1

Hint

For all testdata:

  • 1n,q2×1051 \le n, q \le 2 \times 10^5.
  •  2in, 1fi<i\forall \ 2 \le i \le n, \ 1 \le f_i < i.
  • 0ai,bi,x,y1080 \le a_i, b_i, x, y \le 10^8.
  • 1c,d,pn1 \le c, d, p \le n.
  • cdc \ne d.
Subtask ID Points nn \leq qq \leq Special Property
1 5 500500 None
2 10 30003000
3 2×1052 \times 10^5 fif_i is uniformly random in [1,i1][1, i - 1]
4 15 fi=1f_i = 1
5 30 fi=i1f_i = i - 1
6 None

Translated by ChatGPT 5