Problem:
Problem I : RMQ

n 個の数字a0 ,a1 , ... ,an-1 とq が与えられる。

q 個のクエリーに対して適切な処理を行なって欲しい。

クエリーは以下の3種類の操作が存在する。

値のシフトを行う

l とr のペアが与えられる。(l < r )
al から ar までの値を Circular Shiftさせる。

0 1 2 3 4 5 6 7 8 9

に対してl=2,r=5というクエリーが与えられたとする。

シフトを行った数字列は

0 1 5 2 3 4 6 7 8 9

となる。

最小値を求める

l とr のペアが与えられる。(l ≤ r )

al から arまでの値の中で最小の値を求める。

値の更新

pos と val のペアが与えられる。
apos の値を val に更新する。

Input

入力は以下のフォーマットで与えられる。

n q
a0
a1
.
.
.
an-1
x1 y1 z1
x2 y2 z2
.
.
.
xq yq zq

xi = 0 なら、シフトのクエリーを表す。このとき、l = yi , r = zi とする。

xi = 1 なら、最小値を求めるクエリーを表す。このとき、l = yi , r = zi とする。

xi = 2 なら、値を更新するクエリーを表す。このとき、pos = yi , val = zi とする。

入力は以下の制約を満たす

1 ≤ n , q ≤ 200,000

0 ≤ ai ≤ 10,000,000

値を更新するクエリーについて、0 ≤ val ≤ 10,000,000

Output

クエリーとしてxi = 1 が与えられたときに、答えの値を１行に出力せよ。

Sample Input 1

10 3
0
1
2
3
4
5
6
7
8
9
1 5 7
0 2 5
1 5 7

Sample Output 1

5
4

Sample Input 2

10 10
289383
930886
692777
636915
747793
238335
885386
760492
516649
641421
2 7 368690
1 3 6
0 2 6
1 1 8
0 2 9
2 2 665123
1 5 9
0 2 8
2 7 961393
1 1 2

Sample Output 2

238335
238335
238335
368690

Solution in C# (judge verdict: Accepted)

using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Diagnostics;
//using System.Numerics;
using Enu = System.Linq.Enumerable;

