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)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <string>
#include <sstream>

typedef struct _node {
	int key;
	int priority;
	struct _node * left;
	struct _node * right;
	struct _node * parent;
} node;

node * left_rotate(node * t) {
	node * new_t = t->right;
	t->right = new_t->left;
	if (t->right != NULL) {
		t->right->parent = t;
	}
	new_t->left = t;
	new_t->parent = t->parent;
	t->parent = new_t;

	if (new_t->parent == NULL) {
		return new_t;
	}

	node * parent = new_t->parent;
	if (parent->left == t) {
		parent->left = new_t;
	}
	else {
		parent->right = new_t;
	}
	return new_t;
}

node * right_rotate(node * t) {
	node * new_t = t->left;
	t->left = new_t->right;
	if (t->left != NULL) {
		t->left->parent = t;
	}
	new_t->right = t;
	new_t->parent = t->parent;
	t->parent = new_t;

	if (new_t->parent == NULL) {
		return new_t;
	}

	node * parent = new_t->parent;
	if (parent->left == t) {
		parent->left = new_t;
	}
	else {
		parent->right = new_t;
	}
	return new_t;
}

node * create_node(int key, int priority) {
	node * p;
	p = (node *)malloc(sizeof(node));
	p->key = key;
	p->priority = priority;
	p->left = NULL;
	p->right = NULL;
	p->parent = NULL;
	return p;
}

node * insert(node * t, int key, int priority) {
	if (t == NULL) {
		node * p = create_node(key, priority);
		return p;
	}

	if (key < t->key) {
		if (t->left == NULL) {
			node * p = create_node(key, priority);
			t->left = p;
			p->parent = t;
		}
		else {
			insert(t->left, key, priority);
		}
				
		if (t->priority < t->left->priority) {
			t = right_rotate(t);
		}
		return t;
	}
	else if (key > t->key) {
		if (t->right == NULL) {
			node * p = create_node(key, priority);
			t->right = p;
			p->parent = t;
		}
		else {
			insert(t->right, key, priority);
		}

		if (t->priority < t->right->priority) {
			t = left_rotate(t);
		}
		return t;
	}
	
	return t;
}

node * delete_node(node * t, int key) {
	if (key == t->key) {
		if (t->left == NULL && t->right == NULL) {
			if (t->parent->left == t) {
				t->parent->left = NULL;
			}
			else {
				t->parent->right = NULL;
			}
			free(t);
			return NULL;
		}

		if (t->left == NULL) {
			t = left_rotate(t);	
		}
		else if (t->right == NULL) {
			t = right_rotate(t);
		}
		else if (t->left->priority > t->right->priority) {
			t = right_rotate(t);
		}
		else {
			t = left_rotate(t);
		}
		t = delete_node(t, key);
		return t;
	}

	if (key < t->key) {
		if (t->left == NULL) {
			return NULL;
		}
		delete_node(t->left, key);
		return t;
	}
	else {
		if (t->right == NULL) {
			return NULL;
		}
		delete_node(t->right, key);
		return t;
	}
}

int find(node * t, int key) {
	if (key == t->key) {
		return 1;
	}
	if (key < t->key) {
		if (t->left == NULL) {
			return 0;
		}
		return find(t->left, key);
	}
	else {
		if (t->right == NULL) {
			return 0;
		}
		return find(t->right, key);
	}
}

void print_preorder(node * t) {
	if (t == NULL) {
		return;
	}
	printf(" %d", t->key);
	if (t->left != NULL) {
		print_preorder(t->left);
	}
	if (t->right != NULL) {
		print_preorder(t->right);
	}
}

void print_inorder(node * t) {
	if (t == NULL) {
		return;
	}
	if (t->left != NULL) {
		print_inorder(t->left);
	}
	printf(" %d", t->key);
	if (t->right != NULL) {
		print_inorder(t->right);
	}
}

int main() {
	int m;
	std::string buf;
	std::string str;
	int k, p;
	node * root = NULL;

	std::getline(std::cin, buf);
	std::stringstream ss0(buf);
	ss0 >> m;

	for (int i = 0; i < m; i++) {
		std::getline(std::cin, buf);
		std::stringstream ss(buf);
		std::getline(ss, str, ' ');
		if (str == "insert") {
			ss >> k >> p;
			//printf("--insert %d %d\n", k, p);
			root = insert(root, k, p);
		}
		else if (str == "find") {
			ss >> k;
			//printf("--find %d\n", k);
			int result = find(root, k);
			if (result == 1) {
				printf("yes\n");
			}
			else {
				printf("no\n");
			}
		}
		else if (str =="delete") {
			ss >> k;
			//printf("--delete %d\n", k);
			root = delete_node(root, k);
		}
		else if (str == "print") {
			//printf("--print\n");
			print_inorder(root);
			printf("\n");
			print_preorder(root);
			printf("\n");
		}
		else {
			printf("error\n");
		}
	}
	
	return 0;
}