F.A.Q
Hand In Hand
Online Acmers
Problem Archive
Realtime Judge Status
Authors Ranklist
 
     C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
    DIY | Web-DIY beta
Author ID 
Password 
 Register new ID

Control in a Matrix

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/262144 K (Java/Others)
Total Submission(s): 160    Accepted Submission(s): 20


Problem Description
You are given an $N \times M$ matrix $A[1..N][1..M]$. We say that $(x_1,y_1)$ controls $(x_2,y_2)$ if and only if $A[x_1][y_1] > A[x_2][y_2] + |x_1-x_2| + |y_1-y_2|$. Now you need to calculate the number of pairs $((x_1,y_1),(x_2,y_2))$ that $(x_1,y_1)$ controls $(x_2,y_2)$.
 

Input
There are multiple test cases.

For each test case, the first line contains two integers $N$ and $M$, denoting the size of the matrix.

Then there are $N$ lines denoting the matrix, where each line contains $M$ integers.

$1\leq N,M \leq 10^3, ~ 1\leq A[i][j] \leq N+M$. And the sum of $N \times M$ of all test cases is not larger than $2 \cdot 10^6$.
 

Output
For each test case, print an integer in one line, denoting your answer.
 

Sample Input
3 3 1 1 1 6 6 6 1 1 1 3 3 1 2 3 4 5 6 6 6 6
 

Sample Output
18 14
 

Source
 

Statistic | Submit | Discuss | Note
Hangzhou Dianzi University Online Judge 3.0
Copyright © 2005-2025 HDU ACM Team. All Rights Reserved.
Designer & Developer : Wang Rongtao LinLe GaoJie GanLu
Total 0.000000(s) query 1, Server time : 2025-04-01 07:47:45, Gzip enabled