#P5658. [CSP-S 2019] 括号树

[CSP-S 2019] 括号树

Background

In this problem, a valid bracket sequence is defined as follows:

  1. () is a valid bracket sequence.
  2. If A is a valid bracket sequence, then (A) is a valid bracket sequence.
  3. If A and B are valid bracket sequences, then AB is a valid bracket sequence.

In this problem, the definitions of a substring and distinct substrings are as follows:

  1. A substring of a string S is a string formed by any continuous characters in S. A substring of S can be represented by the starting position ll and the ending position rr, denoted as S(l,r)S (l, r) (1lrS1 \leq l \leq r \leq |S |, where S|S | denotes the length of S).
  2. Two substrings of S are considered different if and only if their positions in S are different, i.e. ll is different or rr is different.

Problem Description

A tree of size nn contains nn nodes and n1n - 1 edges. Each edge connects two nodes, and between any two nodes there exists exactly one simple path.

Xiao Q is a curious kid. One day on his way to school, he encountered a tree of size nn. The nodes of the tree are numbered from 1n1 \sim n, and node 11 is the root. Except for node 11, every node has a parent node. The parent of node uu (2un2 \leq u \leq n) is node fuf_u (1fu<u1 \le f_u < u).

Xiao Q found that each node of the tree has exactly one bracket, either ( or ). Xiao Q defines sis_i as follows: take the brackets on the simple path from the root to node ii, and arrange them into a string in the order the nodes are visited.

Obviously, sis_i is a bracket string, but it is not necessarily a valid bracket sequence. Now Xiao Q wants, for all ii (1in1 \leq i \leq n), to find how many distinct substrings of sis_i are valid bracket sequences.

This problem stumped Xiao Q, so he asks you for help. Suppose sis_i has kik_i distinct substrings that are valid bracket sequences. You only need to tell Xiao Q the XOR sum of all i×kii \times k_i, namely:

$$(1 \times k_1)\ \text{xor}\ (2 \times k_2)\ \text{xor}\ (3 \times k_3)\ \text{xor}\ \cdots\ \text{xor}\ (n \times k_n)$$

where xor\text{xor} is the bitwise XOR operation.

Input Format

The first line contains an integer nn, denoting the size of the tree.

The second line contains a bracket string of length nn consisting of ( and ). The ii-th bracket denotes the bracket on node ii.

The third line contains n1n − 1 integers. The ii-th integer (1i<n1 \leq i \lt n) denotes the parent index fi+1f_{i+1} of node i+1i + 1.

Output Format

Only one line containing one integer, denoting the answer.

5
(()()
1 1 2 2
6

Hint

[Sample 1 Explanation]

The shape of the tree is shown in the figure below:

The string formed by the brackets on the simple path from the root to node 1, arranged in order, is (, and the number of substrings that are valid bracket sequences is 00.

The string from the root to node 2 is ((, and the number of substrings that are valid bracket sequences is 00.

The string from the root to node 3 is (), and the number of substrings that are valid bracket sequences is 11.

The string from the root to node 4 is (((, and the number of substrings that are valid bracket sequences is 00.

The string from the root to node 5 is ((), and the number of substrings that are valid bracket sequences is 11.

[Sample 2]

See brackets/brackets2.in and brackets/brackets2.ans under the contestant directory.

[Constraints]

::cute-table{tuack}

Test Point ID nn\le Special Property
121\sim 2 88 fi=i1f_i=i-1
343\sim 4 200200 ^
575\sim 7 20002000
8108\sim 10 ^ None
111411\sim 14 10510^5 fi=i1f_i=i-1
151615\sim 16 ^ None
172017\sim 20 5×1055\times 10^5 ^

Translated by ChatGPT 5