luogu#P4679. [ZJOI2011] 道馆之战

    ID: 3673 远端评测题 1000ms 250MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2011线段树各省省选浙江树链剖分

[ZJOI2011] 道馆之战

Problem Description

In Pokémon (also known as Pocket Monsters), in the Water-type Gym in Red/Blue/Green/Emerald, you need to pass through three ice floors to reach the Gym Leader. Each ice block on an ice floor can be stepped on at most once. Only after all ice blocks on the current ice floor have been stepped on will the stairs to the next ice floor open.

The three ice floors are as follows:

After leaving the third ice floor, you can battle the Gym Leader. The Gym Leader thinks this is too easy and many challengers can pass, so to increase the difficulty, the Gym is divided into nn rooms. Between any two rooms there is exactly one path, i.e. these nn rooms form a tree structure. Each room is divided into two areas, AA and BB. Each area is either a thin ice block or an obstacle.

Each move can only go to the same type of area in an adjacent room (that is, if you are currently in area AA of this room, then you can only move to area AA of an adjacent room), or to the other area of the same room.

Now a challenger starts from room uu, and the Gym Leader is in room vv. Then the challenger can only move in the direction that gets closer to the room where the Gym Leader is. At the beginning, the challenger may start on either ice area in room uu. If the number of ice blocks the challenger has stepped on reaches the maximum possible (i.e. there is no strategy that steps on more ice blocks), then when the challenger steps on the last ice block, he will be instantly teleported to the Gym Leader to start the battle.

Since the Gym Leader changed the rules, mm days have passed. Each day, either a challenger comes to challenge, or the Gym Leader modifies a certain room. For each challenger, you need to compute how many ice blocks he needs to step on in order to battle the Gym Leader.

Input Format

The first line contains two positive integers nn and mm.

Lines 22 to nn each contain two positive integers xx and yy, indicating an edge connecting room xx and room yy. Rooms are numbered 1n1\cdots n.

Next follow nn lines, each containing two characters. Line n+kn+k describes the two areas of room kk: the first character is area AA and the second character is area BB. Here, “.” (ASCII code 46) means a thin ice block, and “#” (ASCII code 35) means an obstacle.

Finally, the next mm lines each describe one operation:

CC uu ss: modify the two areas in room uu to ss.

QQ uu vv: query the number of ice blocks the challenger needs to step on to battle the Gym Leader, when the challenger is in room uu and the Gym Leader is in room vv. If both areas in room uu are obstacles, output 00.

Output Format

Output multiple lines, each containing one integer. That is, for each query in the input, output the answer in order.

5 3
1 2
2 3
2 4
1 5
.#
..
#.
.#
..
Q 5 3
C 1 ##
Q 4 5

6
3

Hint

Constraints:

Test points 11~66: n1000,m10000n \le 1000, m \le 10000.

Test points 77~1515: n30000,m80000n \le 30000, m \le 80000.

Test points 1616~2020: n50000,m100000n \le 50000, m \le 100000.

Translated by ChatGPT 5