CF1810F.M-tree

省选/NOI-

通过率:0%

时间限制:2.50s

内存限制:256MB

AC君温馨提醒

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

题目描述

A rooted tree is called good if every vertex of the tree either is a leaf (a vertex with no children) or has exactly mm children.

For a good tree, each leaf uu has a positive integer cuc_{u} written on it, and we define the value of the leaf as cu+depuc_{u} + \mathrm{dep}_{u}, where depu\mathrm{dep}_{u} represents the number of edges of the path from vertex uu to the root (also known as the depth of uu). The value of a good tree is the maximum value of all its leaves.

Now, you are given an array of nn integers a1,a2,…,ana_{1}, a_{2}, \ldots, a_{n}, which are the integers that should be written on the leaves. You need to construct a good tree with nn leaves and write the integers from the array aa to all the leaves. Formally, you should assign each leaf uu an index bub_{u}, where bb is a permutation of length nn, and represent that the integer written on leaf uu is cu=abuc_u = a_{b_{u}}. Under these constraints, you need to minimize the value of the good tree.

You have qq queries. Each query gives you xx, yy and changes axa_{x} to yy, and after that, you should output the minimum value of a good tree based on the current array aa.

A permutation of length nn is an array consisting of nn distinct integers from 11 to nn in arbitrary order. For example, [2,3,1,5,4][2,3,1,5,4] is a permutation, but [1,2,2][1,2,2] is not a permutation (22 appears twice in the array), and [1,3,4][1,3,4] is also not a permutation (n=3n=3 but there is 44 in the array).

一棵有根树被称为“好树”,当且仅当该树的每个顶点要么是叶节点(即没有子节点的顶点),要么恰好有 mm 个子节点。

对于一棵好树,每个叶节点 uu 上写有一个正整数 cuc_{u},我们定义该叶节点的值为 cu+depuc_{u} + \mathrm{dep}_{u},其中 depu\mathrm{dep}_{u} 表示从顶点 uu 到根节点的路径上的边数(也称为 uu 的深度)。一棵好树的值定义为它所有叶节点的值中的最大值。

现在,你被给定一个包含 nn 个整数的数组 a1,a2,…,ana_{1}, a_{2}, \ldots, a_{n},这些整数将被写在叶节点上。你需要构造一棵具有 nn 个叶节点的好树,并将数组 aa 中的整数一一写在所有叶节点上。形式化地说,你需要为每个叶节点 uu 分配一个下标 bub_{u},其中 bb 是一个长度为 nn 的排列,使得叶节点 uu 上所写的整数为 cu=abuc_u = a_{b_{u}}。在这些约束条件下,你需要使该好树的值最小化。

你将处理 qq 次查询。每次查询给出 xx 和 yy,并将 axa_{x} 修改为 yy;此后,你需要输出基于当前数组 aa 所能构造出的最小好树的值。

一个长度为 nn 的排列是指由 11 到 nn 这 nn 个互不相同的整数组成的、顺序任意的数组。例如,[2,3,1,5,4][2,3,1,5,4] 是一个排列,但 [1,2,2][1,2,2] 不是一个排列(数字 22 在数组中出现了两次),[1,3,4][1,3,4] 也不是一个排列(此时 n=3n = 3,但数组中包含了 44)。

输入格式

Each test contains multiple test cases. The first line contains a single integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases. Their description follows.

The first line contains three integers nn, mm, and qq (1≤n,q≤2⋅1051\le n,q \le 2 \cdot 10^5, 2≤m≤2⋅1052\le m \le 2\cdot 10^5, n≡1(modm−1)n \equiv 1 \pmod {m - 1}) — the number of the leaves, the constant mm, and the number of queries.

The second line contains nn integers a1,a2,…,ana_{1},a_{2}, \ldots, a_{n} (1≤ai≤n1 \le a_{i} \le n) — the initial array.

For the following qq lines, each line contains two integers xx and yy (1≤x,y≤n1\le x,y\le n), representing a query changing axa_{x} to yy.

It is guaranteed that both the sum of nn and the sum of qq do not exceed 2⋅1052\cdot 10^5.

每个测试包含多个测试用例。第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)—— 测试用例的数量。随后是各测试用例的描述。

第一行包含三个整数 nn、mm 和 qq(1≤n,q≤2⋅1051\le n,q \le 2 \cdot 10^5,2≤m≤2⋅1052\le m \le 2\cdot 10^5,且满足 n≡1(modm−1)n \equiv 1 \pmod {m - 1})—— 分别表示叶子节点的数量、常数 mm 以及查询次数。

第二行包含 nn 个整数 a1,a2,…,ana_{1},a_{2}, \ldots, a_{n}(1≤ai≤n1 \le a_{i} \le n)—— 初始数组。

接下来的 qq 行中,每行包含两个整数 xx 和 yy(1≤x,y≤n1\le x,y\le n),表示一次将 axa_{x} 修改为 yy 的查询。

保证所有测试用例中 nn 的总和与 qq 的总和均不超过 2⋅1052\cdot 10^5。

输出格式

For each test case, output qq integers in one line, the ii-th of which is the minimum tree value after the ii-th change.

对于每个测试用例,在一行中输出 qq 个整数,其中第 ii 个整数表示经过第 ii 次修改后的最小树值。

输入输出样例

  • 输入#1

    3
    5 3 3
    3 3 4 4 5
    1 4
    2 4
    3 5
    5 2 4
    3 3 4 4 5
    1 4
    2 5
    3 5
    4 5
    7 3 4
    1 2 2 3 3 3 4
    1 4
    2 1
    5 5
    6 6

    输出#1

    6 6 6
    7 7 7 8
    6 6 6 7

说明/提示

In the first test case, after the first query, the current array aa is [4,3,4,4,5][4,3,4,4,5]. We can construct such a good tree:

The first number inside a vertex is its index (in this problem, the indices do not matter, but help to understand the figure). If a vertex is a leaf, the second number inside the vertex is the integer written on it.

We can tell that dep3=dep4=1,dep5=dep6=dep7=2\mathrm{dep}_{3}=\mathrm{dep}_{4}=1,\mathrm{dep}_{5}=\mathrm{dep}_{6}=\mathrm{dep}_{7}=2 and the value of the tree, which is the maximum value over all leaves, is 5+1=65+1=6. The value of leaves 55, 66, 77 is also equal to 66. It can be shown that this tree has the minimum value over all valid trees.

在第一个测试用例中,第一次查询后,当前数组 aa 为 [4,3,4,4,5][4,3,4,4,5]。我们可以构造如下一棵“好树”:

顶点内部的第一个数字是其索引(本题中索引并不重要,但有助于理解图示)。若某顶点为叶子节点,则其内部的第二个数字即为写在该叶子上的整数。

我们可以得出:dep3=dep4=1\mathrm{dep}_{3}=\mathrm{dep}_{4}=1,dep5=dep6=dep7=2\mathrm{dep}_{5}=\mathrm{dep}_{6}=\mathrm{dep}_{7}=2;而该树的值(即所有叶子节点对应值中的最大值)为 5+1=65+1=6。叶子节点 55、66、77 的值也均为 66。可以证明,该树在所有合法树中具有最小的值。

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

首页