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 (Python):
class node:
    def __init__(self,key,pri,left=None,right=None):
        self.left = left
        self.right = right
        self.key = key
        self.pri = pri

def insert(n,key,pri):
    if(n == None):
        return node(key,pri)
    if(key == n.key):
        return n
    if(key < n.key):
        n.left = insert(n.left, key, pri)
        if(n.pri < n.left.pri):
            n = rightRotate(n)
    else:
        n.right = insert(n.right, key, pri)
        if(n.pri < n.right.pri):
            n = leftRotate(n)
    return n

def rightRotate(n):
    child = n.left
    swapL,swapR,swapK,swapP = child.left,child.right,child.key,child.pri
    n.left = swapR
    n = node(swapK,swapP,swapL,n)
    return n

def leftRotate(n):
    child = n.right
    swapL,swapR,swapK,swapP = child.left,child.right,child.key,child.pri
    n.right = swapL
    n = node(swapK,swapP,n,swapR)
    return n

def find(n,key):
    if(n == None):
        return None
    elif(n.key == key):
        return n
    elif(n.key > key):
        return find(n.left,key)
    else:
        return find(n.right,key)

def delete(n, key):
    if(n == None):
        return None
    if(key < n.key):
        n.left = delete(n.left, key)
    elif(key > n.key):
        n.right = delete(n.right, key)
    else:
        return _delete(n, key)
    return n

def _delete(n, key):
    if(n.left == None and n.right == None):
        return None
    elif(n.left == None):
        n = leftRotate(n)
    elif(n.right == None):
        n = rightRotate(n)
    else:
        if(n.left.pri > n.right.pri):
            n = rightRotate(n)
        else:
            n = leftRotate(n)
    return delete(n, key)

def inorder(n):
    if(n==None):
        return
    if(n.left != None):
        inorder(n.left)
    print(" "+str(n.key), end='')
    if(n.right != None):
        inorder(n.right)

def preorder(n):
    if(n==None):
        return
    print(" "+str(n.key), end='')
    if(n.left != None):
        preorder(n.left)
    if(n.right != None):
        preorder(n.right)

op_num=(int)(input())
root = None
for i in range(op_num):
    items = input().split()
    if(items[0] == "insert"):
        root = insert(root,(int)(items[1]),(int)(items[2]))
    elif(items[0] == "delete"):
        root = delete(root,(int)(items[1]))
    elif(items[0] == "find"):
        res = find(root,(int)(items[1]))
        if(res == None):
            print("no")
        else:
            print("yes")
    else:
        inorder(root)
        print("")
        preorder(root)
        print("")

Solution (Python):
class Node:
    def __init__(self, key, priority):
        self.right = None
        self.left = None
        self.parent = None
        self.key = key
        self.priority = priority

    def preorder(self):
        ret = [self.key]
        if self.left:
            ret += self.left.preorder()
        if self.right:
            ret += self.right.preorder()
        return ret

    def inorder(self):
        ret = []
        if self.left:
            ret += self.left.inorder()
        ret += [self.key]
        if self.right:
            ret += self.right.inorder()
        return ret

    def find(self, k):
        if self.key == k:
            return self
        elif self.key < k:
            if self.right:
                return self.right.find(k)
            else:
                return None
        else:
            if self.left:
                return self.left.find(k)
            else:
                return None


class Treap:
    def __init__(self):
        self.root = None

    def right_rotate(self, t):
        s = t.left
        t.left = s.right
        s.right = t
        return s

    def left_rotate(self, t):
        s = t.right
        t.right = s.left
        s.left = t
        return s

    def insert(self, k, p):
        self.root = self._insert(self.root, k, p)

    def _insert(self, t, k, p):
        if t is None:
            return Node(k, p)
        if k == t.key:
            return t
        if k < t.key:
            t.left = self._insert(t.left, k, p)
            if t.priority < t.left.priority:
                t = self.right_rotate(t)
        else:
            t.right = self._insert(t.right, k, p)
            if t.priority < t.right.priority:
                t = self.left_rotate(t)
        return t

    def print(self):
        if self.root is None:
            print()
        else:
            print('', ' '.join(map(str, self.root.inorder())))
            print('', ' '.join(map(str, self.root.preorder())))

    def find(self, k):
        if self.root is None:
            return None
        else:
            return self.root.find(k)

    def delete(self, k):
        self.root = self._delete(self.root, k)

    def _delete(self, t, k):
        if t is None:
            return None
        if k < t.key:
            t.left = self._delete(t.left, k)
        elif k > t.key:
            t.right = self._delete(t.right, k)
        else:
            return self._else_delete(t, k)
        return t

    def _else_delete(self, t, k):
        if t.left is None and t.right is None:
            return None
        elif t.left is None:
            t = self.left_rotate(t)
        elif t.right is None:
            t = self.right_rotate(t)
        else:
            if t.left.priority > t.right.priority:
                t = self.right_rotate(t)
            else:
                t = self.left_rotate(t)
        return self._delete(t, k)


