#P69. [CSP-S 2019 Day 1] Bracket Tree

[CSP-S 2019 Day 1] Bracket Tree

Person in Charge

Problem Background

The definition of a valid parentheses string in this problem is as follows:

  1. () is a valid parentheses string.
  2. If A is a valid parentheses string, then (A) is a valid parentheses string.
  3. If A and B are valid parentheses strings, then AB is a valid parentheses string.

The definitions of substring and distinct substrings in this problem are as follows:

  1. A substring of string S is a string composed of consecutive arbitrary number of characters in S. A substring of S can be represented by the starting position ll and ending position rr, denoted as S(l,r)S (l, r) (1lrS1 \le l \le r \le |S|, where S|S| represents the length of SS).
  2. Two substrings of S are considered distinct 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 there is exactly one simple path between any two nodes that is reachable from each other.

Little Q is a curious child. One day on his way to school, he encountered a tree of size nn, where the nodes are numbered from 11 to nn, and node 11 is the root of the tree. Except for node 11, each node has a parent node: the parent of node uu (2un2 \le u \le n) is node fuf_u (1fu<u1 \le f_u < u).

Little Q found that each node of the tree has exactly one parenthesis, which can be ( or ). Little Q defines sis_i as the string formed by arranging the parentheses on the simple path from the root node to node ii in the order of the nodes passed.

Obviously, sis_i is a parentheses string, but not necessarily a valid one. Therefore, Little Q now wants to find, for each ii (1in1\le i\le n), the number of distinct substrings of sis_i that are valid parentheses strings.

This problem stumped Little Q, so he had to ask for your help. Let kik_i be the number of distinct substrings of sis_i that are valid parentheses strings. You only need to tell Little Q the XOR sum of all i×kii \times k_i, i.e.:

$$(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} denotes the bitwise XOR operation.

Input Format

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

The second line contains a parentheses string of length nn composed of ( and ), where the ii-th parenthesis represents the parenthesis on node ii.

The third line contains n1n - 1 integers, where the ii-th integer (1i<n1 \le i \lt n) represents the parent number fi+1f_{i+1} of node i+1i + 1.

Output Format

Output a single integer representing the answer.

Samples

5
(()()
1 1 2 2
6

Sample 1 Explanation

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

  • The string formed by arranging the parentheses on the simple path from the root to node 11 is (, and the number of substrings that are valid parentheses strings is 00.
  • The string for the path from the root to node 22 is ((, and the number of valid parentheses substrings is 00.
  • The string for the path from the root to node 33 is (), and the number of valid parentheses substrings is 11.
  • The string for the path from the root to node 44 is (((, and the number of valid parentheses substrings is 00.
  • The string for the path from the root to node 55 is ((), and the number of valid parentheses substrings is 11.

Sample 2

See brackets/brackets2.in and brackets/brackets2.ans in the contestant's directory.

Sample 3

See brackets/brackets3.in and brackets/brackets3.ans in the contestant's directory.

Data Range

Test Case 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

For 100%100\% of the data: 1n5×1051 \le n \le 5 \times 10^5, 1fii11 \le f_i \le i-1.