結果

問題 No.2786 RMQ on Grid Path
ユーザー tentententen
提出日時 2024-06-17 09:35:58
言語 Java21
(openjdk 21)
結果
AC  
実行時間 2,143 ms / 6,000 ms
コード長 5,265 bytes
コンパイル時間 4,918 ms
コンパイル使用メモリ 84,888 KB
実行使用メモリ 186,552 KB
最終ジャッジ日時 2024-06-17 09:36:58
合計ジャッジ時間 52,442 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
50,932 KB
testcase_01 AC 85 ms
50,908 KB
testcase_02 AC 110 ms
53,636 KB
testcase_03 AC 136 ms
53,060 KB
testcase_04 AC 99 ms
51,980 KB
testcase_05 AC 116 ms
52,808 KB
testcase_06 AC 112 ms
52,976 KB
testcase_07 AC 122 ms
52,936 KB
testcase_08 AC 118 ms
53,012 KB
testcase_09 AC 119 ms
52,980 KB
testcase_10 AC 110 ms
52,060 KB
testcase_11 AC 119 ms
52,944 KB
testcase_12 AC 2,143 ms
174,808 KB
testcase_13 AC 1,977 ms
177,152 KB
testcase_14 AC 1,891 ms
175,300 KB
testcase_15 AC 1,952 ms
174,624 KB
testcase_16 AC 1,927 ms
175,292 KB
testcase_17 AC 1,987 ms
175,136 KB
testcase_18 AC 1,936 ms
174,524 KB
testcase_19 AC 1,951 ms
175,108 KB
testcase_20 AC 1,983 ms
175,300 KB
testcase_21 AC 1,977 ms
175,188 KB
testcase_22 AC 1,814 ms
175,064 KB
testcase_23 AC 1,816 ms
175,160 KB
testcase_24 AC 1,829 ms
186,552 KB
testcase_25 AC 1,766 ms
184,992 KB
testcase_26 AC 1,879 ms
186,392 KB
testcase_27 AC 962 ms
86,944 KB
testcase_28 AC 711 ms
74,744 KB
testcase_29 AC 1,819 ms
157,112 KB
testcase_30 AC 736 ms
75,296 KB
testcase_31 AC 307 ms
62,416 KB
testcase_32 AC 1,571 ms
167,408 KB
testcase_33 AC 513 ms
71,444 KB
testcase_34 AC 1,781 ms
182,276 KB
testcase_35 AC 1,789 ms
183,208 KB
testcase_36 AC 1,697 ms
183,496 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.function.*;
import java.util.stream.*;

public class Main {
    static List<List<Integer>> graph = new ArrayList<>();
    static int[] depths;
    static int[][] parents;
    static int[][] maxes;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int h = sc.nextInt();
        int w = sc.nextInt();
        int[] values = new int[h * w];
        PriorityQueue<Bridge> queue = new PriorityQueue<>();
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                values[i * w + j] = sc.nextInt();
                if (i > 0) {
                    queue.add(new Bridge((i - 1) * w + j, i * w + j, Math.max(values[(i - 1) * w + j], values[i * w + j])));
                }
                if (j > 0) {
                    queue.add(new Bridge(i * w + j - 1, i * w + j, Math.max(values[i * w + j - 1], values[i * w + j])));
                }
            }
        }
        for (int i = 0; i < h * w; i++) {
            graph.add(new ArrayList<>());
        }
        UnionFindTree uft = new UnionFindTree(h * w);
        while (queue.size() > 0) {
            Bridge x = queue.poll();
            if (!uft.same(x)) {
                uft.unite(x);
                graph.get(x.left).add(x.right);
                graph.get(x.right).add(x.left);
            }
        }
        depths = new int[h * w];
        parents = new int[15][h * w];
        setDepth(0, 0, 0);
        maxes = new int[15][h * w];
        for (int i = 0; i < h * w; i++) {
            maxes[0][i] = values[i];
        }
        for (int i = 1; i < 15; i++) {
            for (int j = 0; j < h * w; j++) {
                parents[i][j] = parents[i - 1][parents[i - 1][j]];
                maxes[i][j] = Math.max(maxes[i - 1][j], maxes[i - 1][parents[i - 1][j]]);
            }
        }
        int q = sc.nextInt();
        String result = IntStream.range(0, q)
            .mapToObj(i -> String.valueOf(getMax((sc.nextInt() - 1) * w + sc.nextInt() - 1, (sc.nextInt() - 1) * w + sc.nextInt() - 1)))
            .collect(Collectors.joining("\n"));
        System.out.println(result);
    }
    
    static int getMax(int a, int b) {
        int ans = 0;
        for (int i = 14; i >= 0; i--) {
            if (depths[a] - depths[b] >= (1 << i)) {
                ans = Math.max(ans, maxes[i][a]);
                a = parents[i][a];
            } else if (depths[b] - depths[a] >= (1 << i)) {
                ans = Math.max(ans, maxes[i][b]);
                b = parents[i][b];
            }
        }
        if (a == b) {
            return Math.max(ans, maxes[0][a]);
        }
        for (int i = 14; i>= 0; i--) {
            if (parents[i][a] != parents[i][b]) {
                ans = Math.max(ans, Math.max(maxes[i][a], maxes[i][b]));
                a = parents[i][a];
                b = parents[i][b];
            }
        }
        ans = Math.max(ans, Math.max(maxes[0][a], maxes[0][b]));
        return Math.max(ans, maxes[0][parents[0][a]]);
    }
    
    static void setDepth(int idx, int p, int d) {
        parents[0][idx] = p;
        depths[idx] = d;
        for (int x : graph.get(idx)) {
            if (x != p) {
                setDepth(x, idx, d + 1);
            }
        }
    }
    
    static class Bridge implements Comparable<Bridge> {
        int left;
        int right;
        int value;
        
        public Bridge(int left, int right, int value) {
            this.left = left;
            this.right = right;
            this.value = value;
        }
        
        public int compareTo(Bridge another) {
            return value - another.value;
        }
    }
    
    static class UnionFindTree {
        int[] parents;
        
        public UnionFindTree(int size) {
            parents = new int[size];
            for (int i = 0; i < size; i++) {
                parents[i] = i;
            }
        }
        
        public int find(int x) {
            return x == parents[x] ? x : (parents[x] = find(parents[x]));
        }
        
        public boolean same(int x, int y) {
            return find(x) == find(y);
        }
        
        public boolean same(Bridge b) {
            return same(b.left, b.right);
        }
        
        public void unite(Bridge b) {
            parents[find(b.left)] = find(b.right);
        }
    }
}
class Scanner {
    BufferedReader br;
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() {
        try {
            br = new BufferedReader(new InputStreamReader(System.in));
        } catch (Exception e) {
            
        }
    }
    
    public int nextInt() {
        return Integer.parseInt(next());
    }
    
    public long nextLong() {
        return Long.parseLong(next());
    }
    
    public double nextDouble() {
        return Double.parseDouble(next());
    }
    
    public String next() {
        try {
            while (!st.hasMoreTokens()) {
                st = new StringTokenizer(br.readLine());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return st.nextToken();
        }
    }
    
}


0