m = int(input())
treap = Treap()
for _ in range(m):
    s = input().split()
    if s[0] == "insert":
        treap.insert(int(s[1]), int(s[2]))
    elif s[0] == "find":
        if treap.find(int(s[1])):
            print("yes")
        else:
            print("no")
    elif s[0] == "delete":
        treap.delete(int(s[1]))
    else:
        treap.print()

Solution (Python):
class Node:
    def __init__(self, key, priority):
        self.key = key
        self.priority = priority
        self.parent = None
        self.left = self.right = None
    def preorder(self):
        L = [self.key]
        if self.left: L += self.left.preorder()
        if self.right: L += self.right.preorder()
        return L
    def inorder(self):
        L = []
        if self.left: L += self.left.inorder()
        L.append(self.key)
        if self.right: L += self.right.inorder()
        return L

def rightRotate(t):
    s = t.left
    t.left = s.right
    s.right = t
    return s 
def leftRotate(t):
    s = t.right
    t.right = s.left
    s.left = t
    return s 

def insert(t, key, priority):
    if not t:
        return Node(key, priority) 
    if key == t.key:
        return t 
    if key < t.key: 
        t.left = insert(t.left, key, priority) 
        if t.priority < t.left.priority: 
            t = rightRotate(t)
    else: 
        t.right = insert(t.right, key, priority) 
        if t.priority < t.right.priority: 
            t = leftRotate(t)
    return t

def delete(t, key):
    if not t:
        return None
    if key < t.key: 
        t.left = delete(t.left, key)
    elif key > t.key:
        t.right = delete(t.right, key)
    else:
        return _delete(t, key)
    return t
def _delete(t, key):
    if not t.left and not t.right: 
        return None
    if not t.left: 
        t = leftRotate(t)
    elif not t.right: 
        t = rightRotate(t)
    else: 
        if t.left.priority > t.right.priority:
            t = rightRotate(t)
        else:
            t = leftRotate(t)
    return delete(t, key)

def find(t, k):
    x = t
    while x:
        if x.key == k:
            return x
        x = x.left if k < x.key else x.right
    return None

t = None
m = int(input())
for _ in range(m):
    cmd = list(input().split())
    if cmd[0] == 'insert':
        t = insert(t, int(cmd[1]), int(cmd[2]))
    if cmd[0] == 'find':
        print('yes' if find(t, int(cmd[1])) else 'no')
    if cmd[0] == 'delete':
        t = delete(t, int(cmd[1]))
    if cmd[0] == 'print':
        print('', *t.inorder())
        print('', *t.preorder())

Solution (Python):
import sys, collections
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = 10**10
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

class Node():
    key = None
    priority = None
    p = None
    left = None
    right = None

    def __init__(self, key, priority):
        self.key = key
        self.priority = priority

class BinarySearchTree():
    root = None

    def rightRotate(self, t):
        s = t.left
        t.left = s.right
        s.right = t
        return s

    def leftRotate(self, t):
        s = t.right
        t.right = s.left
        s.left = t
        return s

    def insert(self, t, key, priority):
        if t is None:
            return Node(key, priority)
        if key==t.key:
            return t

        if key < t.key:
            t.left = self.insert(t.left, key, priority)
            if t.priority < t.left.priority:
                t = self.rightRotate(t)
        else:
            t.right = self.insert(t.right, key, priority)
            if t.priority < t.right.priority:
                t = self.leftRotate(t)
        
        return t

    def find(self, k):
        x = self.root
        while x is not None:
            if x.key==k:
                return x
            elif x.key>k:
                x = x.left
            else:
                x = x.right
        return None

    def next_node(self, z):
        if z.right is None:
            x = z
            y = z.p
            while y is not None and y.left!=x:
                x = y
                y = y.p
            return y
        else:
            x = z.right
            while x is not None:
                y = x
                x = x.left
            return y

    def delete(self, t, key):
        if t is None:
            return None
        if key < t.key:
            t.left = self.delete(t.left, key)
        elif key > t.key:
            t.right = self.delete(t.right, key)
        else:
            return self._delete(t, key)
        return t

    def _delete(self, t, key):
        if t.left is None and t.right is None:
            return None
        elif t.left is None:
            t = self.leftRotate(t)
        elif t.right is None:
            t = self.rightRotate(t)
        else:
            if t.left.priority > t.right.priority:
                t = self.rightRotate(t)
            else:
                t = self.leftRotate(t)
        return self.delete(t, key)
            
    def print_nodes(self):
        inorder = []
        preorder = []

        def dfs(v):
            v_key = v.key
            preorder.append(v_key)
            if v.left is not None:
                dfs(v.left)
            inorder.append(v_key)
            if v.right is not None:
                dfs(v.right)

        dfs(self.root)

        print('', *inorder)
        print('', *preorder)

