CF571D.Campus

NOI/NOI+/CTSC

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

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

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

  1. University a__j merges with university b__j. After that all the dormitories that belonged to university b__j are assigned to to university a__j, and university b__j disappears.
  2. Military office c__j merges with military office d__j. After that all the dormitories that were assigned to military office d__j, are assigned to military office c__j, and military office d__j disappears.
  3. Students of university x__j move in dormitories. Lets k__x__j 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 x__j increases by k__x__j (note that the more dormitories belong to the university, the more students move in each dormitory of the university).
  4. Military office number y__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 q__j.

奥索科洛沃市拥有一座校园,其中包括 nn 座学生宿舍、nn 所大学和 nn 个军事办公室。最初,第 ii 座宿舍隶属于第 ii 所大学,并被分配给第 ii 个军事办公室。

生活不断变化,校园也持续经历若干调整。这些调整共分为四种类型:

  1. 第 aja_j 所大学与第 bjb_j 所大学合并。此后,所有原属于第 bjb_j 所大学的宿舍全部划归第 aja_j 所大学,而第 bjb_j 所大学则被撤销。
  2. 第 cjc_j 个军事办公室与第 djd_j 个军事办公室合并。此后,所有原分配给第 djd_j 个军事办公室的宿舍全部改由第 cjc_j 个军事办公室管辖,而第 djd_j 个军事办公室则被撤销。
  3. 第 xjx_j 所大学的学生入住其所属宿舍。设 kxjk_{x_j} 表示在此次入住发生时,隶属于该大学的宿舍数量;则该大学每座宿舍中的学生人数均增加 kxjk_{x_j}(注意:该大学所辖宿舍越多,每座宿舍新增的学生人数就越多)。
  4. 第 yjy_j 个军事办公室对其所管辖的所有宿舍发动突袭,并将其中全部学生带走。

因此,在任意时刻,每座宿舍都恰好隶属于一所大学且被分配给一个军事办公室。初始状态下,所有宿舍均为空。

你的任务是处理校园中发生的各类变更,并回答查询:当前宿舍 qjq_j 中居住着多少人?

输入格式

The first line contains two integers, n and m (1 ≤ n, m ≤ 5·105) — the number of dormitories and the number of queries, respectively.

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

  • «U a__j b__j» — merging universities;
  • «M c__j d__j» — merging military offices;
  • «A x__j» — students of university x__j moving in the dormitories;
  • «Z y__j» — a raid in military office y__j;
  • «Q q__j» — a query asking the number of people in dormitory q__j.

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

第一行包含两个整数 nn 和 mm(1≤n,m≤5⋅1051 \leq n, m \leq 5 \cdot 10^5),分别表示宿舍楼的数量和查询的数量。

接下来的 mm 行包含各条查询,每条查询为以下格式之一:

  • «U aja_j bjb_j» — 合并两所大学;
  • «M cjc_j djd_j» — 合并两个军事办公室;
  • «A xjx_j» — 大学 xjx_j 的学生入住宿舍楼;
  • «Z yjy_j» — 对军事办公室 yjy_j 发动突袭;
  • «Q qjq_j» — 查询宿舍楼 qjq_j 中的人数。

所有查询中的数字均为正整数,且均不超过 nn。保证在执行查询时,查询中涉及的大学和军事办公室均存在。

输出格式

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

在第 ii 行输出第 ii 个查询的答案,即宿舍中的人数。

输入输出样例

  • 输入#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.

考虑第一个样例测试:

  • 在第一个查询中,大学 1 仅拥有宿舍 1,因此该查询后宿舍 1 将有 1 名学生。
  • 在第三个查询后,大学 1 拥有宿舍 1 和宿舍 2。
  • 第四个查询使属于大学 1 的宿舍 1 和宿舍 2 中的学生人数各增加 2。此后,宿舍 1 中有 3 名学生,宿舍 2 中有 2 名学生。
  • 在第五个查询中,分配给军事办公室 1 的宿舍 1 中的学生人数变为 0。

输入解题思路,AI测评打分。不知道怎么写?

首页