CF1303F.Number of Components

普及/提高-

通过率:0%

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,k1]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,q1]i \in [1, q - 1] , cici+1c_i \le c_{i + 1} .

输入格式

The first line contains three integers nn , mm and qq ( 1n,m3001 \le n, m \le 300 , 1q21061 \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 ( 1xin1 \le x_i \le n , 1yim1 \le y_i \le m , 1cimax(1000,2106nm)1 \le c_i \le \max(1000, \lceil \frac{2 \cdot 10^6}{nm} \rceil) ). For every i[1,q1]i \in [1, q - 1] , cici+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.

输入输出样例

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