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"); 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 static ImplicitTreap Merge(ImplicitTreap first, ImplicitTreap second) { if (first == null) { return(second); } if (second == null) { return(first); } ImplicitTreap newTreap; if (first.y > second.y) { newTreap = new ImplicitTreap(first.y, first.value, first.left, Merge(first.right, second)); } else { newTreap = new ImplicitTreap(second.y, second.value, Merge(first, second.left), second.right); } newTreap.Recalc(); return(newTreap); }
public ImplicitTreap Add(long value, long pos, long y) { ImplicitTreap first, second, middle; Split(pos, out first, out second); middle = new ImplicitTreap(y, value); return(Merge(Merge(first, middle), second)); }
public static long GetSize(ImplicitTreap treap) { if (treap == null) { return(0); } return(treap.size); }
public ImplicitTreap(long y, long value, ImplicitTreap left = null, ImplicitTreap right = null) { this.y = y; this.left = left; this.right = right; this.value = value; size = 1; }
public ImplicitTreap(double y, long value, ImplicitTreap left = null, ImplicitTreap right = null) { this.y = y; this.left = left; this.right = right; this.value = value; size = 1; min = long.MaxValue; Recalc(); }
static void DFS(ImplicitTreap current) { if (current == null) { return; } if (current.left != null) { DFS(current.left); } output.Write("{0} ", current.value); if (current.right != null) { DFS(current.right); } }
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(); }