using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Numerics; using System.Text; namespace ProgrammingContestTemplateByDotNetCore { #region "Input and Output Classes" class Scanner : IDisposable { private readonly Queue _buffer; private readonly char[] _sep; private readonly TextReader _reader; public Scanner(TextReader reader = null, char[] sep = null) { _buffer = new Queue(); _sep = sep ?? new[] { ' ' }; _reader = reader ?? Console.In; } public Scanner(string path, char[] sep = null) : this(new StreamReader(path), sep) { } private void CheckBuffer() { if (_buffer.Any() || _reader.Peek() == -1) return; string str = string.Empty; for (; string.IsNullOrWhiteSpace(str); str = _reader.ReadLine()) { } var values = str.Split(_sep).Where(s => !string.IsNullOrWhiteSpace(s)); foreach (var item in values) { _buffer.Enqueue(item); } } public string Next() { CheckBuffer(); return _buffer.Dequeue(); } public string[] GetStringArray(int count) => Enumerable.Range(0, count) .Select(e => Next()) .ToArray(); public int NextInt() => int.Parse(Next()); public int[] GetIntArray(int count) => Enumerable.Range(0, count) .Select(e => NextInt()) .ToArray(); public long NextLong() => long.Parse(Next()); public long[] GetLongArray(int count) => Enumerable.Range(0, count) .Select(e => NextLong()) .ToArray(); public double NextDouble() => double.Parse(Next()); public double[] GetDoubleArray(int count) => Enumerable.Range(0, count) .Select(e => NextDouble()) .ToArray(); public decimal NextDecimal() => decimal.Parse(Next()); public decimal[] GetDecimalArray(int count) => Enumerable.Range(0, count) .Select(e => NextDecimal()) .ToArray(); public BigInteger NextBigInt() => BigInteger.Parse(Next()); public BigInteger[] GetBigIntArray(int count) => Enumerable.Range(0, count) .Select(e => NextBigInt()) .ToArray(); public bool IsEnd { get { CheckBuffer(); return !_buffer.Any(); } } public void Dispose() { if (!_reader.Equals(Console.In)) _reader.Dispose(); } } class Writer : IDisposable { private readonly TextWriter _writer; private readonly StringBuilder _cache; private readonly bool _isReactive; public Writer(TextWriter writer = null, bool isReactive = false) { _writer = writer ?? Console.Out; _isReactive = isReactive; if (!_isReactive) _cache = new StringBuilder(); } public Writer(string path) : this(new StreamWriter(path)) { } public Writer(bool isReactive) : this(null, isReactive) { } public void Write(object value) { if (_isReactive) { _writer.Write(value); _writer.Flush(); } else { _cache.Append(value); } } public void WriteFormat(string format, params object[] values) { var value = string.Format(format, values); Write(value); } public void WriteLine(object value = null) { Write($"{value}{Environment.NewLine}"); } public void WriteLine(string format, params object[] values) { WriteFormat($"{format}{Environment.NewLine}", values); } public void Dispose() { if (!_isReactive) { _writer.Write(_cache); _writer.Flush(); } if (!_writer.Equals(Console.Out)) { _writer.Dispose(); } } } public static class Extensions { public static void Set( this IDictionary source, TKey key, TValue value) { if (source.ContainsKey(key)) source[key] = value; else source.Add(key, value); } } #endregion class MainClass : IDisposable { #region "Template" /// 入力を受け取る private readonly Scanner sc; /// 出力を行う private readonly Writer wr; private const string _inputFile = "input.txt"; private const string _outFile = "output.txt"; static void Main(string[] args) { using (new MainClass()) { } } public MainClass() { wr = new Writer(_isReactive); // TODO: ファイルに出力したい場合はこっち → wr = new Writer(_outFile); #if DEBUG // 手元では、ファイルから入力を受け取る sc = new Scanner(_inputFile); #else // 提出時、標準入力から受け取る sc = new Scanner(); #endif Solve(); } public void Dispose() { sc?.Dispose(); wr?.Dispose(); } #endregion void Solve() { int N = sc.NextInt(); int Q = sc.NextInt(); SegmentTree tree = new SegmentTree(N); var a = sc.GetIntArray(N); for (int i = 0; i < N; i++) { tree.Update(i, a[i]); } for (int i = 0; i < Q; i++) { int id = sc.NextInt(); int l = sc.NextInt() - 1; int r = sc.NextInt() - 1; if (id == 1) { var leftVal = tree.Get(l, l + 1).Value; var rightVal = tree.Get(r, r + 1).Value; tree.Update(r, leftVal); tree.Update(l, rightVal); } else { var res = tree.Get(l, r + 1); wr.WriteLine(res.Index + 1); } } } /// /// TODO: リアクティブ問題か否かを指定してね♪ /// private const bool _isReactive = false; } class SegmentTree { public struct Pair : IComparable { public int Value { get; set; } public int Index { get; } public Pair(int value, int index) { Value = value; Index = index; } public int CompareTo(Pair other) => Value.CompareTo(other.Value); } private Pair[] Data { get; } public int Size { get; } public readonly Pair Inf = new Pair((int)1e9, -1); public SegmentTree(int N) { Size = N; Data = Enumerable.Range(0, N << 2) .Select(i => new Pair(Inf.Value, i)) .ToArray(); } public void Update(int index, int value) => UpdateInternal(index, value, 0, 0, Size); private void UpdateInternal(int index, int value, int k, int L, int R) { if (index < L || R <= index) return; if (L + 1 == R && index == L) { Data[k] = new Pair(value, index); return; } var mid = (L + R) >> 1; UpdateInternal(index, value, (k << 1) + 1, L, mid); UpdateInternal(index, value, (k << 1) + 2, mid, R); var val = Min(Data[(k << 1) + 1], Data[(k << 1) + 2]); Data[k] = val; } public Pair Get(int l, int r) => GetInternal(l, r, 0, 0, Size); private Pair GetInternal(int l, int r, int k, int L, int R) { if (R <= l || r <= L) return Inf; if (l <= L && R <= r) return Data[k]; var res = Inf; var mid = (L + R) >> 1; if (l < mid) res = Min(res, GetInternal(l, r, (k << 1) + 1, L, mid)); if (mid < r) res = Min(res, GetInternal(l, r, (k << 1) + 2, mid, R)); return res; } private Pair Min(Pair left, Pair right) => left.CompareTo(right) <= 0 ? left : right; } }