Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Program
- {
- public class Treap
- {
- public class Node
- {
- int key, prior;
- Node l, r;
- public Node(int key, int prior)
- {
- this.key = key;
- this.prior = prior;
- l = null;
- r = null;
- }
- public static void Split(Node t, int key, ref Node l, ref Node r)
- {
- if (t == null)
- l = r = null;
- else if (key < t.key)
- {
- Split(t.l, key, ref l, ref t.l); r = t;
- }
- else
- {
- Split(t.r, key, ref t.r, ref r); l = t;
- }
- }
- public static void Add(ref Node t, Node it)
- {
- if (t == null)
- t = it;
- else if (it.prior > t.prior)
- {
- Split(t, it.key, ref it.l, ref it.r); t = it;
- }
- else
- {
- if (it.key < t.key) Add(ref t.l, it);
- else Add(ref t.r, it);
- //insert( it.key < t.key ? t.l : t.r, it);
- }
- }
- public static void Merge( ref Node t, Node l, Node r)
- {
- if (l == null || r == null)
- t = l == null ? l : r;
- else if (l.prior > r.prior)
- {
- Merge(ref l.r, l.r, r); t = l;
- }
- else
- {
- Merge(ref r.l, l, r.l); t = r;
- }
- }
- public static void Remove(ref Node t, int key)
- {
- if (t.key == key)
- Merge(ref t, t.l, t.r);
- else
- {
- if (key < t.key) Remove(ref t.l, key);
- else Remove(ref t.r, key);
- //erase(key < t.key ? t.l : t.r, key);
- }
- }
- //public Node Unite(Node l, Node r)
- //{
- // if (l == null || r == null) return l != null ? l : r;
- // if (l.prior < r.prior)
- // {
- // Node tmp = l;
- // l = r;
- // r = tmp;
- // }
- // Node lt = null, rt = null;
- // Split(r, l.key, ref lt, ref rt);
- // l.l = unite(l.l, lt);
- // l.r = unite(l.r, rt);
- // return l;
- //}
- public static void Print (Node tmp)
- {
- if (tmp != null)
- {
- Console.WriteLine("{0} {1}", tmp.key, tmp.prior);
- Print(tmp.l);
- Print(tmp.r);
- }
- }
- }
- Node tree;
- public Treap()
- {
- tree = null;
- }
- public void Add (int x, int y)
- {
- Node tmp = new Node(x, y);
- Node.Add(ref tree, tmp);
- }
- public void Remove(int x)
- {
- Node.Remove(ref tree, x);
- }
- public void Print()
- {
- Node.Print(tree);
- }
- }
- static void Main(string[] args)
- {
- StreamReader fin = new StreamReader("input.txt");
- string temp = fin.ReadToEnd();
- string[] numbers = temp.Split(' ');
- fin.Close();
- Random rand = new Random();
- Treap tr = new Treap();
- foreach (string item in numbers)
- tr.Add(int.Parse(item),rand.Next(200));
- tr.Print();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement