CF571D.Campus

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Oscolcovo city has a campus consisting of nn student dormitories, nn universities and nn military offices. Initially, the ii -th dormitory belongs to the ii -th university and is assigned to the ii -th military office.

Life goes on and the campus is continuously going through some changes. The changes can be of four types:

  1. University aja_{j} merges with university bjb_{j} . After that all the dormitories that belonged to university bjb_{j} are assigned to to university aja_{j} , and university bjb_{j} disappears.
  2. Military office cjc_{j} merges with military office djd_{j} . After that all the dormitories that were assigned to military office djd_{j} , are assigned to military office cjc_{j} , and military office djd_{j} disappears.
  3. Students of university xjx_{j} move in dormitories. Lets kxjk_{xj} is the number of dormitories that belong to this university at the time when the students move in. Then the number of students in each dormitory of university xjx_{j} increases by kxjk_{xj} (note that the more dormitories belong to the university, the more students move in each dormitory of the university).
  4. Military office number yjy_{j} conducts raids on all the dormitories assigned to it and takes all students from there.

Thus, at each moment of time each dormitory is assigned to exactly one university and one military office. Initially, all the dormitory are empty.

Your task is to process the changes that take place in the campus and answer the queries, how many people currently live in dormitory qjq_{j} .

输入格式

The first line contains two integers, nn and mm (1<=n,m<=5105)(1<=n,m<=5·10^{5}) — the number of dormitories and the number of queries, respectively.

Next mm lines contain the queries, each of them is given in one of the following formats:

  • «U aja_{j} bjb_{j} » — merging universities;
  • «M cjc_{j} djd_{j} » — merging military offices;
  • «A xjx_{j} » — students of university xjx_{j} moving in the dormitories;
  • «Z yjy_{j} » — a raid in military office yjy_{j} ;
  • «Q qjq_{j} » — a query asking the number of people in dormitory qjq_{j} .

All the numbers in the queries are positive integers and do not exceed nn . It is guaranteed that at the moment of the query the universities and military offices, that are present in the query, exist.

输出格式

In the ii -th line print the answer to the ii -th query asking the number of people in the dormitory.

输入输出样例

  • 输入#1

    2 7
    A 1
    Q 1
    U 1 2
    A 1
    Z 1
    Q 1
    Q 2
    

    输出#1

    1
    0
    2
    
  • 输入#2

    5 12
    U 1 2
    M 4 5
    A 1
    Q 1
    A 3
    A 4
    Q 3
    Q 4
    Z 4
    Q 4
    A 5
    Q 5
    

    输出#2

    2
    1
    1
    0
    1
    

说明/提示

Consider the first sample test:

  • In the first query university 1 owns only dormitory 1, so after the query dormitory 1 will have 1 student.
  • After the third query university 1 owns dormitories 1 and 2.
  • The fourth query increases by 2 the number of students living in dormitories 1 and 2 that belong to university number 1. After that 3 students live in the first dormitory and 2 students live in the second dormitory.
  • At the fifth query the number of students living in dormitory 1, assigned to the military office 1, becomes zero.
首页