def resolve():
    m = I()
    T = BinarySearchTree()
    for _ in range(m):
        q = LSS()
        if q[0]=='insert':
            k = int(q[1])
            p = int(q[2])
            T.root = T.insert(T.root, k, p)
        elif q[0]=='find':
            k = int(q[1])
            if T.find(k):
                print('yes')
            else:
                print('no')
        elif q[0]=='delete':
            k = int(q[1])
            T.root = T.delete(T.root, k)
        else:
            T.print_nodes()

if __name__ == '__main__':
    resolve()

Solution (Python):
class Node:
    def __init__(self,key,priority,left=None,right=None):
        self.left = left
        self.right = right
        self.key = key
        self.priority = priority

def insert(t,key,priority):
    if t == None :
        return Node(key,priority)
    if key == t.key :
        return t
    if key < t.key :
        t.left = insert(t.left, key, priority)
        if t.priority < t.left.priority :
            t = rightRotate(t)
    else:
        t.right = insert(t.right, key, priority)
        if t.priority < t.right.priority :
            t = leftRotate(t)
    return t


def rightRotate(t):
    s = t.left
    t.left = s.right
    s.right = t
    return s


def leftRotate(t):
    s= t.right
    t.right = s.left
    s.left = t
    return s


def find(t,key):
    if t == None: return None
    elif t.key == key:return t
    elif t.key > key :return find(t.left,key)
    else: return find(t.right,key)

def erase(t, key):
    if t == None : return None
    if key == t.key:
        if t.left == None and t.right == None:return None
        elif t.left == None : t = leftRotate(t)
        elif t.right == None : t = rightRotate(t)
        else:
            if t.left.priority > t.right.priority:t = rightRotate(t)
            else: t = leftRotate(t)
        return erase(t, key)
    elif key < t.key : t.left = erase(t.left, key)
    else : t.right = erase(t.right, key)
    return t

def inorder(t):
    if t.left != None:inorder(t.left)
    print(" "+str(t.key), end='')
    if t.right != None: inorder(t.right)

def preorder(t):
    print(" "+str(t.key), end='')
    if t.left != None :preorder(t.left)
    if t.right != None : preorder(t.right)

if __name__=="__main__":
        m=(int)(input())
        root = None
        for i in range(m):
            order = input().split()
            if order[0] == "insert" :root = insert(root,(int)(order[1]),(int)(order[2]))
            elif order[0] == "delete" :root = erase(root,(int)(order[1]))
            elif order[0] == "find" :
                res = find(root,(int)(order[1]))
                if res == None :print("no")
                else:print("yes")
            else:
                inorder(root)
                print()
                preorder(root)
                print()

Solution (Python):
class Node:
    def __init__(self, x, priority):
        self.data = x
        self.priority = priority
        self.left = None
        self.right = None

def rightRotate(node):
    leftNode = node.left
    node.left = leftNode.right
    leftNode.right = node
    return leftNode

def leftRotate(node):
    rightNode = node.right
    node.right = rightNode.left
    rightNode.left = node
    return rightNode

def find(node, x):
    while node is not None:
        if x == node.data:
            return True
        elif x < node.data:
            node = node.left
        else:
            node = node.right
    return False

def insert(node, x, priority):
    if node is None:
        return Node(x, priority)

    if x == node.data:
        return node

    if x < node.data:
        node.left = insert(node.left, x, priority)
        if node.priority < node.left.priority:
            node = rightRotate(node)

    else:
        node.right = insert(node.right, x, priority)
        if node.priority < node.right.priority:
            node = leftRotate(node)
    return node

