Problem:
Treap

A binary search tree can be unbalanced depending on features of data. For example, if we insert $n$ elements into a binary search tree in ascending order, the tree become a list, leading to long search times. One of strategies is to randomly shuffle the elements to be inserted. However, we should consider to maintain the balanced binary tree where different operations can be performed one by one depending on requirement.

We can maintain the balanced binary search tree by assigning a priority randomly selected to each node and by ordering nodes based on the following properties. Here, we assume that all priorities are distinct and also that all keys are distinct.

binary-search-tree property. If $v$ is a left child of $u$, then $v.key < u.key$ and if $v$ is a right child of $u$, then $u.key < v.key$

heap property. If $v$ is a child of $u$, then $v.priority < u.priority$

This combination of properties is why the tree is called Treap (tree + heap).

An example of Treap is shown in the following figure.

Insert

To insert a new element into a Treap, first of all, insert a node which a randomly selected priority value is assigned in the same way for ordinal binary search tree. For example, the following figure shows the Treap after a node with key = 6 and priority = 90 is inserted.

It is clear that this Treap violates the heap property, so we need to modify the structure of the tree by rotate operations. The rotate operation is to change parent-child relation while maintaing the binary-search-tree property.

The rotate operations can be implemented as follows.

rightRotate(Node t)
Node s = t.left
t.left = s.right
s.right = t
return s // the new root of subtree

leftRotate(Node t)
Node s = t.right
t.right = s.left
s.left = t
return s // the new root of subtree

The following figure shows processes of the rotate operations after the insert operation to maintain the properties.

The insert operation with rotate operations can be implemented as follows.

insert(Node t, int key, int priority) // search the corresponding place recursively
if t == NIL
return Node(key, priority) // create a new node when you reach a leaf
if key == t.key
return t // ignore duplicated keys

if key < t.key // move to the left child
t.left = insert(t.left, key, priority) // update the pointer to the left child
if t.priority < t.left.priority // rotate right if the left child has higher priority
t = rightRotate(t)
else // move to the right child
t.right = insert(t.right, key, priority) // update the pointer to the right child
if t.priority < t.right.priority // rotate left if the right child has higher priority
t = leftRotate(t)

return t

Delete

To delete a node from the Treap, first of all, the target node should be moved until it becomes a leaf by rotate operations. Then, you can remove the node (the leaf). These processes can be implemented as follows.

delete(Node t, int key) // seach the target recursively
if t == NIL
return NIL
if key < t.key // search the target recursively
t.left = delete(t.left, key)
else if key > t.key
t.right = delete(t.right, key)
else
return _delete(t, key)
return t

_delete(Node t, int key) // if t is the target node
if t.left == NIL && t.right == NIL // if t is a leaf
return NIL
else if t.left == NIL // if t has only the right child, then perform left rotate
t = leftRotate(t)
else if t.right == NIL // if t has only the left child, then perform right rotate
t = rightRotate(t)
else // if t has both the left and right child
if t.left.priority > t.right.priority // pull up the child with higher priority
t = rightRotate(t)
else
t = leftRotate(t)
return delete(t, key)

Write a program which performs the following operations to a Treap $T$ based on the above described algorithm.

insert ($k$, $p$): Insert a node containing $k$ as key and $p$ as priority to $T$.

find ($k$): Report whether $T$ has a node containing $k$.

delete ($k$): Delete a node containing $k$.

print(): Print the keys of the binary search tree by inorder tree walk and preorder tree walk respectively.

Input

In the first line, the number of operations $m$ is given. In the following $m$ lines, operations represented by insert $k \; p$, find $k$, delete $k$ or print are given.

Output

For each find($k$) operation, print "yes" if $T$ has a node containing $k$, "no" if not.

In addition, for each print operation, print a list of keys obtained by inorder tree walk and preorder tree walk in a line respectively. Put a space character before each key.

Constraints

The number of operations $ \leq 200,000$

$0 \leq k, p \leq 2,000,000,000$

The height of the binary tree does not exceed 50 if you employ the above algorithm

The keys in the binary search tree are all different.

The priorities in the binary search tree are all different.

The size of output $\leq 10$ MB

