跳到主要内容
图灵 OJTURING / ONLINE JUDGE

#F0751. Vladik and Entertaining Flags

    ID: 757 传统题 2000ms 256MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>数据结构并查集图论挑战难度共享题库Codeforces英文题面题目来源题面语言

Vladik and Entertaining Flags

题目描述

E. Vladik and Entertaining Flags
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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:

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≤lrm 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≤105)− 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 106.

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

Output

For each segment print the result on the corresponding line.

Example
Input
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
6
7
3
4
Note

Partitioning on components for every segment from first test case:


题目来源:fps-www.educg.net-codeforce-1-2833.xml.zip;FPS 共享题包,题包内第 170 题。保留原作者与原赛事署名。