Submission #2458097


Source Code Expand

Copy
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

int main() {
    int H, W, D;
    cin >> H >> W >> D;

    pair<int, int> place[H * W + 1];
    // 各数値の座標を記録

    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            int A;
            cin >> A;
            place[A] = {i, j};
        }
    }

    int mp[H * W + 1];
    // 始点からの消費魔力をメモ

    // 下ごしらえ開始
    for (int i = 1; i <= H * W; i++) {
        if (i <= D) {
            // いわゆる「始点」なので消費魔力は0
            mp[i] = 0;

        } else {
            // 座標を求める
            int x1 = place[i - D].first;
            int y1 = place[i - D].second;
            int x2 = place[i].first;
            int y2 = place[i].second;

            // 魔力を計算
            mp[i] = mp[i - D] + abs(x2 - x1) + abs(y2 - y1);
        }
    }

    int Q;
    cin >> Q;
    for (int i = 0; i < Q; i++) {
        // ここからクエリを捌いていく
        int L, R;
        cin >> L >> R;

        // 差分を取るだけで答えが出る
        cout << mp[R] - mp[L] << endl;
    }
    return 0;
}

Submission Info

Submission Time
Task D - Practical Skill Test
User Misteer
Language C++14 (GCC 5.4.1)
Score 400
Code Size 1271 Byte
Status
Exec Time 264 ms
Memory 2176 KB

Test Cases

Set Name Score / Max Score Test Cases
Sample 0 / 0 sample_01.txt, sample_02.txt, sample_03.txt
All 400 / 400 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, sample_01.txt, sample_02.txt, sample_03.txt
Case Name Status Exec Time Memory
01.txt 264 ms 1792 KB
02.txt 254 ms 1792 KB
03.txt 213 ms 640 KB
04.txt 254 ms 1408 KB
05.txt 260 ms 1536 KB
06.txt 247 ms 2176 KB
07.txt 256 ms 2176 KB
08.txt 1 ms 256 KB
09.txt 260 ms 1920 KB
sample_01.txt 1 ms 256 KB
sample_02.txt 1 ms 256 KB
sample_03.txt 1 ms 256 KB