static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); int[] x = new int[n]; int[] y = new int[n]; for (int i = 0; i < n; i++) { string[] s = Console.ReadLine().Split(' '); x[i] = int.Parse(s[0]); //y[i] = int.Parse(s[1]); //xy.Add(x[i], -int.Parse(s[1])); //dict.Add(x[i], i + 1); //ans.Add(i + 1, new vertex { }); } Array.Sort(x); /*for (int i = 0; i < n; i++) * { * y[i] = xy[x[i]]; * }*/ Treap myTreap = Treap.Build(x, y); // Console.WriteLine("YES"); DFS(myTreap, null); /* for (int i = 0; i < n; i++) * { * Console.WriteLine("{0} {1} {2}", ans[i + 1].parent, ans[i + 1].left, ans[i + 1].right); * }*/ //Console.ReadKey(); }
public void Split(int x0, out Treap first, out Treap second) { Treap newTreap = null; if (x <= x0) { if (right == null) { second = null; } else { right.Split(x0, out newTreap, out second); } first = new Treap(x, y, left, newTreap); } else { if (left == null) { first = null; } else { left.Split(x0, out first, out newTreap); } second = new Treap(x, y, newTreap, right); } }
public void Split(long x, out Treap L, out Treap R) { Treap newTree = null; if (this.x <= x) { if (Right == null) { R = null; } else { Right.Split(x, out newTree, out R); } L = new Treap(this.x, y, Left, newTree); } else { if (Left == null) { L = null; } else { Left.Split(x, out L, out newTree); } R = new Treap(this.x, y, newTree, Right); } }
static void AddUnique(long x) { if (treap == null) { treap = new Treap(x, rnd.NextDouble()); } else { Treap first, second, middle; treap.Split(x - 1, out first, out second); if (second != null) { second.Split(x, out middle, out second); if (middle == null) { middle = new Treap(x, rnd.NextDouble()); treap = Treap.Merge(Treap.Merge(first, middle), second); } } else { middle = new Treap(x, rnd.NextDouble()); treap = Treap.Merge(first, middle); } } }
private Treap(int x, int y, Treap left = null, Treap right = null, Treap parent = null) { this.x = x; this.y = y; this.left = left; this.right = right; this.parent = parent; }
public Treap(long x, double y, Treap left = null, Treap right = null) { this.x = x; this.y = y; this.Left = left; this.Right = right; min = x; Recalc(); }
static void DFS(Treap current, Treap parent) { ans[dict[current.x]] = new vertex { parent = (parent == null ? 0 : dict[parent.x]), left = (current.left == null ? 0 : dict[current.left.x]), right = (current.right == null ? 0 : dict[current.right.x]), }; if (current.left != null) { DFS(current.left, current); } if (current.right != null) { DFS(current.right, current); } }
public static Treap Merge(Treap first, Treap second) { if (first == null) { return(second); } if (second == null) { return(first); } if (first.y > second.y) { return(new Treap(first.x, first.y, first.left, Merge(first.right, second))); } else { return(new Treap(second.x, second.y, Merge(first, second.left), second.right)); } }
public static Treap Merge(Treap L, Treap R) { if (L == null) { return(R); } if (R == null) { return(L); } // TreapnewT; if (L.y > R.y) { return(new Treap(L.x, L.y, L.Left, Merge(L.Right, R))); } else { return(new Treap(R.x, R.y, Merge(L, R.Left), R.Right)); } }
public static Treap Build(int[] xs, int[] ys) { var tree = new Treap(xs[0], ys[0]); var last = tree; for (int i = 1; i < xs.Length; ++i) { if (last.y > ys[i]) { last.right = new Treap(xs[i], ys[i], parent: last); last = last.right; } else { Treap cur = last; while (cur.parent != null && cur.y <= ys[i]) { cur = cur.parent; } if (cur.y <= ys[i]) { last = new Treap(xs[i], ys[i], cur); } else { last = new Treap(xs[i], ys[i], cur.right, null, cur); cur.right = last; } } } while (last.parent != null) { last = last.parent; } return(last); }