#P3313. [SDOI2014] 旅行

    ID: 2362 远端评测题 1000ms 512MiB 尝试: 1 已通过: 0 难度: 7 上传者: 标签>2014线段树山东树链剖分,树剖

[SDOI2014] 旅行

Description

Country S has NN cities, numbered from 11 to NN. The cities are connected by N1N-1 undirected roads, forming a structure where starting from any city, one can reach all other cities. Each city believes in a different religion; common beliefs include the Church of the Flying Spaghetti Monster, the Invisible Pink Unicorn, and Jediism.

For convenience, we use different positive integers to represent various religions. Residents of Country S often travel. They always take the shortest path, and to avoid trouble, they only stay overnight in cities whose religion is the same as theirs. Of course, the destination of the trip also has the same religion as the traveler. Country S assigns a distinct travel rating to each city. Travelers often record the sum or the maximum of the ratings of the cities where they stayed overnight along the way, including the start and end cities.

In the history of Country S, the following types of events often occur:

  • CC x c: All residents of city xx convert to religion cc.
  • CW x w: The rating of city xx is adjusted to ww.
  • QS x y: A traveler starts from city xx, goes to city yy, and records the sum of the ratings of the cities where they stayed overnight along the way.
  • QM x y: A traveler starts from city xx, goes to city yy, and records the maximum of the ratings of the cities where they stayed overnight along the way.

The recorded numbers have been lost, but the religion and rating of each city before the records began, as well as the event log itself, are intact. Based on this information, restore the numbers that the travelers recorded. For convenience, we assume that the interval between events is long enough so that during any single trip, all cities’ ratings and religions remain unchanged.

Input Format

The first line contains the integers N,QN, Q, representing the number of cities and the number of events, respectively.

The next NN lines: on the (i+1)(i+1)-th line, two integers Wi,CiW_i, C_i denote the initial rating and religion of city ii before the records began.

The next N1N-1 lines each contain two integers x,yx, y, indicating an undirected road between cities xx and yy.

The next QQ lines each contain one operation, in one of the formats described above.

Output Format

For each QS and QM event, output one line containing the number recorded by the traveler.

5 6
3 1
2 3
1 2
3 3
5 1
1 2
1 3
3 4
3 5
QS 1 5
CC 3 1
QS 1 5
CW 3 3
QS 1 5
QM 2 4
8
9
11
3

Hint

For 100%100\% of the testdata, N,Q105N, Q \le 10^5, C105C \le 10^5.

It is guaranteed that for all QS and QM events, the start and end cities have the same religion. At any time, a city’s rating is a positive integer not exceeding 10410^4, and the religion value does not exceed CC.

Translated by ChatGPT 5