def inorder_print(node, x = 0):
    if node is not None:
        print('',node.data, end = '')
        inorder_print(node.left, x + 1)
        inorder_print(node.right, x + 1)

def preorder_print(node, x = 0):
    if node is not None:
        preorder_print(node.left, x + 1)
        print('',node.data, end = '')
        preorder_print(node.right, x + 1)


def delete(node, x):
    if node == None:
        return None
    if x < node.data:
        node.left = delete(node.left, x)
    elif x > node.data:
        node.right = delete(node.right, x)
    else:
        return _delete(node, x)

    return node

def _delete(node, x):
    if node.left == None and node.right == None:
        return None
    elif node.left == None:
        node = leftRotate(node)
    elif node.right == None:
        node = rightRotate(node)
    else:
        if node.left.priority > node.right.priority:
            node = rightRotate(node)
        else:
            node = leftRotate(node)
    return delete(node, x)

class Treap:
    def __init__(self):
        self.root = None

    def find_(self, x):
        return find(self.root, x)

    def insert_(self, x, pri):
        self.root = insert(self.root, x, pri)

    def delete_(self, x):
        self.root = delete(self.root, x)

    def traverse_(self):
        for x in traverse(self.root):
            yield x

    def print_treap_(self):
        preorder_print(self.root)
        print('')
        inorder_print(self.root)
        print('')

if __name__ == '__main__':

    n = int(input())
    treap = Treap()
    for i in range(n):
        _input = input()
        if _input == 'print':
            
            treap.print_treap_()

        else:
            operate, nums = _input.split(' ', 1)
            if operate == 'insert':
                
                k, p = (int(z) for z in nums.split())
                treap.insert_(k, p)

            elif operate == 'find':
                k = int(nums)
                res = treap.find_(k)
                if res:
                    print("yes")
                else:
                    print("no")

            elif operate == 'delete':
                k = int(nums)
                treap.delete_(k)

Solution (Python):
class Node:
    def __init__(self, key, priority, left=None, right=None):
        self.key = key
        self.priority = priority
        self.left = left
        self.right = right


def rightRotate(t):
    s = t.left
    t.left = s.right
    s.right = t
    return s 


def leftRotate(t):
    s = t.right
    t.right = s.left
    s.left = t
    return s 


def insert(t, key, priority):
    
    if t == None:
        return Node(key, priority)
    
    if key == t.key:
        return t

    if key < t.key: 
        
        t.left = insert(t.left, key, priority)
        
        if t.priority < t.left.priority:
            t = rightRotate(t)
    else: 
        
        t.right = insert(t.right, key, priority)
        
        if t.priority < t.right.priority:
            t = leftRotate(t)

    return t


def erase(t, key):
    if t == None:
        return None

    if key == t.key: 
        if (t.left == None) and (t.right == None): 
            return None
        elif t.left == None: 
            t = leftRotate(t)
        elif t.right == None:
            t = rightRotate(t) 
        else: 
            
            if t.left.priority > t.right.priority:
                t = rightRotate(t)
            else:
                t = leftRotate(t)
        return erase(t, key)

    
    elif key > t.key:
        t.right = erase(t.right, key)
    else:
        t.left = erase(t.left, key)
    return t



def find(t, key):
    if t.key == key:
        print("yes")
    elif t.key < key and t.right != None:
        find(t.right, key)
    elif t.key > key and t.left != None:
        find(t.left, key)
    else:
        print("no")
    return 0


def in_print(t):
    if t.left != None:
        in_print(t.left)
    print(" " + str(t.key), end='')
    if t.right != None:
        in_print(t.right)


def pre_print(t):
    print(" " + str(t.key), end='')
    if t.left != None:
        pre_print(t.left)
    if t.right != None:
        pre_print(t.right)


def main():
    Treap = None
    p = []
    k = int(input())
    i = 0
    while i < k:
        try:
            p.append(list(map(str, input().split())))
            i = i + 1
        except:
            break;
    
    i = 0
    while i < k:
        if p[i][0] == 'insert':
            key = int(p[i][1])
            priority = int(p[i][2])
            Treap = insert(Treap, key, priority)
        elif p[i][0] == 'find':
            key = int(p[i][1])
            find(Treap, key)
        elif p[i][0] == 'delete':
            key = int(p[i][1])
            Treap = erase(Treap, key)
        else:
            in_print(Treap)
            print()
            pre_print(Treap)
            print()
        i = i + 1

