Practical Skill Test——AT

简介: 题目描述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.

题目描述


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 |x−i|+|y−j| 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 Ri−Li is a multiple of D.

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


Constraints

1≤H,W≤300

1≤D≤H×W

1≤Ai,j≤H×W

Ai,j≠Ax,y((i,j)≠(x,y))

1≤Q≤105

1≤Li≤Ri≤H×W

(Ri−Li) is a multiple of D.


输入


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


输出


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

Output should be in the order the tests are conducted.


样例输入


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


样例输出


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.


刚开始疯狂T了两次,原来是这个题目卡cout


#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize (2)
#pragma G++ optimize (2)
#include <bits/stdc++.h>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define wuyt main
typedef long long ll;
#define HEAP(...) priority_queue<__VA_ARGS__ >
#define heap(...) priority_queue<__VA_ARGS__,vector<__VA_ARGS__ >,greater<__VA_ARGS__ > >
template<class T> inline T min(T &x,const T &y){return x>y?y:x;}
template<class T> inline T max(T &x,const T &y){return x<y?y:x;}
///#define getchar()(p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
///char buf[(1 << 21) + 1], *p1 = buf, *p2 = buf;
ll read(){ll c = getchar(),Nig = 1,x = 0;while(!isdigit(c) && c!='-')c = getchar();
if(c == '-')Nig = -1,c = getchar();
while(isdigit(c))x = ((x<<1) + (x<<3)) + (c^'0'),c = getchar();
return Nig*x;}
#define read read()
const ll inf = 1e15;
const int maxn = 2e5 + 7;
const int mod = 1e9 + 7;
#define start int wuyt()
#define end return 0
int cnt;
string s;
int num[maxn];
int x[maxn],y[maxn];
start{
    int h=read,w=read,d=read;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            int temp=read;
            x[temp]=i;
            y[temp]=j;
        }
    }
    for(int i=d+1;i<=h*w;i++)
        num[i]=num[i-d]+abs(x[i]-x[i-d])+abs(y[i]-y[i-d]);
    int q=read;
    for(int i=1;i<=q;i++){
        int l=read,r=read;
        printf("%d\n",num[r]-num[l]);
    }
    end;
}
/**************************************************************
    Language: C++
    Result: 正确
    Time:41 ms
    Memory:4368 kb
****************************************************************/


目录
相关文章
|
Oracle Java 关系型数据库
CentOS 7 - 安装Oracle JDK8
我们要在CentOS安装最新版本的JDK8,需要首先将JDK下载到服务器,然后通过操作系统自带的工具yum进行安装。 本文我们将介绍CentOS 7下JDK8的安装。
4318 0
|
机器学习/深度学习 并行计算 算法
ICASSP 2023论文模型开源|语音分离Mossformer
人类能在复杂的多人说话环境中轻易地分离干扰声音,选择性聆听感兴趣的主讲人说话。但这对机器却不容易,如何构建一个能够媲美人类听觉系统的自动化系统颇具挑战性。 本文将详细解读ICASSP2023本届会议收录的单通道语音分离模型Mossformer论文,以及如何基于开发者自有数据进行该模型的调优训练。
961 0
|
SQL XML Java
力扣MyBatis框架 三万多字干货,来了解一下?(一)
力扣MyBatis框架 三万多字干货,来了解一下?
196 0
|
XML JSON Java
Spring - Bean管理之注解(@Component、@Controller、@RestController、@Service、@Repository)
Spring - Bean管理之注解(@Component、@Controller、@RestController、@Service、@Repository)
576 0
Spring - Bean管理之注解(@Component、@Controller、@RestController、@Service、@Repository)
|
SQL 测试技术 数据库
什么是单元测试?为什么要做?
什么是UT? UT(Unit Test)即单元测试
535 0
什么是单元测试?为什么要做?
|
Java
JDK8默认垃圾回收器
JDK8默认垃圾回收器
1617 0
|
网络协议 C++ Windows
我对开源C++网络库简单应用总结
网上有篇文章《开源免费的C/C++网络库(c/c++ sockets library) 七剑下天山》,看了之后觉得每个库都不错,这里我具体下载这些库看一下,简单总结一下; 顺便添加一些我找到的网络库;     (1)ACE 庞大、复杂,适合大型项目。
1537 0