static void Main(string[] args) { TextReader input = new StreamReader("input.txt"); output = new StreamWriter("output.txt"); string[] s = input.ReadLine().Split(' '); int n = int.Parse(s[0]); int m = int.Parse(s[1]); Random rnd = new Random(); treap = new ImplicitTreap(rnd.Next(), 1); for (int i = 2; i <= n; i++) { treap = treap.Add(i, i, rnd.Next()); } for (int i = 0; i < m; i++) { s = input.ReadLine().Split(' '); int l = int.Parse(s[0]); int r = int.Parse(s[1]); ImplicitTreap first, second, middle; treap.Split(l - 1, out first, out second); second.Split(r - l + 1, out middle, out second); treap = ImplicitTreap.Merge(middle, ImplicitTreap.Merge(first, second)); } DFS(treap); output.Close(); //Console.ReadKey(); }
public void Split(long x0, out ImplicitTreap first, out ImplicitTreap second) { ImplicitTreap newTreap = null; if (GetSize(left) + 1 > x0) { if (left == null) { first = null; } else { left.Split(x0, out first, out newTreap); } second = new ImplicitTreap(y, value, newTreap, right); second.Recalc(); } else { if (right == null) { second = null; } else { right.Split(x0 - GetSize(left) - 1, out newTreap, out second); } first = new ImplicitTreap(y, value, left, newTreap); first.Recalc(); } }
static void Main(string[] args) { TextReader input = new StreamReader("input.txt"); TextWriter output = new StreamWriter("output.txt"); int n = int.Parse(input.ReadLine()); ImplicitTreap treap = null; Random rnd = new Random(); for (int i = 0; i < n; i++) { string[] s = input.ReadLine().Split(' '); if (s[0][0] == '+') { int x0 = int.Parse(s[1]); int arg = int.Parse(s[2]); ImplicitTreap tmp = new ImplicitTreap(rnd.NextDouble(), arg); if (x0 == 0) { treap = ImplicitTreap.Merge(tmp, treap); } else { ImplicitTreap first, second; treap.Split(x0, out first, out second); treap = ImplicitTreap.Merge(ImplicitTreap.Merge(first, tmp), second); } } else { int l = int.Parse(s[1]); int r = int.Parse(s[2]); ImplicitTreap first, second, middle; treap.Split(l - 1, out first, out second); second.Split(r - l + 1, out middle, out second); middle.Recalc(); output.WriteLine(middle.min); } } //Console.ReadKey(); output.Close(); /*string[] s = Console.ReadLine().Split(' '); * int n = int.Parse(s[0]); * int m = int.Parse(s[1]); * * s = Console.ReadLine().Split(' '); * Random rnd = new Random(); * ImplicitTreap treap = new ImplicitTreap(rnd.Next(), int.Parse(s[0])); * for (int i = 1; i < n; i++) * { * treap = ImplicitTreap.Merge(treap, new ImplicitTreap(rnd.Next(), int.Parse(s[i]))); * } * * for (int i = 0; i < m; i++) * { * s = Console.ReadLine().Split(' '); * if (s[0] == "2") * { * int l = int.Parse(s[1]); * int r = int.Parse(s[2]); * ImplicitTreap first, second, middle; * treap.Split(l - 1, out first, out second); * second.Split(r - l + 1, out middle, out second); * //middle.Recalc(); * Console.WriteLine(middle.min); * } * else * { * int l = int.Parse(s[1]); * int r = int.Parse(s[2]); * treap = treap.Reverse(l, r); * } * }*/ //Console.ReadKey(); }