結果

問題 No.875 Range Mindex Query
ユーザー iwkjoseciwkjosec
提出日時 2019-12-18 15:11:19
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 158 ms / 2,000 ms
コード長 3,732 bytes
コンパイル時間 2,416 ms
コンパイル使用メモリ 106,972 KB
実行使用メモリ 26,228 KB
最終ジャッジ日時 2023-09-20 17:03:03
合計ジャッジ時間 6,459 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 50 ms
20,460 KB
testcase_01 AC 50 ms
22,428 KB
testcase_02 AC 50 ms
20,304 KB
testcase_03 AC 50 ms
20,440 KB
testcase_04 AC 50 ms
22,376 KB
testcase_05 AC 50 ms
20,388 KB
testcase_06 AC 50 ms
20,380 KB
testcase_07 AC 51 ms
22,412 KB
testcase_08 AC 51 ms
22,520 KB
testcase_09 AC 50 ms
20,376 KB
testcase_10 AC 50 ms
20,560 KB
testcase_11 AC 158 ms
21,716 KB
testcase_12 AC 139 ms
23,184 KB
testcase_13 AC 124 ms
19,848 KB
testcase_14 AC 123 ms
21,844 KB
testcase_15 AC 152 ms
23,996 KB
testcase_16 AC 143 ms
23,948 KB
testcase_17 AC 153 ms
26,228 KB
testcase_18 AC 148 ms
24,168 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 N = FastIO.Int();
        var Q = FastIO.Int();
        var a = new Pair[N];
        for (int i = 0; i < a.Length; i++)
        {
            a[i] = new Pair(i + 1, FastIO.Int());
        }
        var s = new SegmentTree<Pair>(a, Mindex, new Pair(0, int.MaxValue));
        for (int i = 0; i < Q; i++)
        {
            var q = FastIO.Int();
            var l = FastIO.Int() - 1;
            var r = FastIO.Int() + (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;
    Func<T, T, T> F;
    T TI;
    int N;
    public int Length => N;

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

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

    public void Update(int i, T v)
    {
        i += N;
        buffer[i] = v;
        while (i > 1)
        {
            buffer[i >> 1] = F(buffer[i], buffer[i ^ 1]);
            i >>= 1;
        }
    }

    public T Query(int l, int r)
    {
        l += N;
        r += N;
        var lv = TI;
        var rv = TI;
        while (l < r)
        {
            if ((l & 1) == 1) lv = F(lv, buffer[l++]);
            if ((r & 1) == 1) rv = F(buffer[--r], rv);
            l >>= 1;
            r >>= 1;
        }
        return F(lv, rv);
    }

    public T this[int i]
    {
        set { Update(i, value); }
        get { return buffer[i + N]; }
    }
}

public static class FastIO
{
    static System.IO.Stream str = System.Console.OpenStandardInput();
    const int size = 1024;
    static byte[] buffer = new byte[size];
    static int ptr;
    static int len;

    static byte Read()
    {
        if (ptr == len)
        {
            len = str.Read(buffer, 0, size);
            if (len == 0) return 0;
            ptr = 0;
        }
        return buffer[ptr++];
    }

    public static int Int()
    {
        var c = Read();
        while (c < 0x21)
        {
            c = Read();
        }
        var n = false;
        if (c == '-')
        {
            n = true;
            c = Read();
        }
        var ret = 0;
        while (c > 0x20)
        {
            ret = ret * 10 + c - '0';
            c = Read();
        }
        return n ? -ret : ret;
    }

    public static long Long()
    {
        var c = Read();
        while (c < 0x21)
        {
            c = Read();
        }
        var n = false;
        if (c == '-')
        {
            n = true;
            c = Read();
        }
        var ret = 0L;
        while (c > 0x20)
        {
            ret = ret * 10 + c - '0';
            c = Read();
        }
        return n ? -ret : ret;
    }
}
0