Problem Statement

We have a grid with H rows and W columns. The square at the i-th row and the j-th column will be called Square (i,j).

The integers from 1 through H×W are written throughout the grid, and the integer written in Square (i,j) is Ai,j.

You, a magical girl, can teleport a piece placed on Square (i,j) to Square (x,y) by consuming |xi|+|yj| magic points.

You now have to take Q practical tests of your ability as a magical girl.

The i-th test will be conducted as follows:

  • Initially, a piece is placed on the square where the integer Li is written.

  • Let x be the integer written in the square occupied by the piece. Repeatedly move the piece to the square where the integer x+D is written, as long as x is not Ri. The test ends when x=Ri.

  • Here, it is guaranteed that RiLi is a multiple of D.

For each test, find the sum of magic points consumed during that test.

Constraints

  • 1≤H,W≤300
  • 1≤DH×W
  • 1≤Ai,jH×W
  • Ai,jAx,y((i,j)≠(x,y))
  • 1≤Q≤105
  • 1≤LiRiH×W
  • (RiLi) is a multiple of D.

Input

Input is given from Standard Input in the following format:

H W D
A1,1 A1,2 A1,W
:
AH,1 AH,2 AH,W
Q
L1 R1
:
LQ RQ

Output

For each test, print the sum of magic points consumed during that test.

Output should be in the order the tests are conducted.

Sample Input 1

3 3 2
1 4 3
2 5 7
8 9 6
1
4 8

Sample Output 1

5
  • 4 is written in Square (1,2).

  • 6 is written in Square (3,3).

  • 8 is written in Square (3,1).

Thus, the sum of magic points consumed during the first test is (|3−1|+|3−2|)+(|3−3|+|1−3|)=5.

Sample Input 2

4 2 3
3 7
1 4
5 2
6 8
2
2 2
2 2

Sample Output 2

0
0

Note that there may be a test where the piece is not moved at all, and there may be multiple identical tests.

Sample Input 3

5 5 4
13 25 7 15 17
16 22 20 2 9
14 11 12 1 19
10 6 23 8 18
3 21 5 24 4
3
13 13
2 10
13 13

Sample Output 3

0
5
0

Q最高100000,如果每次都要计算,会超时,先记录前缀和,直接用前缀和相减就好了。
代码:
#include <bits/stdc++.h>///int * int = int
using namespace std;
int h,w,d,s[][],q,a,b;
int x[],y[];
int dp[];
int main()
{
scanf("%d%d%d",&h,&w,&d);
for(int i = ;i < h;i ++)
{
for(int j = ;j < w;j ++)
{
scanf("%d",&s[i][j]);
x[s[i][j]] = i;
y[s[i][j]] = j;
}
}
for(int i = + d;i <= h * w;i ++)
{
dp[i] = dp[i - d] + abs(x[i] - x[i - d]) + abs(y[i] - y[i - d]);
}
scanf("%d",&q);
for(int i = ;i < q;i ++)
{
scanf("%d%d",&a,&b);
printf("%lld\n",abs(dp[a] - dp[b]));
}
}

AtCoder Beginner Contest 089 D - Practical Skill Test的更多相关文章

  1. AtCoder Beginner Contest 089完整题解

    A - Grouping 2 Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement There a ...

  2. AtCoder Beginner Contest 100 2018/06/16

    A - Happy Birthday! Time limit : 2sec / Memory limit : 1000MB Score: 100 points Problem Statement E8 ...

  3. AtCoder Beginner Contest 052

    没看到Beginner,然后就做啊做,发现A,B太简单了...然后想想做完算了..没想到C卡了一下,然后还是做出来了.D的话瞎想了一下,然后感觉也没问题.假装all kill.2333 AtCoder ...

  4. AtCoder Beginner Contest 053 ABCD题

    A - ABC/ARC Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Smeke has ...

  5. AtCoder Beginner Contest 136

    AtCoder Beginner Contest 136 题目链接 A - +-x 直接取\(max\)即可. Code #include <bits/stdc++.h> using na ...

  6. AtCoder Beginner Contest 137 F

    AtCoder Beginner Contest 137 F 数论鬼题(虽然不算特别数论) 希望你在浏览这篇题解前已经知道了费马小定理 利用用费马小定理构造函数\(g(x)=(x-i)^{P-1}\) ...

  7. AtCoder Beginner Contest 076

    A - Rating Goal Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Takaha ...

  8. AtCoder Beginner Contest 079 D - Wall【Warshall Floyd algorithm】

    AtCoder Beginner Contest 079 D - Wall Warshall Floyd 最短路....先枚举 k #include<iostream> #include& ...

  9. AtCoder Beginner Contest 064 D - Insertion

    AtCoder Beginner Contest 064 D - Insertion Problem Statement You are given a string S of length N co ...

随机推荐

  1. 九、设置RF自定义的日志输出路径

    在Arguments输入-d E:\\robot,每次运行完都会发送该目录日志

  2. C:\WINDOWS\system32\drivers\etc\hosts文件的作用

    在网络上访问网站,要首先通过DNS服务器把网络域名(www.XXXX.com)解析成XXX.XXX.XXX.XXX的IP地址后,我们的计算机才能访问.要是对于每个域名请求我们都要等待域名服务器解析后返 ...

  3. Vue创建全局组件

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. html不点击提交,自动post

    <!DOCTYPE html> <html> <form id="form1" name="form1" method=" ...

  5. Window7系统安装和使用MySql

    win7系统MySql安装和使用教程 首先下载mysql安装包 点击下载mysql v5.7.1 解压 下载完毕后解压在D盘 路径为D:\mysql-5.7.13-winx64,然后进入这个目录,新建 ...

  6. js五种不同的遍历 (filter, map,foreach,every, some,)

    var arr=[1,2,"a",2,4,1,4,"a",5,6,7,8,"aa","bb","c" ...

  7. spring -boot定时任务 quartz 基于 JobDetailFactoryBean实现

    这个有点小问题 尚未解决  后期优化 基于 JobDetailFactoryBean实现 依赖包 <dependencies> <dependency> <groupId ...

  8. /etc/syscofig/network 修改主机名

    [root@mysql ~]# cat /etc/sysconfig/network NETWORKING=yes HOSTNAME=mysql

  9. 题解 CF292A 【SMSC】

    蒟蒻本想发一篇黑题题解,没想到黑题竟然掉紫了QwQ. 趁着题解还只有几篇,赶紧来发一篇嘿嘿嘿. 这道题其实很简单,题解其实都不需要 AC代码: #include<bits/stdc++.h> ...

  10. 题解 AT2684 【K-City】

    此题这么水,竟然发题解的这么少. 本蒟蒻也来发一篇QwQ. 题目中所谓“四条街包围一个街区”其实就是两条街之间夹一个街区而已: n条街有几条两两相邻呢?答案是n-1条: m条街有几条两两相邻呢?答案是 ...