2325: [zjoi2011] Battle of the pavilion

Source: Internet
Author: User
2325: [zjoi2011] battle time limit: 40 sec memory limit: 256 MB
Submit: 813 solved: 309
[Submit] [Status] Description

The water system pavilions in the Pokemon (also known as Pokémon or Pokemon) Red/blue/Green Gems must pass through three ice areas before they can reach the main site. Each ice in the ice area can only go through once. When all ice cubes on an ice ground are passed, the stairs to the next ICE ground are opened.

The three regions are as follows:

 

After leaving the third ice, you can fight with the pavilion owner.

The hall owner finds that this difficulty is too small, which often leads to challengers passing through. To increase the difficulty, the hall is divided into N rooms. Each room contains two ice cubes or obstacles, indicating a column of ice. Any two rooms are connected by only one path, that is, the N rooms constitute a tree structure.

Each room is divided into areas A and B, and each area is a thin ice or obstacle. Each time, you can only move to the same area of the adjacent room (that is, if you are in the area of the room, you can only move to the area of the adjacent room) or another area of the room.

Now, the challenger starts from room U and the main is in Room V. Then, the Challenger can only go in the direction close to the main room. At the beginning, the Challenger can be in any ice area of room U. If the number of ice cubes that the Challenger has stepped on reaches the maximum (that is, there is no way to step on more ice cubes), then when the challenger reaches the last ice cube, he will be instantly transferred to the main in front of the main hall to fight with the main hall.

It has been M days since the owner modified the rules. Every day, either a challenger or the owner modified a room. For each challenger, You need to calculate the number of ice required for him to fight with the host.

 

Input

The first line contains two positive integers n and M.

Line 2 to line N, each line contains two positive integers x and y, indicating an edge connecting room X and room y. The room number is 1... N.

The next n lines contain two characters. Line N + k indicates the two regions of room K. The first character is Area A and the second character is area B. "." (ASCII code 46) indicates ice cubes, and "#" (ASCII code 35) indicates obstacles.

The last m row has one operation per row:

L c u s: Change the two areas in the room u to S.

L q u v: Ask the challenger in the room U. when the subject is in the room V, the Challenger can challenge the number of ice required to be stepped on with the subject. If both areas of room u are obstacles, the output is 0.

Output

Contains several rows. Each row has an integer. That is, one answer is output for each query in the input.

Sample input5 3

1 2

2 3

2 4

1 5

.#

..

#.

.#

..

Q 5 3

C 1 ##

Q 4 5

Sample output6

3

Hint

[Example]


In the first query, the path with the largest number of ice cubes going through from Room 5 is:

The second question is:


[Data scale]




N ≤ 30 000



M ≤ 80 000



Source

Day2

 

The question is so painful ......

To sum up, there is a tree where each vertex has two values, A and B, and the open space or obstacle means that the obstacle cannot pass. In the same node, A can go to B, but a is an obstacle or B is an obstacle.