public class Program
{
    public void Solve()
    {
        Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false });
        int N = Reader.Int(), Q = Reader.Int();
        var A = Reader.IntArray(N);
        var tree = new Treap(N);
        for (int i = 0; i < N; i++) tree.Insert(i, A[i]);

        foreach (var q in Reader.IntTable(Q))
            if (q[0] == 0)
            {
                // b1, b2 -> b2, b1
                int L = q[1], R = q[2] + 1;
                int a, b, b1, b2, c, t;
                tree.Split(tree.Root, R, out t, out c);
                tree.Split(t, L, out a, out b);
                tree.Split(b, R - L - 1, out b1, out b2);
                tree.Root = tree.Merge(a, b2);
                tree.Root = tree.Merge(tree.Root, b1);
                tree.Root = tree.Merge(tree.Root, c);
            }
            else if (q[0] == 1)
                Console.WriteLine(tree.Min(q[1], q[2] + 1));
            else
                tree[q[1]] = q[2];
        Console.Out.Flush();
        Console.ReadLine();

    }

    public class Treap
    {
        public struct Node
        {
            public int L, R;
            public int Count;
            public long Val;
            public long Min;
            //public long Max;
            //public long Sum;
            public readonly uint Priority;
            static readonly XorShift random = new XorShift();

            public Node(long val, uint priority = uint.MaxValue)
            {
                L = R = None; Val = val; Count = 1;
                Min = val;
                //Max = val;
                //Sum = val;
                Priority = (priority == uint.MaxValue) ? random.Next() : priority;
            }
        }
        public class XorShift
        {
            uint m = 2463534242;
            public uint Next() { m = m ^ m << 13; m = m ^ m >> 17; return m = m ^ m << 5; }
        }

        static readonly int None = 0;
        int nodesIndex = 1;
        public Node[] nodes;
        public int Root;
        public int Count { get { return nodes[Root].Count; } }
        public Treap(int size) { Init(size); }

        private void Init(int size)
        {
            nodes = new Node[size + 1];
            nodes[None].Min = long.MaxValue;
            //nodes[None].Max = long.MinValue;
        }
        int GenNode(long val, uint priority = uint.MaxValue)
        {
            int i = nodesIndex++;
            nodes[i] = new Node(val, priority);
            return i;
        }

        //[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
        int Update(int i)
        {
            nodes[i].Count = nodes[nodes[i].L].Count + nodes[nodes[i].R].Count + 1;
            nodes[i].Min = Math.Min(nodes[i].Val, Math.Min(nodes[nodes[i].L].Min, nodes[nodes[i].R].Min));
            //nodes[i].Max = Math.Max(nodes[i].Val, Math.Max(nodes[nodes[i].L].Max, nodes[nodes[i].R].Max));
            return i;
        }

        int[] setTmp = new int[128];
        public long this[int index]
        {
            get
            {
                Debug.Assert(index >= 0 && index < nodes[Root].Count);
                int i = Root;
                while (index != nodes[nodes[i].L].Count)
                    if (index < nodes[nodes[i].L].Count) i = nodes[i].L;
                    else { index -= nodes[nodes[i].L].Count + 1; i = nodes[i].R; }
                return nodes[i].Val;
            }
            set
            {
                //if (index < nodes[Root].Count) RemoveAt(index);
                //Insert(index, value);

                int num = 0;
                int i = Root, Lsize;
                while ((Lsize = nodes[nodes[i].L].Count) != index)
                    if (index < Lsize) { setTmp[num++] = i; i = nodes[i].L; }
                    else { setTmp[num++] = ~i; index -= Lsize + 1; i = nodes[i].R; }
                nodes[i].Val = value;
                Update(i);
                while (--num >= 0)
                {
                    int v = setTmp[num];
                    if (v >= 0) nodes[v].L = i;
                    else { v = ~v; nodes[v].R = i; }
                    Update(v);
                    i = v;
                }
            }
        }

        int[] MergeTmp = new int[128];
        public int Merge(int L, int R)
        {
            int num = 0;
            int child = 0;
            for (; ; )
            {
                if (L == None) { child = R; break; }
                if (R == None) { child = L; break; }
                if (nodes[L].Priority > nodes[R].Priority)
                {
                    MergeTmp[num++] = ~L;
                    L = nodes[L].R;
                }
                else
                {
                    MergeTmp[num++] = R;
                    R = nodes[R].L;
                }
            }
            while (--num >= 0)
            {
                int v = MergeTmp[num];
                if (v >= 0) nodes[v].L = child;
                else { v = ~v; nodes[v].R = child; }
                Update(v);
                child = v;
            }
            return child;

            //if (L == None) return R;
            //if (R == None) return L;
            //if (nodes[L].Priority > nodes[R].Priority)
            //{
            //    nodes[L].R = Merge(nodes[L].R, R);
            //    return Update(L);
            //}
            //else
            //{
            //    nodes[R].L = Merge(L, nodes[R].L);
            //    return Update(R);
            //}
        }

        int[] SplitTmp = new int[128];
        public void Split(int i, int index, out int L, out int R)
        {
            if (i == None) { L = R = None; return; }

            L = R = None;
            int num = 0, Lsize;
            for (; ; )
            {
                if (i == None) { L = R = None; break; }
                Lsize = nodes[nodes[i].L].Count;
                if (index == Lsize)
                {
                    L = nodes[i].L;
                    nodes[i].L = None;
                    R = Update(i);
                    break;
                }
                if (index < Lsize) { SplitTmp[num++] = i; i = nodes[i].L; }
                else { SplitTmp[num++] = ~i; index -= Lsize + 1; i = nodes[i].R; }
            }
            while (--num >= 0)
            {
                int v = SplitTmp[num];
                if (v >= 0) { nodes[v].L = R; R = Update(v); }
                else { v = ~v; nodes[v].R = L; L = Update(v); }
                Update(v);
            }


            //if (index == nodes[nodes[i].L].Count)
            //{
            //    L = nodes[i].L;
            //    R = i;
            //    nodes[i].L = None;
            //    Update(i);
            //    return;
            //}
            //int a, b;
            //if (index <= nodes[nodes[i].L].Count)
            //{
            //    Split(nodes[i].L, index, out a, out b);
            //    nodes[i].L = b;
            //    L = a;
            //    R = Update(i);
            //}
            //else
            //{
            //    Split(nodes[i].R, index - nodes[nodes[i].L].Count - 1, out a, out b);
            //    nodes[i].R = a;
            //    L = Update(i);
            //    R = b;
            //}
        }

        public void Insert(int index, long val) { Root = Insert(Root, GenNode(val), index); }

        int[] InsertTmp = new int[128];
        public int Insert(int i, int newNodeIndex, int index)
        {
            int num = 0, Lsize;
            for (; ; )
            {
                if (i == None) { i = newNodeIndex; break; }
                if (nodes[newNodeIndex].Priority > nodes[i].Priority)
                {
                    int L, R;
                    Split(i, index, out L, out R);
                    i = Merge(L, newNodeIndex);
                    i = Merge(i, R);
                    break;
                }
                Lsize = nodes[nodes[i].L].Count;
                if (index <= Lsize) { InsertTmp[num++] = i; i = nodes[i].L; }
                else { InsertTmp[num++] = ~i; index -= Lsize + 1; i = nodes[i].R; }
            }
            while (--num >= 0)
            {
                int v = InsertTmp[num];
                if (v >= 0) nodes[v].L = i;
                else { v = ~v; nodes[v].R = i; }
                Update(v);
                i = v;
            }
            return i;


            //if (i == None) return newNodeIndex;
            //if (nodes[newNodeIndex].Priority > nodes[i].Priority)
            //{
            //    int L, R;
            //    Split(i, index, out L, out R);
            //    i = Merge(L, newNodeIndex);
            //    i = Merge(i, R);
            //}
            //else
            //{
            //    if (index <= nodes[nodes[i].L].Count)
            //        nodes[i].L = Insert(nodes[i].L, newNodeIndex, index);
            //    else
            //        nodes[i].R = Insert(nodes[i].R, newNodeIndex, index - nodes[nodes[i].L].Count - 1);
            //    Update(i);
            //}
            //return i;
        }


        public void RemoveAt(int index) { Root = RemoveAt(Root, index); }

        int[] RemoveAtTmp = new int[128];
        int RemoveAt(int i, int index)
        {
            int num = 0, Lsize;
            for (; (Lsize = nodes[nodes[i].L].Count) != index; )
                if (index < Lsize) { RemoveAtTmp[num++] = i; i = nodes[i].L; }
                else { RemoveAtTmp[num++] = ~i; index -= Lsize + 1; i = nodes[i].R; }
            i = Merge(nodes[i].L, nodes[i].R);
            while (--num >= 0)
            {
                int v = RemoveAtTmp[num];
                if (v >= 0) nodes[v].L = i;
                else { v = ~v; nodes[v].R = i; }
                Update(v);
                i = v;
            }
            return i;


            //if (index == nodes[nodes[i].L].Count)
            //    return Merge(nodes[i].L, nodes[i].R);
            //if (index < nodes[nodes[i].L].Count)
            //    nodes[i].L = RemoveAt(nodes[i].L, index);
            //else
            //    nodes[i].R = RemoveAt(nodes[i].R, index - nodes[nodes[i].L].Count - 1);
            //return Update(i);
        }


        //long RangeQuery(int i, int L, int R, long init, Func<int, long> nF, Func<long, long, long> F)
        //{
        //    if (i == None || L >= R) return init;
        //    if (L == 0 && R == nodes[i].Count) return nF(i);
        //    long res = init;
        //    int Lsize = nodes[nodes[i].L].Count;
        //    if (Lsize >= L && Lsize < R)
        //        res = nodes[i].Val;
        //    if (L < Lsize)
        //        res = F(res, RangeQuery(nodes[i].L, L, Math.Min(R, Lsize), init, nF, F));
        //    if (R - Lsize - 1 > 0)
        //        res = F(res, RangeQuery(nodes[i].R, Math.Max(0, L - Lsize - 1), R - Lsize - 1, init, nF, F));
        //    return res;
        //}
        public long Min(int L, int R)
        {
            return Min(Root, L, R);
            //return RangeQuery(Root, L, R, long.MaxValue, i => nodes[i].Min, Math.Min);
        }
        public long Min(int i, int L, int R)
        {
            if (i == None || L >= R) return long.MaxValue;
            if (L == 0 && R == nodes[i].Count) return nodes[i].Min;
            long res = long.MaxValue;
            int Lsize = nodes[nodes[i].L].Count;
            if (Lsize >= L && Lsize < R)
                res = nodes[i].Val;
            if (L < Lsize)
                res = Math.Min(res, Min(nodes[i].L, L, Math.Min(R, Lsize)));
            if (R - Lsize - 1 > 0)
                res = Math.Min(res, Min(nodes[i].R, Math.Max(0, L - Lsize - 1), R - Lsize - 1));
            return res;

        }

        //public long Max(int L, int R) { return RangeQuery(Root, L, R, long.MinValue, n => n.Max, Math.Max); }
        //public long Sum(int L, int R) { return RangeQuery(Root, L, R, 0, n => n.Sum, (a, b) => a + b); }

    }

}