Sample Input 1

16
insert 35 99
insert 3 80
insert 1 53
insert 14 25
insert 80 76
insert 42 3
insert 86 47
insert 21 12
insert 7 10
insert 6 90
print
find 21
find 22
delete 35
delete 99
print

Sample Output 1

1 3 6 7 14 21 35 42 80 86
35 6 3 1 14 7 21 80 42 86
yes
no
1 3 6 7 14 21 42 80 86
6 3 1 80 14 7 21 42 86

Reference

Introduction to Algorithms, Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. The MIT Press.

Randomized Search Trees, R. Seidel, C.R. Aragon.

Solution in C# (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;



namespace CSharpSample01
{

    class Node
    {
        public int key;
        public int pri;  //priority
        public Node par;  //parent
        public Node left;
        public Node right;
        public Node(int k, int p)
        {
            key = k;
            pri = p;
            par = null;
            left = null;
            right = null;
        }
    }

    class Treap
    {
        Node root;  //根のノード
        int size;  //ノードの個数
        Node obj;
        int min;  //keyの最小値

        public Treap()
        {
            root = null;
            size = 0;
            min = 0;
        }

        public void Insert(int key, int priority)
        {
            if (size == 0) min = key;
            else min = Math.Min(min, key);

            //根がない場合、新しいノードを根とする
            if (root == null)
            {
                root = new Node(key, priority);
            }
            else
            {
                //まずは、二分木に従って挿入
                Node new_node = new Node(key, priority);
                Insert_Binary(new_node);

                //次に優先度に従って回転
                Insert_Prioriry(new_node);
            }
            size++;
        }

        //二分木に従ってnodeを挿入する
        //最終的にnodeは必ず葉として挿入される
        private void Insert_Binary(Node node)
        {
            //最初の探索対象ノードは根ノード
            Node cur = root;
            Node before = root;  //curノードの親ノードを一時的に保存

            //curにより挿入する葉の位置まで移動
            while (cur != null)
            {
                if (cur != root) before = cur.par.left == cur ? cur.par.left : cur.par.right;
                if (cur.key == node.key) return;  //keyの重複は認めない
                else if (cur.key > node.key) cur = cur.left;
                else cur = cur.right;
            }

            //nodeと親のbeforeの関係を設定
            node.par = before;

            if (node.key < before.key) before.left = node;
            else before.right = node;
            /*
            Console.WriteLine($"before:{before.key}");
            Console.WriteLine($"before.left:{(before.left != null ? before.left.key : -1)} " +
                $"before.right:{(before.right != null ? before.right.key : -1)}");
                */
        }

        //ヒープに従い、二分探索木の関係を壊さないまま回転し続ける
        //右回転または左回転を順次行う
        public void Insert_Prioriry(Node node)
        {
            Node parent = node.par;
            //Show2();
            while (true)
            {
                if (node.par == null) { root = node; break; }
                if (parent.pri > node.pri) break;
                //回転
                else if (parent.pri < node.pri)
                {                   
                    //Console.WriteLine($"node({node.key}, {node.pri}) p({parent.key}, {parent.pri})");
                    if (node == node.par.left) RightRotate(node);
                    else if (node == node.par.right) LeftRotate(node);
                    parent = node.par;
                    //Show2();
                }

            }
        }

        //右回転(nodeに関して右回転)
        private void RightRotate(Node node)
        {
            //Console.WriteLine("R");
            Node p = node.par;
            Node pp = node.par.par;
            Node r = node.right;

            //pとnode
            p.par = node;
            node.right = p;

            //rがnullの時を考慮
            if (r != null) r.par = p;
            p.left = r;

            //nodeとpp
            if (pp != null)
            {
                if (pp.left == p) pp.left = node;
                else pp.right = node;
            }
            else
            {
                //pp = nullの時、pは根だったので今度はnodeが根になる
                root = node;
            }
            node.par = pp;
        }

