CF1303F.Number of Components
省选/NOI-
通过率:0%
时间限制:4.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a matrix n×m, initially filled with zeroes. We define ai,j as the element in the i-th row and the j-th column of the matrix.
Two cells of the matrix are connected if they share a side, and the elements in these cells are equal. Two cells of the matrix belong to the same connected component if there exists a sequence s1, s2, ..., sk such that s1 is the first cell, sk is the second cell, and for every i∈[1,k−1], si and si+1 are connected.
You are given q queries of the form xi yi ci (i∈[1,q]). For every such query, you have to do the following:
- replace the element ax,y with c;
- count the number of connected components in the matrix.
There is one additional constraint: for every i∈[1,q−1], ci≤ci+1.
给你一个 n×m 的矩阵,初始时所有元素均为 0。我们用 ai,j 表示该矩阵第 i 行、第 j 列的元素。
若两个矩阵单元格具有公共边,且其中的元素值相等,则称这两个单元格是连通的。若存在一个序列 s1,s2,…,sk,使得 s1 为第一个单元格,sk 为第二个单元格,且对每个 i∈[1,k−1],si 与 si+1 均连通,则称这两个单元格属于同一个连通分量。
你将收到 q 个形如 xi yi ci(其中 i∈[1,q])的查询。对每个查询,你需要执行以下操作:
- 将元素 ax,y 替换为 c;
- 统计当前矩阵中连通分量的总数。
另有一条附加约束:对每个 i∈[1,q−1],均有 ci≤ci+1。
输入格式
The first line contains three integers n, m and q (1≤n,m≤300, 1≤q≤2⋅106) — the number of rows, the number of columns and the number of queries, respectively.
Then q lines follow, each representing a query. The i-th line contains three integers xi, yi and ci (1≤xi≤n, 1≤yi≤m, 1≤ci≤max(1000,⌈nm2⋅106⌉)). For every i∈[1,q−1], ci≤ci+1.
第一行包含三个整数 n、m 和 q(1≤n,m≤300,1≤q≤2⋅106),分别表示行数、列数和查询次数。
接下来是 q 行,每行表示一个查询。第 i 行包含三个整数 xi、yi 和 ci(1≤xi≤n,1≤yi≤m,1≤ci≤max(1000,⌈nm2⋅106⌉))。对每个 i∈[1,q−1],均有 ci≤ci+1。
输出格式
Print q integers, the i-th of them should be equal to the number of components in the matrix after the first i queries are performed.
输出 q 个整数,其中第 i 个整数应等于执行前 i 个查询后矩阵中连通块的数量。
输入输出样例
输入#1
3 2 10 2 1 1 1 2 1 2 2 1 1 1 2 3 1 2 1 2 2 2 2 2 2 1 2 3 2 4 2 1 5
输出#1
2 4 3 3 4 4 4 2 2 4
输入解题思路,AI测评打分。不知道怎么写?