Ask how many open spaces can be accessed from X to Y and from (a, B. That is, you do not need to be able to reach Y. You only need to find the maximum number of steps.

I read people's questions, X [0 .. 1] [0 .. 1] indicates the maximum number of steps to be taken from the Left/top right to the left/top right, that is, the number of steps to be taken must be reached.

L [0 .. 1] indicates how many steps can be taken from the Left/top right corner. R [0 .. 1] is the same

Look at the UPDATE function. It is not very troublesome, but pay attention to the details.

In addition, when the tree chain is split to calculate the answer, because each line segment of you is down from the root

But the path from X to Y is from bottom up, and the path from LCA to Y is from top down,

That is to say, the path from X to LCA needs to be reversed !!!!!

Mom, I only swap L [0], R [0], L [1], and R [1]

I need swap X [0] [1] and X [1] [0 !!!! Renewed for an hour and a half

/* Author: wuw.jun */# include <cstdio> # include <cstring> # include <cstdlib> # include <algorithm> # define lson L, mid, RT <1 # define rson Mid + 1, R, RT <1 | 1 using namespace STD; typedef long ll; typedef double dd; const int maxn = 100010, INF = 1000000007; int edge, N, Fa [maxn], SZ [maxn], son [maxn], DEP [maxn], hash [maxn], top [maxn]; int H [maxn], num, a [maxn], X, Y, TX, Ty, Q, B [maxn]; char s [22], S1 [22 ], S2 [22]; struct edge {int to, Ne;} e [maxn * 2]; struct seg {int X [2] [2], L [2], R [2]; void clear () {x [0] [0] = x [0] [1] = x [1] [0] = x [1] [1] = 0; L [0] = L [1] = R [0] = R [1] = 0 ;}} seg [maxn <2], ANS, C, L, R, RET; void close () {exit (0);} void addedge (int x, int y) {e [edge]. to = y; E [edge]. ne = H [X]; H [x] = edge ++;} void DFS (int K, int from) {SZ [k] = 1; son [k] = 0; Dep [k] = Dep [from] + 1; for (int p = H [k]; P! =-1; P = E [p]. ne) {int to = E [p]. to; If (from = to) continue; Fa [to] = K; DFS (to, k); SZ [k] + = SZ [to]; if (SZ [to]> SZ [son [k]) Son [k] = to;} void build (int K, int from) {hash [k] = ++ num; top [k] = from; If (son [k]) Build (son [K], from ); for (int p = H [k]; P! =-1; P = E [p]. Ne) {int to = E [p]. To; If (! = Fa [k] &! = Son [k]) Build (to, to) ;}// {segment part int CAL (int x, int y) {return max (-INF, max (x, y);} seg operator + (seg a, seg B) {if (. X [0] [0] = 0 &. X [0] [1] = 0 &. X [1] [0] = 0 &. X [1] [1] = 0 &. L [0] = 0 &. L [1] = 0 &. R [0] = 0 &. R [1] = 0) return B; If (B. X [0] [0] = 0 & B. X [0] [1] = 0 & B. X [1] [0] = 0 & B. X [1] [1] = 0 & B. L [0] = 0 & B. L [1] = 0 & B. R [0] = 0 & B. R [1] = 0) Return a; C. X [0] [0] = CAL (. X [0] [0] + B. X [0] [0],. X [0] [1] + B. X [1] [0]); C. X [0] [1] = CAL (. X [0] [0] + B. X [0] [1],. X [0] [1] + B. X [1] [1]); C. X [1] [0] = CAL (. X [1] [0] + B. X [0] [0],. X [1] [1] + B. X [1] [0]); C. X [1] [1] = CAL (. X [1] [0] + B. X [0] [1],. X [1] [1] + B. X [1] [1]); C. L [0] = max (. L [0], max (. X [0] [0] + B. L [0],. X [0] [1] + B. L [1]); C. L [1] = max (. L [1], max (. X [1] [0] + B. L [0],. X [1] [1] + B. L [1]); C. R [0] = max (B. R [0], max (B. X [0] [0] +. R [0], B. X [1] [0] +. R [1]); C. R [1] = max (B. R [1], max (B. X [0] [1] +. R [0], B. X [1] [1] +. R [1]); Return C;} void change (int l, int R, int val1, int val2, int L, int R, int RT) {If (L <= L & R <= r) {seg [RT]. X [0] [0] = seg [RT]. L [0] = seg [RT]. R [0] = val1; seg [RT]. X [1] [1] = seg [RT]. L [1] = seg [RT]. R [1] = val2; If (val1 = 1 & val2 = 1) {seg [RT]. L [0] = seg [RT]. L [1] = Se G [RT]. R [0] = seg [RT]. R [1] = 2; seg [RT]. X [0] [1] = seg [RT]. X [1] [0] = 2;} else {seg [RT]. X [0] [1] = seg [RT]. X [1] [0] =-INF;} seg [RT]. L [0] = max (SEG [RT]. L [0], 0); seg [RT]. L [1] = max (SEG [RT]. L [1], 0); seg [RT]. R [0] = max (SEG [RT]. R [0], 0); seg [RT]. R [1] = max (SEG [RT]. R [1], 0); return;} int mid = (L + r)> 1; if (L <= mid) Change (L, R, val1, val2, lson); If (Mid + 1 <= r) Change (L, R, val1, val2, rson); seg [RT] = Seg [RT <1] + seg [RT <1 | 1];} seg query (int l, int R, int L, int R, int RT) {If (L <= L & R <= r) {return seg [RT];} int mid = (L + r)> 1; seg ans; ans. clear (); If (L <= mid) ans = ans + query (L, R, lson); If (Mid + 1 <= r) ans = ans + query (L, R, rson); Return ans ;}//}}} seg get_ans () {Tx = top [x]; ty = top [y]; L. clear (); R. clear (); While (TX! = Ty) {If (DEP [TX] <Dep [ty]) {r = query (hash [ty], hash [Y], 1, n, 1) + R; y = Fa [ty]; ty = top [y];} else {L = query (hash [TX], hash [X], 1, n, 1) + L; X = Fa [TX]; Tx = top [x] ;}} if (DEP [x] <Dep [y]) {r = query (hash [X], hash [Y], 1, n, 1) + R;} else {L = query (hash [Y], hash [X], 1, n, 1) + L;} swap (L. X [0] [1], L. X [1] [0]); swap (L. L [0], L. R [0]); swap (L. L [1], L. R [1]); Return L + R;} void Init () {s CANF ("% d", & N, & Q); memset (H,-1, sizeof (h); For (INT I = 1; I <= n-1; I ++) {scanf ("% d", & X, & Y); addedge (x, y); addedge (Y, x) ;}for (INT I = 1; I <= N; I ++) {scanf ("% s", S1 ); A [I] = S1 [0] = '. '? 1:-INF; B [I] = S1 [1] = '.'? 1:-INF;} DFS (1, 0); Build (1, 1);/* For (INT I = 1; I <= N; I ++) {printf ("I: % d top: % d hash: % d \ n", I, top [I], hash [I]);} */For (INT I = 1; I <= N; I ++) Change (hash [I], hash [I], a [I], B [I], 1, n, 1); While (Q --) {scanf ("% s", S); If (s [0] = 'q ') {scanf ("% d", & X, & Y); ret = get_ans (); printf ("% d \ n", max (Ret. L [0], Ret. L [1]);} else {scanf ("% d", & X); scanf ("% s", S1 ); int T1 = S1 [0] = '. '? 1:-INF; int t2 = S1 [1] = '.'? 1:-INF; change (hash [X], hash [X], T1, T2, 1, n, 1) ;}} int main () {Init (); close (); Return 0 ;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.