結果

問題 No.2786 RMQ on Grid Path
ユーザー kakel-san
提出日時 2024-06-24 22:14:08
言語 C#
(.NET 8.0.404)
結果
AC  
実行時間 1,096 ms / 6,000 ms
コード長 3,313 bytes
コンパイル時間 13,868 ms
コンパイル使用メモリ 168,096 KB
実行使用メモリ 265,624 KB
最終ジャッジ日時 2024-06-24 22:14:58
合計ジャッジ時間 43,100 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 35
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (98 ms)。
MSBuild のバージョン 17.9.6+a4ecab324 (.NET)
  main -> /home/judge/data/code/bin/Release/net8.0/main.dll
  main -> /home/judge/data/code/bin/Release/net8.0/publish/

ソースコード

diff #

using System;
using static System.Console;
using System.Linq;
using System.Collections.Generic;

class Program
{
    static int NN => int.Parse(ReadLine());
    static int[] NList => ReadLine().Split().Select(int.Parse).ToArray();
    static int[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray();
    public static void Main()
    {
        Solve();
    }
    static void Solve()
    {
        var c = NList;
        var (h, w) = (c[0], c[1]);
        var map = NArr(h);
        var q = NN;
        var query = NArr(q);
        var cells = new (int x, int y, int a)[h * w];
        for (var i = 0; i < h; ++i) for (var j = 0; j < w; ++j) cells[i * w + j] = (i, j, map[i][j]);
        Array.Sort(cells, (l, r) => l.a.CompareTo(r.a));
        var sets = new HashSet<int>[h * w];
        for (var i = 0; i < sets.Length; ++i) sets[i] = new HashSet<int>();
        for (var i = 0; i < q; ++i)
        {
            sets[query[i][0] * w + query[i][1] - w - 1].Add(i);
            sets[query[i][2] * w + query[i][3] - w - 1].Add(i);
        }
        var mx = new int[] { -1, 1, 0, 0 };
        var my = new int[] { 0, 0, -1, 1 };
        var uf = new UnionFindTree(h * w);
        var ans = new int[q];
        foreach (var ci in cells)
        {
            for (var j = 0; j < 4; ++j)
            {
                var nx = ci.x + mx[j];
                var ny = ci.y + my[j];
                if (nx < 0 || nx >= h || ny < 0 || ny >= w) continue;
                if (map[ci.x][ci.y] < map[nx][ny]) continue;
                if (!uf.IsSameTree(ci.x * w + ci.y, nx * w + ny))
                {
                    var r1 = uf.GetRoot(ci.x * w + ci.y);
                    var r2 = uf.GetRoot(nx * w + ny);
                    if (sets[r1].Count > sets[r2].Count) (r1, r2) = (r2, r1);
                    foreach (var s in sets[r1])
                    {
                        if (sets[r2].Contains(s))
                        {
                            ans[s] = ci.a;
                            sets[r2].Remove(s);
                        }
                        else sets[r2].Add(s);
                    }
                    uf.Unite(ci.x * w + ci.y, nx * w + ny);
                    sets[uf.GetRoot(r2)] = sets[r2];
                }
            }
        }
        WriteLine(string.Join("\n", ans));
    }
    class UnionFindTree
    {
        int[] roots;
        public UnionFindTree(int size)
        {
            roots = new int[size];
            for (var i = 0; i < size; ++i) roots[i] = -1;
        }
        public int GetRoot(int a)
        {
            if (roots[a] < 0) return a;
            return roots[a] = GetRoot(roots[a]);
        }
        public bool IsSameTree(int a, int b)
        {
            return GetRoot(a) == GetRoot(b);
        }
        public bool Unite(int a, int b)
        {
            var x = GetRoot(a);
            var y = GetRoot(b);
            if (x == y) return false;
            if (-roots[x] < -roots[y]) { var tmp = x; x = y; y = tmp; }
            roots[x] += roots[y];
            roots[y] = x;
            return true;
        }
        public int GetSize(int a)
        {
            return -roots[GetRoot(a)];
        }
    }
}
0