        //左回転(nodeに関して左回転)
        private void LeftRotate(Node node)
        {
            //Console.WriteLine("L");
            Node p = node.par;
            Node pp = node.par.par;
            Node l = node.left;

            //pとnode
            p.par = node;
            node.left = p;

            //lとp
            //lがnullの時を考慮
            if (l != null) l.par = p;
            p.right = l;

            //ppとnode
            if (pp != null)
            {
                if (pp.left == p) pp.left = node;
                else pp.right = node;
            }
            else
            {
                root = node;
            }
            node.par = pp;
        }

        //keyをもつノードを削除
        public void Erase(int key)
        {
            //Console.WriteLine("Start Erase");
            if (!Find(key)) return;         

            Node cur = obj;
            //Console.WriteLine($"Erase key:{cur.key}");
        
            while (cur.right != null || cur.left != null)
            {
                //Console.WriteLine($"cur.par key:{(cur.par != null ? cur.par.key : -1)}");
                if(cur.left != null && cur.right != null)
                {
                    if(cur.left.pri > cur.right.pri) RightRotate(cur.left);
                    else LeftRotate(cur.right);
                }
                else if (cur.left != null) RightRotate(cur.left);
                else LeftRotate(cur.right);              
            }

            if(key == min)
            {
                if (size >= 2) min = cur.par.key;
                else if (size == 1) min = 0;
            }
            
            //Console.WriteLine($"cur.par key:{(cur.par != null ? cur.par.key : -1)}");
            if (cur.par == null) root = null;
            else if (cur.par.left == cur) cur.par.left = null;
            else if (cur.par.right == cur) cur.par.right = null;
            size--;         
        }

        //keyの値を持つノードを二分木の探索方に従い再帰的に探す
        public bool Find(int key)
        {
            Node cur = root;
            obj = null;

            while (cur != null)
            {
                if (cur.key == key) { obj = cur; return true; }  //発見
                else if (cur.key > key) cur = cur.left;  //左の子へ
                else cur = cur.right;  //右の子へ
            }
            
            return false;
        }

        
        //表示
        public void Show()
        {
            if (size == 0)
            {
                Console.WriteLine("要素がありません");
                return;
            }
            Inorder(root);
            Console.WriteLine();
        }

        //表示2
        public void Show2()
        {
            if (size == 0)
            {
                Console.WriteLine("要素がありません");
                return;
            }
            Preorder(root);
            Console.WriteLine();
        }

        //中順で表示(二分探索目であるので昇順であるはず)
        public void Inorder(Node node)
        {
            if (node == null) return;

            Inorder(node.left);
            Console.Write($" {node.key}");
            Inorder(node.right);
        }

        //行きがけ順で表示
        public void Preorder(Node node)
        {
            if (node == null) return;

            Console.Write($" {node.key}");
            Preorder(node.left);
            Preorder(node.right);
        }

        //Treapのサイズを返す
        public int Size()
        {
            return size;
        }

        public void RootInfo()
        {
            if (root != null) Console.WriteLine($"key:{root.key} pri:{root.pri} " +
                 $"left:{(root.left != null ? root.left.key : -1)} " +
                 $"right:{(root.right != null ? root.right.key : -1)}");
        }

        public int GetMin()
        {
            return min;
        }
        
    }
    
    class Program
    {
        
        static void Main(string[] args)
        {
            
            int N = int.Parse(Console.ReadLine());
            Treap tp = new Treap();

            for (int i = 0; i < N; i++)
            {
                string[] input = Console.ReadLine().Split(' ');
                string order = input[0];

                int key = 0;
                int pri = 0;

                if (input.Count() == 2)
                {
                    key = int.Parse(input[1]);
                }
                if (input.Count() == 3)
                {
                    key = int.Parse(input[1]);
                    pri = int.Parse(input[2]);
                }

                if (order == "print") {tp.Show(); tp.Show2(); }
                else if (order == "size") Console.WriteLine($"size:{tp.Size()}");
                else if (order == "insert") tp.Insert(key, pri);
                else if (order == "find") Console.WriteLine(tp.Find(key) ? "yes" : "no");
                else if (order == "root") tp.RootInfo();
                else if (order == "show2") tp.Show2();
                else if (order == "delete") tp.Erase(key);
                else if (order == "end") break;
                else Console.WriteLine("no comand");
            }


        }
    }

}