if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque
import sys
readline = sys.stdin.readline
write = sys.stdout.write

def rotate(nd, d):
    c = nd[d]
    if d:
        nd[1] = c[0]
        c[0] = nd
    else:
        nd[0] = c[1]
        c[1] = nd
    return c


root = None
def insert(val, pri):
    global root
    st = []
    dr = []
    x = root
    while x:
        st.append(x)
        if x[2] == val:
            return
        d = (x[2] < val)
        dr.append(d)
        x = x[d]
    nd = [None, None, val, pri]
    while st:
        x = st.pop(); d = dr.pop()
        c = x[d] = nd
        if x[3] >= c[3]:
            break
        rotate(x, d)
    else:
        root = nd

def __delete(nd):
    st = []; dr = []
    while nd[0] or nd[1]:
        l = nd[0]; r = nd[1]
        d = (l[3] <= r[3]) if l and r else (l is None)
        st.append(rotate(nd, d))
        dr.append(d ^ 1)
    nd = x = None
    while st:
        nd = x; x = st.pop(); d = dr.pop()
        x[d] = nd
    return x

def delete(val):
    global root
    x = root

    y = None
    while x:
        if val == x[2]:
            break
        y = x; d = (x[2] < val)
        x = x[d]
    else:
        return

    if y:
        y[d] = __delete(x)
    else:
        root = __delete(x)

def find(val):
    x = root
    while x:
        if val == x[2]:
            return 1
        x = x[x[2] < val]
    return 0

def debug():
    s0 = [""]
    s1 = [""]

    def dfs(nd):
        v = str(nd[2])
        s0.append(v)
        if nd[0]:
            dfs(nd[0])
        s1.append(v)
        if nd[1]:
            dfs(nd[1])
    dfs(root)
    return " ".join(s1), " ".join(s0)


M = int(readline())
ans = []
for m in range(M):
    cmd, *V, = readline().split()
    if cmd == "print":
        ans.extend(debug())
    elif cmd == "find":
        ans.append("yes" if find(int(V[0])) else "no")
    elif cmd == "delete":
        delete(int(V[0]))
    else:
        val, pri = map(int, V)
        insert(val, pri)
write("\n".join(ans))
write("\n")

Solution (Python):
def lrotate(t):
    s = t.right
    t.right = s.left
    s.left = t
    return s

def rrotate(t):
    s = t.left
    t.left = s.right
    s.right = t
    return s

def insert(t, key, pri):
    if t == None:
        return Treap(key, pri)
    if key == t.key:
        return t

    if key < t.key:
        t.left = insert(t.left, key, pri)
        if t.pri < t.left.pri:
            t = rrotate(t)
    else:
        t.right = insert(t.right, key, pri)
        if t.pri < t.right.pri:
            t = lrotate(t)
    return t

def delete(t, key):
    if t == None:
        return None

    if key == t.key:
        if t.left == None and t.right == None:
            return None
        elif t.left == None:
            t = lrotate(t)
        elif t.right == None:
            t = rrotate(t)
        else:
            if t.left.pri > t.right.pri:
                t = rrotate(t)
            else:
                t = lrotate(t)
        return delete(t, key)

    if key < t.key:
        t.left = delete(t.left, key)
    else:
        t.right = delete(t.right, key)
    return t

def find(t, key):
    if t == None:
        return -1
    if t.key == key:
        return 1
    if key < t.key:
        return find(t.left, key)
    else:
        return find(t.right, key)
    
def inorder(t):
    if t == None:
        return 
    inorder(t.left)
    print(" " + str(t.key), end="")
    inorder(t.right)

def preorder(t):
    if t == None:
        return
    print(" " + str(t.key), end="")
    preorder(t.left)
    preorder(t.right)



class Treap():
    def __init__(self, key, pri):
        self.key = key
        self.pri = pri
        self.left = None
        self.right = None
        
t = None
data = []
m = int(input())
for i in range(m):
    data.append(list(input().split()))

for i in range(m):
    if data[i][0] == "insert":
        t = insert(t, int(data[i][1]), int(data[i][2]))
    elif data[i][0] == "delete":
        t = delete(t, int(data[i][1]))
    elif data[i][0] == "find":
        result = find(t, int(data[i][1]))
        if result == 1:
            print("yes")
        else:
            print("no")
    else:
        inorder(t)
        print()
        preorder(t)
        print()