Fundamental Skills in Data Structures
时间限制:C/C++/Rust/Pascal 4秒,其他语言8秒
空间限制:C/C++/Rust/Pascal 256 M,其他语言512 M
64bit IO Format: %lld

题目描述

“To strengthen the fundamental skills in data structures and gain a profound understanding of the essence of data structures, as well as to implement code effectively, we need to make efforts to study the profound connotation of data structures...”

The ACM Training Base is having a meeting, but after listening to only two sentences, Capoo the cat falls asleep. When he wakes up, he feels at a loss facing the homework left behind. Luckily, he knows a data structure expert, which is you! In order to prevent poor Capoo from being scolded by his senior, you decide to help him complete his data structure homework.

[capoo_confused.jpg](Please imagine it, we can't display it for you due to copyright reasons. T.T)

The homework questions are as follows:

Given a tree rooted at node 1, each node in the tree has a node weight a_i\in \{0,1\}. Now, you need to perform q operations, which can be divided into two types:
  •  1 u v x, which means setting the node weights of all the nodes on the simple path between u and v (inclusive) to x.
  •  2 u, which means querying the number of pairs of nodes (x,y) in the subtree rooted at u such that x < y and a_x \oplus a_y \oplus a_{lca(x, y)}=0 holds.

Here, \oplus represents the bitwise XOR operation, similar to the XOR operator (^) in C/C++.

lca(x,y) refers to the lowest common ancestor of nodes x and y. For example, in Sample 3, lca(4,5)=3.

输入描述:

The first line consists of two integers, n and q (2\leq n \leq 3\times 10^5,1\leq q\leq 3\times 10^5), representing the number of nodes in the tree and the number of operations, respectively.

The second line contains n integers, a_i (a_i\in\{0,1\}), representing the initial node weights.

The third line contains n-1 integers, where the ith integer, f_i (1\leq f_i\leq i), represents the existence of an edge between the (i+1)th node and the f_ith node.

Following are q lines, each representing an operation:

For each operation, the first integer of the line, op (op \in\{1,2\}), denotes the type of operation.
  • If op=1, then the next three integers, u, v, and x (1\leq u,v\leq n,x\in \{0,1\}), represent the two endpoints of the covered chain and the assigned weight for that chain.

  • If op=2, then the next integer, u (1\leq u\leq n), represents the root node of the subtree for which a query is required.


输出描述:

For each query, output a single line containing an integer representing the answer to that query.
示例1

输入

复制
2 1
0 0
1
2 1

输出

复制
1
示例2

输入

复制
3 3
0 0 1
1 1
2 1
1 1 2 1
2 1

输出

复制
1
0
示例3

输入

复制
5 5
1 0 0 1 1
1 1 3 3
1 5 4 1
1 2 4 0
2 3
1 2 1 1
2 1

输出

复制
1
5

说明

For Sample 3, in the first query, after the first two coverings, the node weights of the tree become \{0,0,0,0,1\}, and the valid point pairs in subtree 3 are (3,4).

In the second query, after the first three coverings, the node weights of the tree become \{1,1,0,0,1\}, and the valid point pairs in subtree 1 are (1,3),(1,4),(2,3),(2,4),(3,4).