{"problem":{"name":"E. Vladik and Entertaining Flags","description":{"content":"In his spare time Vladik estimates beauty of the flags. Every flag could be represented as the matrix _n_ × _m_ which consists of positive integers. Let's define the beauty of the flag as number of ","description_type":"Markdown"},"platform":"Codeforces","limit":{"time_limit":2000,"memory_limit":262144},"difficulty":"None","is_remote":true,"is_sync":true,"sync_url":null,"sign":"CF811E"},"statements":[{"statement_type":"Markdown","content":"In his spare time Vladik estimates beauty of the flags.\n\nEvery flag could be represented as the matrix _n_ × _m_ which consists of positive integers.\n\nLet's define the beauty of the flag as number of components in its matrix. We call component a set of cells with same numbers and between any pair of cells from that set there exists a path through adjacent cells from same component. Here is the example of the partitioning some flag matrix into components:\n\n<center>![image](https://espresso.codeforces.com/d28a7abcca2a160f6f91c56a581268f621c83dc1.png)</center>But this time he decided to change something in the process. Now he wants to estimate not the entire flag, but some segment. Segment of flag can be described as a submatrix of the flag matrix with opposite corners at (1, _l_) and (_n_, _r_), where conditions 1 ≤ _l_ ≤ _r_ ≤ _m_ are satisfied.\n\nHelp Vladik to calculate the beauty for some segments of the given flag.\n\n## Input\n\nFirst line contains three space-separated integers _n_, _m_, _q_ (1 ≤ _n_ ≤ 10, 1 ≤ _m_, _q_ ≤ 105) — dimensions of flag matrix and number of segments respectively.\n\nEach of next _n_ lines contains _m_ space-separated integers — description of flag matrix. All elements of flag matrix is positive integers not exceeding 106.\n\nEach of next _q_ lines contains two space-separated integers _l_, _r_ (1 ≤ _l_ ≤ _r_ ≤ _m_) — borders of segment which beauty Vladik wants to know.\n\n## Output\n\nFor each segment print the result on the corresponding line.\n\n[samples]\n\n## Note\n\nPartitioning on components for every segment from first test case:\n\n![image](https://espresso.codeforces.com/ebd6237eb1b036862262f7ae3e405399232a9f73.png)","is_translate":false,"language":"English"},{"statement_type":"Markdown","content":"在空闲时间，Vladik 会评估旗帜的美丽度。\n\n每面旗帜可以用一个 #cf_span[n × m] 的矩阵表示，其中包含正整数。\n\n我们定义旗帜的美丽度为其矩阵中连通分量的数量。连通分量是指由相同数字的单元格组成的集合，且该集合中任意两个单元格之间都存在一条通过相邻同值单元格构成的路径。下图展示了一个将旗帜矩阵划分为连通分量的例子：\n\n但这一次，他决定在过程中做一些改变。现在他想评估的不是整个旗帜，而是一些子段。旗帜的子段可以描述为一个子矩阵，其左上角为 #cf_span[(1, l)]，右下角为 #cf_span[(n, r)]，满足条件 #cf_span[1 ≤ l ≤ r ≤ m]。\n\n请帮助 Vladik 计算给定旗帜某些子段的美丽度。\n\n第一行包含三个用空格分隔的整数 #cf_span[n], #cf_span[m], #cf_span[q]（#cf_span[1 ≤ n ≤ 10], #cf_span[1 ≤ m, q ≤ 10^5]）——分别表示旗帜矩阵的行数、列数和查询的子段数量。\n\n接下来的 #cf_span[n] 行，每行包含 #cf_span[m] 个用空格分隔的整数——表示旗帜矩阵的描述。矩阵中所有元素均为不超过 #cf_span[10^6] 的正整数。\n\n接下来的 #cf_span[q] 行，每行包含两个用空格分隔的整数 #cf_span[l], #cf_span[r]（#cf_span[1 ≤ l ≤ r ≤ m]）——表示 Vladik 想要知道美丽度的子段边界。\n\n对于每个子段，请在对应行输出结果。\n\n第一个测试用例中每个子段的连通分量划分如下：\n\n## Input\n\n第一行包含三个用空格分隔的整数 #cf_span[n], #cf_span[m], #cf_span[q]（#cf_span[1 ≤ n ≤ 10], #cf_span[1 ≤ m, q ≤ 10^5]）——分别表示旗帜矩阵的行数、列数和查询的子段数量。接下来的 #cf_span[n] 行，每行包含 #cf_span[m] 个用空格分隔的整数——表示旗帜矩阵的描述。矩阵中所有元素均为不超过 #cf_span[10^6] 的正整数。接下来的 #cf_span[q] 行，每行包含两个用空格分隔的整数 #cf_span[l], #cf_span[r]（#cf_span[1 ≤ l ≤ r ≤ m]）——表示 Vladik 想要知道美丽度的子段边界。\n\n## Output\n\n对于每个子段，请在对应行输出结果。\n\n[samples]\n\n## Note\n\n第一个测试用例中每个子段的连通分量划分如下：","is_translate":true,"language":"Chinese"},{"statement_type":"Markdown","content":"**Definitions:**\n\n- Let $ A \\in \\mathbb{Z}^{n \\times m} $ be a matrix of positive integers, with $ 1 \\leq n \\leq 10 $, $ 1 \\leq m, q \\leq 10^5 $.\n- A **component** is a maximal connected set of cells (under 4-directional adjacency: up, down, left, right) such that all cells in the set have the same value.\n- For a segment $ [l, r] $ with $ 1 \\leq l \\leq r \\leq m $, define the **submatrix** $ A_{[l:r]} \\in \\mathbb{Z}^{n \\times (r-l+1)} $ as the submatrix consisting of columns $ l $ through $ r $.\n\n**Given:**\n\n- Matrix $ A $ of size $ n \\times m $.\n- $ q $ queries, each specifying a segment $ [l, r] $.\n\n**Objective:**\n\nFor each query $ [l, r] $, compute the number of connected components in the submatrix $ A_{[l:r]} $, where connectivity is defined via 4-directional adjacency and cells must have equal values to be in the same component.\n\n**Formal Output:**\n\nFor each query $ (l, r) $, output $ C(A_{[l:r]}) $, where $ C(B) $ denotes the number of connected components in matrix $ B $ under the above definition.","is_translate":false,"language":"Formal"}],"meta":{"iden":"CF811E","tags":["data structures","dsu","graphs"],"sample_group":[["4 5 4\n1 1 1 1 1\n1 2 2 3 3\n1 1 1 2 5\n4 4 5 5 5\n1 5\n2 5\n1 2\n4 5","6\n7\n3\n4"]],"created_at":"2026-03-03 11:00:39"}}