CF1303F.Number of Components

省选/NOI-

通过率:0%

时间限制:4.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given a matrix n×mn \times m, initially filled with zeroes. We define ai,ja_{i, j} as the element in the ii-th row and the jj-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 s1s_1, s2s_2, ..., sks_k such that s1s_1 is the first cell, sks_k is the second cell, and for every i∈[1,k−1]i \in [1, k - 1], sis_i and si+1s_{i + 1} are connected.

You are given qq queries of the form xix_i yiy_i cic_i (i∈[1,q]i \in [1, q]). For every such query, you have to do the following:

  1. replace the element ax,ya_{x, y} with cc;
  2. count the number of connected components in the matrix.

There is one additional constraint: for every i∈[1,q−1]i \in [1, q - 1], ci≤ci+1c_i \le c_{i + 1}.

给你一个 n×mn \times m 的矩阵,初始时所有元素均为 00。我们用 ai,ja_{i, j} 表示该矩阵第 ii 行、第 jj 列的元素。

若两个矩阵单元格具有公共边,且其中的元素值相等,则称这两个单元格是连通的。若存在一个序列 s1,s2,…,sks_1, s_2, \dots, s_k,使得 s1s_1 为第一个单元格,sks_k 为第二个单元格,且对每个 i∈[1,k−1]i \in [1, k - 1],sis_i 与 si+1s_{i + 1} 均连通,则称这两个单元格属于同一个连通分量。

你将收到 qq 个形如 xi yi cix_i\ y_i\ c_i(其中 i∈[1,q]i \in [1, q])的查询。对每个查询,你需要执行以下操作:

  1. 将元素 ax,ya_{x, y} 替换为 cc;
  2. 统计当前矩阵中连通分量的总数。

另有一条附加约束:对每个 i∈[1,q−1]i \in [1, q - 1],均有 ci≤ci+1c_i \le c_{i + 1}。

输入格式

The first line contains three integers nn, mm and qq (1≤n,m≤3001 \le n, m \le 300, 1≤q≤2⋅1061 \le q \le 2 \cdot 10^6) — the number of rows, the number of columns and the number of queries, respectively.

Then qq lines follow, each representing a query. The ii-th line contains three integers xix_i, yiy_i and cic_i (1≤xi≤n1 \le x_i \le n, 1≤yi≤m1 \le y_i \le m, 1≤ci≤max⁡(1000,⌈2⋅106nm⌉)1 \le c_i \le \max(1000, \lceil \frac{2 \cdot 10^6}{nm} \rceil)). For every i∈[1,q−1]i \in [1, q - 1], ci≤ci+1c_i \le c_{i + 1}.

第一行包含三个整数 nn、mm 和 qq(1≤n,m≤3001 \le n, m \le 300,1≤q≤2⋅1061 \le q \le 2 \cdot 10^6),分别表示行数、列数和查询次数。

接下来是 qq 行,每行表示一个查询。第 ii 行包含三个整数 xix_i、yiy_i 和 cic_i(1≤xi≤n1 \le x_i \le n,1≤yi≤m1 \le y_i \le m,1≤ci≤max⁡(1000,⌈2⋅106nm⌉)1 \le c_i \le \max(1000, \lceil \frac{2 \cdot 10^6}{nm} \rceil))。对每个 i∈[1,q−1]i \in [1, q - 1],均有 ci≤ci+1c_i \le c_{i + 1}。

输出格式

Print qq integers, the ii-th of them should be equal to the number of components in the matrix after the first ii queries are performed.

输出 qq 个整数,其中第 ii 个整数应等于执行前 ii 个查询后矩阵中连通块的数量。

输入输出样例

  • 输入#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测评打分。不知道怎么写?

首页