Codeforces Round 416 (Div. 2)
E
Vladik and Entertaining Flags
Codeforces
2600
原题
← D→
翻译中…

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 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:

翻译中…

image

翻译中…

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.

翻译中…

Help Vladik to calculate the beauty for some segments of the given flag.

翻译中…

Input

翻译中…

First line contains three space-separated integers $*n*$, $*m*$, $*q*$ ($1 ≤ *n* ≤ 10$, $1 ≤ *m*, *q* ≤ 10^{5}$) — dimensions of flag matrix and number of segments respectively.

翻译中…

Each of next $*n*$ lines contains $*m*$ space-separated integers — description of flag matrix. All elements of flag matrix is positive integers not exceeding $10^{6}$.

翻译中…

Each of next $*q*$ lines contains two space-separated integers $*l*$, $*r*$ ($1 ≤ *l* ≤ *r* ≤ *m*$) — borders of segment which beauty Vladik wants to know.

翻译中…

Output

翻译中…

For each segment print the result on the corresponding line.

翻译中…

Note

翻译中…

Partitioning on components for every segment from first test case:

翻译中…

image

翻译中…

样例

Input #1
4 5 4
1 1 1 1 1
1 2 2 3 3
1 1 1 2 5
4 4 5 5 5
1 5
2 5
1 2
4 5
Output #1
6
7
3
4
InputCopy OutputCopy
题目信息
题号
811E
时间限制
2 s
内存限制
256 MB
标签
数据结构
并查集
图论

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。