#P4332. [SHOI2014] 三叉神经树

    ID: 3265 远端评测题 4000ms 250MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>模拟2014线段树各省省选上海O2优化分治树链剖分,树剖Link-Cut Tree,LCT

[SHOI2014] 三叉神经树

Description

Computational neuroscience, as an emerging interdisciplinary field, has been a research hotspot in recent years. A kind of neural tissue called SHOI has attracted great attention because of its close relationship with a recently discovered compound, SHTSC.

A SHOI tissue consists of several SHOI cells forming a strict tree structure. Each SHOI cell has exactly one output terminal, called an axon. Except for a special SHOI cell called the root cell whose output serves as the output of the entire tissue, the axons of all other cells connect to their parent SHOI cell. Each cell also has exactly three input terminals, called dendrites, which receive information from its child cells or from other neural tissues. The signaling mechanism of SHOI cells is simple, with only 00 and 11. Each SHOI cell outputs the majority value among the three input signals, i.e., whichever of 00 or 11 appears more times among its three inputs.

You are given the structure of a SHOI tissue and the changes of external neural inputs. Please simulate the output of the SHOI tissue.

Input Format

  • The first line contains an integer nn, the total number of SHOI cells. Cells are numbered 1n1 \sim n, and cell 11 is the root cell.
  • The next nn lines each contain three integers x1,x2,x3x_1, x_2, x_3, describing the dendrite connections of cells 1n1 \sim n, respectively. If 1<xin1 < x_i \leq n, it connects to the axon of cell xix_i. If n<xi3n+1n < x_i \leq 3n+1, it connects to the external input with index xix_i. The input guarantees that the given SHOI tissue is valid, and for each cell the three xix_i are pairwise distinct.
  • The next line contains 2n+12n+1 integers (each 00 or 11), giving the initial values of the external inputs in the order of indices n+1,n+2,,3n+1n+1, n+2, \dots, 3n+1.
  • The next line contains an integer qq, the number of operations.
  • The next qq lines each contain one integer xx, indicating that the external input with index xx (where n<x3n+1n < x \leq 3n+1) toggles its value.

Output Format

Output qq lines. For the ii-th change of an external input, print one integer: the output of the root cell after this change.

3
2 3 4
5 6 7
8 9 10
0 0 0 0 1 1 1
5
4
4
5
6
8

1
0
0
1
1

Hint

  • For 10%10 \% of the testdata, 1n1031 \leq n \leq 10^3, 1q1031 \leq q \leq 10^3.
  • For 30%30 \% of the testdata, 1n1051 \leq n \leq 10^5, 1q1051 \leq q \leq 10^5.
  • For 100%100 \% of the testdata, 1n5×1051 \leq n \leq 5 \times 10^5, 1q5×1051 \leq q \leq 5 \times 10^5.

Translated by ChatGPT 5