結果

問題 No.875 Range Mindex Query
ユーザー iwkjoseciwkjosec
提出日時 2019-10-05 17:15:23
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 300 ms / 2,000 ms
コード長 2,945 bytes
コンパイル時間 1,022 ms
コンパイル使用メモリ 109,696 KB
実行使用メモリ 33,792 KB
最終ジャッジ日時 2024-04-16 01:15:42
合計ジャッジ時間 4,734 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
19,072 KB
testcase_01 AC 38 ms
19,712 KB
testcase_02 AC 39 ms
19,968 KB
testcase_03 AC 33 ms
19,072 KB
testcase_04 AC 38 ms
19,584 KB
testcase_05 AC 38 ms
19,584 KB
testcase_06 AC 39 ms
19,584 KB
testcase_07 AC 39 ms
19,968 KB
testcase_08 AC 38 ms
19,712 KB
testcase_09 AC 37 ms
19,584 KB
testcase_10 AC 38 ms
19,968 KB
testcase_11 AC 295 ms
32,128 KB
testcase_12 AC 252 ms
29,056 KB
testcase_13 AC 229 ms
33,280 KB
testcase_14 AC 221 ms
32,768 KB
testcase_15 AC 290 ms
33,536 KB
testcase_16 AC 278 ms
33,536 KB
testcase_17 AC 300 ms
33,792 KB
testcase_18 AC 287 ms
33,664 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Collections.Generic;
using System.Linq;
using static System.Console;
using static System.Math;
using Pair = System.Collections.Generic.KeyValuePair<int, int>;

class Program
{
    static void Main()
    {
        SetOut(new System.IO.StreamWriter(OpenStandardOutput()) { AutoFlush = false });
        var NQ = ReadLine().Split().Select(int.Parse).ToArray();
        var N = NQ[0];
        var Q = NQ[1];
        var a = ReadLine().Split().Select(int.Parse).Select((x, i) => new Pair(i + 1, x)).ToArray();
        var s = new SegmentTree<Pair>(a, Mindex, new Pair(0, int.MaxValue));
        for (int i = 0; i < Q; i++)
        {
            var line = ReadLine().Split().Select(int.Parse).ToArray();
            var q = line[0];
            var l = line[1] - 1;
            var r = line[2] + (q == 1 ? -1 : 0);
            if (q == 1)
            {
                var k = s[l].Value;
                s[l] = new Pair(l + 1, s[r].Value);
                s[r] = new Pair(r + 1, k);
            }
            else
            {
                WriteLine(s.Query(l, r).Key);
            }
        }
        Out.Flush();
    }

    static Pair Mindex(Pair l, Pair r) => l.Value < r.Value ? l : r;
}

public class SegmentTree<T>
{
    T[] buffer;//0-index
    int N;
    public int Length => N;
    T TI;
    Func<T, T, T> F;//デリゲート遅そう 

    public SegmentTree(int length, Func<T, T, T> f, T ti)//モノイドが直接渡せれば C#9.0ではいるか
    {
        N = length;
        buffer = new T[N * 2 - 1];
        for (int i = 0; i < buffer.Length; i++) buffer[i] = ti; //!
        F = f;
        TI = ti;
    }

    public SegmentTree(T[] init, Func<T, T, T> f, T ti)
    {
        N = init.Length;
        buffer = new T[N * 2 - 1];
        Array.Copy(init, 0, buffer, N - 1, init.Length);
        for (int i = N - 2; i >= 0; i--)
        {
            buffer[i] = f(buffer[i * 2 + 1], buffer[i * 2 + 2]);
        }
        F = f;
        TI = ti;
    }

    public void Update(int k, T v)//k [0, N)
    {
        k += N - 1;
        buffer[k] = v;
        while (k > 0)
        {
            k = (k - 1) >> 1;
            buffer[k] = F(buffer[k * 2 + 1], buffer[k * 2 + 2]);
        }
    }

    public T this[int k]
    {
        set//setの式形式はC#7.0以降
        {
            Update(k, value);
        }
        get
        {
            return buffer[k + N - 1];
        }
    }

    public T Query(int l, int r)//[l, r) 0 <= l, r <= N // 0 <= r - l のとき空区間
    {
        var lv = TI;
        var rv = TI;
        l += N - 1;
        r += N - 1;
        while (l < r)
        {
            if ((l & 1) == 0)
            {
                lv = F(lv, buffer[l]);
            }
            if ((r & 1) == 0)
            {
                rv = F(buffer[r - 1], rv);
            }
            l >>= 1;
            r = (r - 1) >> 1;
        }
        return F(lv, rv);
    }
}
0