class Entry { static void Main() { new Program().Solve(); } }
class Reader
{
    static TextReader reader = Console.In;
    static readonly char[] separator = { ' ' };
    static readonly StringSplitOptions op = StringSplitOptions.RemoveEmptyEntries;
    static string[] A = new string[0];
    static int i;
    static void Init() { A = new string[0]; }
    public static void Set(TextReader r) { reader = r; Init(); }
    public static void Set(string file) { reader = new StreamReader(file); Init(); }
    public static bool HasNext() { return CheckNext(); }
    public static string String() { return Next(); }
    public static int Int() { return int.Parse(Next()); }
    public static long Long() { return long.Parse(Next()); }
    public static double Double() { return double.Parse(Next()); }
    public static int[] IntLine() { return Array.ConvertAll(Split(Line()), int.Parse); }
    public static int[] IntArray(int N) { return Range(N, Int); }
    public static int[][] IntTable(int H) { return Range(H, IntLine); }
    public static string[] StringArray(int N) { return Range(N, Next); }
    public static string[][] StringTable(int N) { return Range(N, () => Split(Line())); }
    public static string Line() { return reader.ReadLine().Trim(); }
    static string[] Split(string s) { return s.Split(separator, op); }
    static T[] Range<T>(int N, Func<T> f) { var r = new T[N]; for (int i = 0; i < N; r[i++] = f()) ; return r; }
    static string Next() { CheckNext(); return A[i++]; }
    static bool CheckNext()
    {
        if (i < A.Length) return true;
        string line = reader.ReadLine();
        if (line == null) return false;
        if (line == "") return CheckNext();
        A = Split(line);
        i = 0;
        return true;
    }
}