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
****************************************************************/


目录
相关文章
|
机器学习/深度学习 编解码 人工智能
Reading Notes: Human-Computer Interaction System: A Survey of Talking-Head Generation
由于人工智能的快速发展,虚拟人被广泛应用于各种行业,包括个人辅助、智能客户服务和在线教育。拟人化的数字人可以快速与人接触,并在人机交互中增强用户体验。因此,我们设计了人机交互系统框架,包括语音识别、文本到语音、对话系统和虚拟人生成。接下来,我们通过虚拟人深度生成框架对Talking-Head Generation视频生成模型进行了分类。同时,我们系统地回顾了过去五年来在有声头部视频生成方面的技术进步和趋势,强调了关键工作并总结了数据集。 对于有关于Talking-Head Generation的方法,这是一篇比较好的综述,我想着整理一下里面比较重要的部分,大概了解近几年对虚拟人工作的一些发展和
|
7月前
|
安全 测试技术 网络安全
什么是 Penetration test
什么是 Penetration test
28 0
|
人工智能
P8969 幻梦 | Dream with Dynamic
P8969 幻梦 | Dream with Dynamic
148 0
Data Structures and Algorithms (English) - 7-10 Saving James Bond - Easy Version(25 分)
Data Structures and Algorithms (English) - 7-10 Saving James Bond - Easy Version(25 分)
71 0
|
iOS开发 开发者 C++
Effective Objective-C 2.0 Tips 总结 Chapter 5,6,7
Effective Objective-C 2.0 Tips 总结 Chapter 5,6,7 Chapter 5 内存管理 Tips 29 理解引用计数 引用计数是 Objective-C 内存管理的基础,包括 ARC 也是建立在引用计数的基础之...
1260 0
How China's Developers Are Defining The Information Age (Infographic 5)
China’s Developers: the technologies of tomorrow that are defining the information age
1602 0
How China's Developers Are Defining The Information Age (Infographic 5)
How China's Developers Are Defining The Information Age (Infographic 4)
China’s Developers: the essentials of today that are defining the information age
1454 0
How China's Developers Are Defining The Information Age (Infographic 4)
How China's Developers Are Defining The Information Age (Infographic 3)
China’s Developers: the trending technologies that are defining the information age
1553 0
How China's Developers Are Defining The Information Age (Infographic 3)
How China's Developers Are Defining The Information Age (Infographic 2)
China’s Developers: the tools that are defining the information age
1571 0
How China's Developers Are Defining The Information Age (Infographic 2)
How China's Developers Are Defining The Information Age (Infographic 1)
China's Developers at a Glance: 9 key takeaways from China's Developer Survey Report 2017
1845 0
How China's Developers Are Defining The Information Age (Infographic 1)

热门文章

最新文章