結果

問題 No.875 Range Mindex Query
ユーザー bluemeganebluemegane
提出日時 2021-04-30 06:57:21
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 657 ms / 2,000 ms
コード長 2,259 bytes
コンパイル時間 1,546 ms
コンパイル使用メモリ 67,768 KB
実行使用メモリ 36,536 KB
最終ジャッジ日時 2023-09-25 07:32:45
合計ジャッジ時間 8,113 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 61 ms
25,648 KB
testcase_01 AC 71 ms
21,808 KB
testcase_02 AC 71 ms
19,576 KB
testcase_03 AC 62 ms
23,576 KB
testcase_04 AC 70 ms
21,704 KB
testcase_05 AC 69 ms
21,784 KB
testcase_06 AC 72 ms
23,752 KB
testcase_07 AC 72 ms
21,652 KB
testcase_08 AC 70 ms
19,784 KB
testcase_09 AC 71 ms
21,548 KB
testcase_10 AC 71 ms
21,640 KB
testcase_11 AC 434 ms
32,924 KB
testcase_12 AC 378 ms
32,348 KB
testcase_13 AC 316 ms
34,268 KB
testcase_14 AC 318 ms
33,688 KB
testcase_15 AC 409 ms
34,136 KB
testcase_16 AC 600 ms
36,336 KB
testcase_17 AC 657 ms
36,536 KB
testcase_18 AC 630 ms
34,440 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

using System.Linq;
using static System.Math;
using System;

public class rmq
{
    private readonly int INTMAX = int.MaxValue;
    private int[] dat;
    private static int size;

    public rmq(int n)
    {
        size = 1;
        while (size < n) size *= 2;
        dat = Enumerable.Repeat(INTMAX, 2 * size - 1).ToArray();
    }

    public void update(int i, int a)
    {
        i += size - 1;
        dat[i] = a;
        while (i > 0)
        {
            i = (i - 1) / 2;
            dat[i] = Min(dat[i * 2 + 1], dat[i * 2 + 2]);
        }
    }

    private int query(int a, int b, int k, int L, int r)
    {
        if (r <= a || b <= L) return INTMAX;
        if (a <= L && r <= b) return dat[k];
        else
        {
            int vL = query(a, b, k * 2 + 1, L, (L + r) / 2);
            int vr = query(a, b, k * 2 + 2, (L + r) / 2, r);
            return Min(vL, vr);
        }
    }

    public int query(int a, int b) => query(a, b, 0, 0, size);

}

public class Hello
{
    static void Main()
    {
        string[] line = Console.ReadLine().Trim().Split(' ');
        var n = int.Parse(line[0]);
        var q = int.Parse(line[1]);
        getAns(n, q);

    }
    static void getAns(int n, int q)
    {
        var rmq = new rmq(n + 10);
        string[] line = Console.ReadLine().Trim().Split(' ');
        var a = Array.ConvertAll(line, int.Parse);
        var reva = new int[n + 1];
        for (int i = 0; i < n; i++)
        {
            rmq.update(i, a[i]);
            reva[a[i]] = i;
        }
        for (int i = 0; i < q; i++)
        {
            line = Console.ReadLine().Trim().Split(' ');
            var L = int.Parse(line[1]) - 1;
            var r = int.Parse(line[2]) - 1;
            if (line[0] == "1")
            {
                var wL = a[L];
                var wr = a[r];
                a[L] = wr;
                a[r] = wL;
                rmq.update(L, wr);
                rmq.update(r, wL);
                var xx = reva[a[L]];
                reva[a[L]] = reva[a[r]];
                reva[a[r]] = xx;
            }
            else
            {
                var w2 = rmq.query(L, r + 1);
                var p = reva[w2];
                Console.WriteLine(p + 1);
            }
        }
    }
}
0