#P2486. [SDOI2011] 染色

    ID: 1500 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2011线段树各省省选山东动态树树链剖分,树剖

[SDOI2011] 染色

Description

Given an unrooted tree with nn nodes, there are mm operations of two types:

  1. Color all nodes on the path from node aa to node bb (including aa and bb) with color cc.
  2. Query the number of color segments on the path from node aa to node bb.

A color segment is defined as a maximal consecutive run of the same color. For example, 112221 consists of three segments: 11, 222, 1.

Input Format

The first line contains two integers separated by a space, representing the number of nodes nn and the number of operations mm.

The second line contains nn integers separated by spaces. The ii-th integer wiw_i represents the initial color of node ii.

Lines 33 through (n+1)(n + 1) each contain two integers u,vu, v separated by a space, indicating that there is an edge between nodes uu and vv in the tree.

Lines (n+2)(n + 2) through (n+m+1)(n + m + 1) each describe an operation in the following format:

Each line starts with a character opop, indicating the type of operation.

  • If opop is C, it denotes a coloring operation. After a space, there are three integers a,b,ca, b, c separated by spaces, meaning that all nodes on the path from aa to bb are colored with color cc.
  • If opop is Q, it denotes a query operation. After a space, there are two integers a,ba, b separated by a space, indicating a query for the number of color segments on the path from aa to bb.

Output Format

For each query operation, output a single line containing one integer representing the answer.

6 5
2 2 1 2 1 1
1 2
1 3
2 4
2 5
2 6
Q 3 5
C 2 1 1
Q 3 5
C 5 1 2
Q 3 5

3
1
2

Hint

Constraints

For 100%100\% of the testdata, 1n,m1051 \leq n, m \leq 10^5, 1wi,c1091 \leq w_i, c \leq 10^9, 1a,b,u,vn1 \leq a, b, u, v \leq n, opop is guaranteed to be C or Q, and the given graph is a tree.

In addition to the original testdata, there exists a non-scoring set of hack testdata.

Translated by ChatGPT 5