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 move
...(truncated)

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
struct point{
	int a,b;
	point *l,*r,*pa;
	point(){l=r=pa=NULL;}
	point(int aa,int bb):a(aa),b(bb){l=r=pa=NULL;};
};
point *root;
point *rightRotate(point *t){
	point *s=t->l;
	t->l=s->r;
	s->r=t;
	return s;
}
point *leftRotate(point *t){
	point *s=t->r;
	t->r=s->l;
	s->l=t;
	return s;
}
point* insert(point *p,int a,int b){
	if(p==NULL)return new point(a,b);
	if(a==p->a)return p;
	if(a<p->a){
		p->l=insert(p->l,a,b);
		if(p->b<p->l->b)
			p=rightRotate(p);
	}else{
		p->r=insert(p->r,a,b);
		if(p->b<p->r->b)
			p=leftRotate(p);
	}return p;
}
point *_del(point *p, int a);
point *del(point *p,int a){
	if(p==NULL)return NULL;
	if(a<p->a)p->l=del(p->l,a);
	else if(a>p->a)p->r=del(p->r,a);
	else return _del(p,a);
	return p;
}
point *_del(point *p, int a){
	if(p->l==NULL&&p->r==NULL)return NULL;
	else if(p->l==NULL)p=leftRotate(p);
	else if(p->r==NULL)p=rightRotate(p);
	else{
		if(p->l->b>p->r->b)p=rightRotate(p);
		else p=leftRotate(p);
	}return del(p,a);
}
bool find(int a){
	point *p=root;
	while(p){
		if(a==p->a)return 1;
		if(a>p->a)p=p->r;
		else p=p->l;
	}
	return 0;
}
void dfs1(point *p){
	if(!p)return;
	dfs1(p->l);
	printf(" %d",p->a);
	dfs1(p->r);
}
void dfs2(point *p){
	if(!p)return;
	printf(" %d",p->a);
	dfs2(p->l);
	dfs2(p->r);
}
int main(){
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	int T;cin>>T;
	int ta,tb;
	string s;
	while(cin>>s){
		if(s=="insert"){
			cin>>ta>>tb;
//			if(!root)root=new point(ta,tb);
//			else 
			root=insert(root,ta,tb);
		}else if(s=="find"){
			cin>>ta;
			if(find(ta))printf("yes\n");
			else printf("no\n");
		}else if(s=="print"){
			dfs1(root);
			printf("\n");
			dfs2(root);
			printf("\n");
		}else if(s=="delete"){
			cin>>ta;
			root=del(root,ta);
		}
	}
	return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 12868
final_rank: 6

code_snippet: #include <bits/stdc++.h>
#define endl '\n'
#define rep(i, n) for(int i = 0; i < n; ++i)
using namespace std;

struct Node {
    int key, priority;
    Node *left, *right;
    Node(int k, int p) : key(k), priority(p), left(nullptr), right(nullptr) {}
};

class Treap {
    public:
    Node* root;
    Treap() : root(nullptr) {}

    Node* rightRotate(Node* root) {
        Node* newRoot = root->left;
        root->left = newRoot->right;
        newRoot->right = root;
        return newRoot;
    }

    Node* leftRotate(Node* root) {
        Node* newRoot = root->right;
        root->right = newRoot->left;
        newRoot->left = root;
        return newRoot;
    }

    Node* insert(Node* root, int key, int priority) {
        if(root == nullptr) return new Node(key, priority);

        if(key == root->key) return root;

        if(key < root->key) {
            root->left = insert(root->left, key, priority);
            if(root->priority < root->left->priority) root = rightRotate(root);
        }   
        else {
            root->right = insert(root->right, key, priority);
            if(root->priority < root->right->priority) root = leftRotate(root);
        }

        return root;
    }

    Node* _deleteNode(Node* root, int key) {
        if(root->left == nullptr && root->right == nullptr) {
            delete root;
            return nullptr;
        }
        else if(root->left == nullptr) {
            root = leftRotate(root);
        }
        else if(root->right == nullptr) {
            root = rightRotate(root);
        }
        else {
            if(root->left->priority > root->right->priority) {
                root = rightRotate(root);
            }
            else {
                root = leftRotate(root);
            }
        }
        return deleteNode(root, key);
    }

    Node* deleteNode(Node* root, int key) {
        if(root == nullptr) return nullptr;

        if(key < root->key) {
            root->left = deleteNode(root->left, key);
        }
        else if(key > root->key) {
            root->right = deleteNode(root->right, key);
        }
        else {
            return _deleteNode(root, key);
        }
        return root;
    }

    bool find(Node* root, int key) {
        if(root == nullptr) return false;
        if(key == root->key) return true;
        if(key < root->key) return find(root->left, key);
        else return find(root->right, key);
    }

    void inOrder(Node* root) {
        if(root == nullptr) return;
        if(root->left != nullptr) inOrder(root->left);
        cout << " " << root->key;
        if(root->right != nullptr) inOrder(root->right);
    }

    void preOrder(Node* root) {
        if(root == nullptr) return;
        cout << " " << root->key;
        if(root->left != nullptr) preOrder(root->left);
        if(root->right != nullptr) preOrder(root->right);
    }
};

int main() {
    int m;
    cin >> m;
    Treap treap;

    rep(i, m) {
        string com;
        cin >> com;
        if(com == "insert") {
            int k, p;
            cin >> k >> p;
            treap.root = treap.insert(treap.root, k, p);
        }
        else if(com == "find") {
            int k;
            cin >> k;
            cout << (treap.find(treap.root, k) ? "yes" : "no") << endl;
        }
        else if(com == "delete") {
            int k;
            cin >> k;
            treap.root = treap.deleteNode(treap.root, k);
        }
        else if(com == "print") {
            treap.inOrder(treap.root);
            cout << endl;
            treap.preOrder(treap.root);
            cout << endl;
        }
    }      
    return 0;
}
accuracy: 1
time_ms: 210
memory_kb: 9748
final_rank: 17

code_snippet: #include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct Node {
    int key;
    int priority;
    struct Node *left, *right;
} Node;

Node *root;

Node* createNode(int key, int priority) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->key = key;
    newNode->priority = priority;
    newNode->left = newNode->right = NULL;
    return newNode;
}

Node* rightRotate(Node *t) {
    Node *s = t->left;
    t->left = s->right;
    s->right = t;
    return s;
}

Node* leftRotate(Node *t) {
    Node *s = t->right;
    t->right = s->left;
    s->left = t;
    return s;
}

Node* insert(Node *t, int key, int priority) {
    if (t == NULL) return createNode(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;
}

Node* deleteNode(Node *t, int key) {
    if (t == NULL) return NULL;
    
    if (key < t->key) {
        t->left = deleteNode(t->left, key);
    } else if (key > t->key) {
        t->right = deleteNode(t->right, key);
    } else { // 削除対象のノードを発見
        if (t->left == NULL && t->right == NULL) {
            free(t);
            return NULL;
        } else if (t->left == NULL) {
            t = leftRotate(t);
        } else if (t->right == NULL) {
            t = rightRotate(t);
        } else {
            if (t->left->priority > t->right->priority) {
                t = rightRotate(t);
            } else {
                t = leftRotate(t);
            }
        }
        // 回転後、削除対象が下に移動したので、そこから再度deleteを呼び出す
        return deleteNode(t, key);
    }
    return t;
}

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

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

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

int main() {
    int m, k, p;
    char command[10];
    root = NULL;

    scanf("%d", &m);
    for (int i = 0; i < m; i++) {
        scanf("%s", command);
        if (strcmp(command, "insert") == 0) {
            scanf("%d %d", &k, &p);
            root = insert(root, k, p);
        } else if (strcmp(command, "find") == 0) {
            scanf("%d", &k);
            if (find(root, k)) {
                printf("yes\n");
            } else {
                printf("no\n");
            }
        } else if (strcmp(command, "delete") == 0) {
            scanf("%d", &k);
            root = deleteNode(root, k);
        } else if (strcmp(command, "print") == 0) {
            inorder(root);
            printf("\n");
            preorder(root);
            printf("\n");
        }
    }

    return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 9056
final_rank: 3

code_snippet: #include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;

// ノード構造体の定義
struct Node {
    int key;       // キー
    int priority;  // 優先度
    Node* left;    // 左子
    Node* right;   // 右子
    Node(int k, int p) : key(k), priority(p), left(nullptr), right(nullptr) {}
};

// 右回転
Node* rightRotate(Node* t) {
    Node* s = t->left;
    t->left = s->right;
    s->right = t;
    return s;
}

// 左回転
Node* leftRotate(Node* t) {
    Node* s = t->right;
    t->right = s->left;
    s->left = t;
    return s;
}

// 挿入操作
Node* insert(Node* t, int key, int priority) {
    if (!t) return new Node(key, priority);  // 新しいノードを挿入

    if (key < t->key) {
        t->left = insert(t->left, key, priority);
        if (t->left->priority > t->priority) {
            t = rightRotate(t);  // ヒープ条件を満たすまで右回転
        }
    } else {
        t->right = insert(t->right, key, priority);
        if (t->right->priority > t->priority) {
            t = leftRotate(t);  // ヒープ条件を満たすまで左回転
        }
    }
    return t;
}

// 削除操作
Node* deleteNode(Node* t, int key) {
    if (!t) return nullptr;  // ノードがない場合

    if (key < t->key) {
        t->left = deleteNode(t->left, key);
    } else if (key > t->key) {
        t->right = deleteNode(t->right, key);
    } else {
        // 削除対象のノードを発見
        if (!t->left || !t->right) {
            Node* tmp = t->left ? t->left : t->right;
            delete t;
            return tmp;
        } else if (t->left->priority > t->right->priority) {
            t = rightRotate(t);
            t->right = deleteNode(t->right, key);
        } else {
            t = leftRotate(t);
            t->left = deleteNode(t->left, key);
        }
    }
    return t;
}

// 探索操作
bool find(Node* t, int key) {
    if (!t) return false;
    if (key == t->key) return true;
    if (key < t->key) return find(t->left, key);
    return find(t->right, key);
}

// 中間順巡回 (inorder)
void inorder(Node* t) {
    if (!t) return;
    inorder(t->left);
    cout << " " << t->key ;
    inorder(t->right);
}
// 先行順巡回 (preorder)
void preorder(Node* t) {
    if (!t) return;
    cout << " " << t->key ;
    preorder(t->left);
    preorder(t->right);
}

int main() {
    int m;
    cin >> m;  // 操作数を入力
    Node* treap = nullptr;  // 空のTreapを初期化

    while (m--) {
        string command;
        cin >> command;  // コマンドを入力

        if (command == "insert") {
            int key, priority;
            cin >> key >> priority;
            treap = insert(treap, key, priority);  // 挿入
        } else if (command == "find") {
            int key;
            cin >> key;
            cout << (find(treap, key) ? "yes" : "no") << endl;  // 探索
        } else if (command == "delete") {
            int key;
            cin >> key;
            treap = deleteNode(treap, key);  // 削除
        } else if (command == "print") {
            inorder(treap);  // 中間順巡回
            cout << endl;
            preorder(treap);  // 先行順巡回
            cout << endl;
        }
    }
    return 0;
}
accuracy: 1
time_ms: 190
memory_kb: 9596
final_rank: 8

code_snippet: #include <iostream>
#include <string>
using namespace std;

struct Treap {
    int key, priority;
    Treap *left, *right;
    Treap(int k, int p) : key(k), priority(p), left(nullptr), right(nullptr) {}
};

Treap* root = nullptr;

Treap* rightRotate(Treap* t) {
    Treap* s = t->left;
    t->left = s->right;
    s->right = t;
    return s;
}

Treap* leftRotate(Treap* t) {
    Treap* s = t->right;
    t->right = s->left;
    s->left = t;
    return s;
}


Treap* insert(Treap* t, int key, int priority) {
    if (!t) return new Treap(key, priority);
    if (key == t->key) return t;

    if (key < t->key) {
        t->left = insert(t->left, key, priority);
        if (t->left->priority > t->priority)
            t = rightRotate(t);
    } else {
        t->right = insert(t->right, key, priority);
        if (t->right->priority > t->priority)
            t = leftRotate(t);
    }
    return t;
}

bool find(Treap* t, int key) {
    if (!t) return false;
    if (key == t->key) return true;
    if (key < t->key) return find(t->left, key);
    return find(t->right, key);
}

Treap* deleteTreap(Treap* t, int key) {
    if (!t) return nullptr;
    if (key < t->key) {
        t->left = deleteTreap(t->left, key);
    } else if (key > t->key) {
        t->right = deleteTreap(t->right, key);
    } else {
        if (!t->left && !t->right) {
            delete t;
            return nullptr;
        } else if (!t->left) {
            t = leftRotate(t);
            t->left = deleteTreap(t->left, key);
        } else if (!t->right) {
            t = rightRotate(t);
            t->right = deleteTreap(t->right, key);
        } else {
            if (t->left->priority > t->right->priority) {
                t = rightRotate(t);
                t->right = deleteTreap(t->right, key);
            } else {
                t = leftRotate(t);
                t->left = deleteTreap(t->left, key);
            }
        }
    }
    return t;
}
void inorder(Treap* t) {
    if (!t) return;
    inorder(t->left);
    cout << " " << t->key;
    inorder(t->right);
}

void preorder(Treap* t) {
    if (!t) return;
    cout << " " << t->key;
    preorder(t->left);
    preorder(t->right);
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int m;
    cin >> m;
    string cmd;
    int k, p;

    for (int i = 0; i < m; ++i) {
        cin >> cmd;
        if (cmd == "insert") {
            cin >> k >> p;
            root = insert(root, k, p);
        } else if (cmd == "find") {
            cin >> k;
            cout << (find(root, k) ? "yes" : "no") << '\n';
        } else if (cmd == "delete") {
            cin >> k;
            root = deleteTreap(root, k);
        } else if (cmd == "print") {
            inorder(root); cout << endl;
            preorder(root); cout << endl;
        }
    }
    return 0;
}
accuracy: 1
time_ms: 110
memory_kb: 9648
final_rank: 2

code_snippet: #include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
int cnt,rt,n,m;
char op[10];
struct node{int ls,rs,val=0,rnd=0,siz=0;}tr[N];
void newnode(int &x,int val,int rnd){tr[x=++cnt]={0,0,val,rnd,1},(!rt?rt=x:0);}
void push_up(int x){tr[x].siz=x?tr[tr[x].ls].siz+tr[tr[x].rs].siz+1:0;}
void split(int x,int val,int &u,int &v){
    if(!x) return u=v=0,void();
    if(tr[x].val<=val) u=x,split(tr[x].rs,val,tr[u].rs,v),push_up(x);
    else v=x,split(tr[x].ls,val,u,tr[v].ls),push_up(v);
}
int merge(int x,int y){
    if(!x||!y) return x|y;
    if(tr[x].rnd>tr[y].rnd) return tr[x].rs=merge(tr[x].rs,y),push_up(x),x;
    else return tr[y].ls=merge(x,tr[y].ls),push_up(y),y;
}
void insert(int val,int rnd){
    int x,y,z;
    split(rt,val,x,y),newnode(z,val,rnd),rt=merge(merge(x,z),y);
}
void del(int val){
    int x,y,z;
    split(rt,val,x,z),split(x,val-1,x,y),
    y=merge(tr[y].ls,tr[y].rs),rt=merge(merge(x,y),z);
}
int get_rank(int val){
    int x,y,ans;
    split(rt,val-1,x,y),ans=tr[x].siz+1,rt=merge(x,y);
    return ans;
}
int get_val(int x,int k){
    int lsiz=tr[tr[x].ls].siz;
    return k==lsiz+1?tr[x].val:(k<=lsiz?get_val(tr[x].ls,k):get_val(tr[x].rs,k-(lsiz+1)));
}
int get_pre(int val){
    int x,y,ans;
    split(rt,val-1,x,y),ans=get_val(x,tr[x].siz),rt=merge(x,y);
    return ans;
}
int get_nxt(int val){
    int x,y,ans;
    split(rt,val,x,y),ans=get_val(y,1),rt=merge(x,y);
    return ans;
}
void Preorder(int x){
    if(tr[x].val!=1e9&&tr[x].val!=-1e9) printf(" %d",tr[x].val);
    if(tr[x].ls) Preorder(tr[x].ls);
    if(tr[x].rs) Preorder(tr[x].rs);
}
void Mid_order(int x){
    if(tr[x].ls) Mid_order(tr[x].ls);
    if(tr[x].val!=1e9&&tr[x].val!=-1e9) printf(" %d",tr[x].val);
    if(tr[x].rs) Mid_order(tr[x].rs);
}
int main(){
    scanf("%d",&m),insert(1e9,0),insert(-1e9,0);
    for(int k,p,i=1;i<=m;i++){
        scanf(" %s",op+1);
        if(op[1]=='i') scanf("%d%d",&k,&p),insert(k,p);
        if(op[1]=='f')
            scanf("%d",&k),
            printf("%s\n",get_pre(k+1)==k?"yes":"no");
        if(op[1]=='d') scanf("%d",&k),del(k);
        if(op[1]=='p')
            Mid_order(rt),printf("\n"),Preorder(rt),printf("\n");
    }
    return 0;
}
accuracy: 1
time_ms: 140
memory_kb: 7476
final_rank: 4

code_snippet: #include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
int cnt,rt;
int n,m;
char op[10];
struct node{int ls,rs,val=0,rnd=0,siz=0;}tr[N];
void newnode(int &x,int val,int rnd){tr[x=++cnt]={0,0,val,rnd,1},(!rt?rt=x:0);}
void push_up(int x){tr[x].siz=x?tr[tr[x].ls].siz+tr[tr[x].rs].siz+1:0;}
void split(int x,int val,int &u,int &v){
    if(!x) return u=v=0,void();
    if(tr[x].val<=val) u=x,split(tr[x].rs,val,tr[u].rs,v),push_up(x);
    else v=x,split(tr[x].ls,val,u,tr[v].ls),push_up(v);
}
int merge(int x,int y){
    if(!x||!y) return x|y;
    if(tr[x].rnd>tr[y].rnd) return tr[x].rs=merge(tr[x].rs,y),push_up(x),x;
    else return tr[y].ls=merge(x,tr[y].ls),push_up(y),y;
}
void insert(int val,int rnd){
    int x,y,z;
    split(rt,val,x,y),newnode(z,val,rnd),rt=merge(merge(x,z),y);
}
void del(int val){
    int x,y,z;
    split(rt,val,x,z),split(x,val-1,x,y),
    y=merge(tr[y].ls,tr[y].rs),rt=merge(merge(x,y),z);
}
int get_rank(int val){
    int x,y,ans;
    split(rt,val-1,x,y),ans=tr[x].siz+1,rt=merge(x,y);
    return ans;
}
int get_val(int x,int k){
    int lsiz=tr[tr[x].ls].siz;
    return k==lsiz+1?tr[x].val:(k<=lsiz?get_val(tr[x].ls,k):get_val(tr[x].rs,k-(lsiz+1)));
}
int get_pre(int val){
    int x,y,ans;
    split(rt,val-1,x,y),ans=get_val(x,tr[x].siz),rt=merge(x,y);
    return ans;
}
int get_nxt(int val){
    int x,y,ans;
    split(rt,val,x,y),ans=get_val(y,1),rt=merge(x,y);
    return ans;
}
void Preorder(int x){
    printf(" %d",tr[x].val);
    if(tr[x].ls) Preorder(tr[x].ls);
    if(tr[x].rs) Preorder(tr[x].rs);
}
void Mid_order(int x){
    if(tr[x].ls) Mid_order(tr[x].ls);
    printf(" %d",tr[x].val);
    if(tr[x].rs) Mid_order(tr[x].rs);
}
int main(){
    scanf("%d",&m);
    for(int k,p,i=1;i<=m;i++){
        scanf(" %s",op+1);
        if(op[1]=='i') scanf("%d%d",&k,&p),insert(k,p);
        if(op[1]=='f')
            scanf("%d",&k),
            printf("%s\n",get_val(rt,get_rank(k))==k?"yes":"no");
        if(op[1]=='d') scanf("%d",&k),del(k);
        if(op[1]=='p')
            Mid_order(rt),printf("\n"),Preorder(rt),printf("\n");
    }
    return 0;
}
accuracy: 1
time_ms: 140
memory_kb: 7476
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define int long long
const int INF = (1 << 30);

struct Node
{
    int key, priority;
    Node *left;
    Node *right;
    Node(int _k, int _p) : key(_k), priority(_p), left(nullptr), right(nullptr) {}
};

struct treap
{
    int siz;
    Node *n;
    const int MAXI;
    treap(int _size, int _max) : siz(_size), MAXI(_max), n(nullptr) {};

    Node *rightRotate(Node *t)
    {
        Node *s = t->left;
        t->left = s->right;
        s->right = t;
        return s;
    }

    Node *leftRotate(Node *t)
    {
        Node *s = t->right;
        t->right = s->left;
        s->left = t;
        return s;
    }

    Node *insert(Node *t, int key, int priority)
    {
        if (t == nullptr)
        {
            return new 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;
    }
    Node *_delete(Node *t, int key)
    {
        if (t == nullptr)
        {
            return t;
        }
        if (key < t->key)
        {
            t->left = _delete(t->left, key);
        }
        else if (key > t->key)
        {
            t->right = _delete(t->right, key);
        }
        else
        {
            return _delete_(t, key);
        }
        return t;
    }
    Node *_delete_(Node *t, int key)
    {
        if (t->left == nullptr and t->right == nullptr)
        {
            return nullptr;
        }
        else if (t->left == nullptr)
        {
            t = leftRotate(t);
        }
        else if (t->right == nullptr)
        {
            t = rightRotate(t);
        }
        else
        {
            if (t->left->priority > t->right->priority)
            {
                t = rightRotate(t);
            }
            else
            {
                t = leftRotate(t);
            }
        }
        return _delete(t, key);
    }
    bool find(int key, Node *t)
    {
        if (t == nullptr)
        {
            return false;
        }
        else if (key == t->key)
        {
            return true;
        }
        else if (key < t->key)
        {
            return find(key, t->left);
        }
        else
        {
            return find(key, t->right);
        }
    }
    void order(Node *t, bool inorder = true)
    {
        if (t == nullptr)
        {
            return;
        }
        if (!inorder)
        {
            cout << " " << t->key;
        }
        order(t->left, inorder);
        if (inorder)
        {
            cout << " " << t->key;
        }
        order(t->right, inorder);
    }
    void print()
    {
        order(n);
        cout << endl;
        order(n, false);
        cout << endl;

    }
};

signed main()
{
    int m;
    cin >> m;
    treap t(200005, 2000000000);
    for (int i = 0; i < m; i++)
    {
        string s;
        cin >> s;
        if (s == "insert")
        {
            int k, p;
            cin >> k >> p;
            t.n = t.insert(t.n, k, p);
        }
        else if (s == "find")
        {
            int k;
            cin >> k;
            if (t.find(k, t.n)) {
                cout << "yes" << endl;
            }
            else {
                cout << "no" << endl;
            }
        }
        else if (s == "delete")
        {
            int k;
            cin >> k;
            t.n = t._delete(t.n, k);
        }
        else
        {
            t.print();
        }
    }
}
accuracy: 1
time_ms: 190
memory_kb: 12760
final_rank: 18

code_snippet: #include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

struct Node{
  int key;
  int pri;
  Node* left;
  Node* right;
  Node(int k, int p) : key(k), pri(p), left(nullptr), right(nullptr) {}
};

Node* rightRotate(Node* &t){
  Node* s = t->left;
  t->left = s->right;
  s->right = t;
  return s;
}

Node* leftRotate(Node* &t){
  Node* s = t->right;
  t->right = s->left;
  s->left = t;
  return s;
}

Node* insert(Node* &t, int key, int pri) {
  if (t == nullptr) {
    t = new Node(key, pri);
  } else if (key < t->key) {
    insert(t->left, key, pri);
    if (t->left->pri > t->pri) t = rightRotate(t);
  } else {
    insert(t->right, key, pri);
    if (t->right->pri > t->pri) t = leftRotate(t);
  }
  return t;
}


Node* erase(Node* t, int key){
  if (t == nullptr) return NULL;
  if(key == t->key){
    if(t->left == NULL && t->right == NULL){
      return NULL;
    }else if(t->left == NULL){
      t = leftRotate(t);
    }else if(t->right == NULL){
      t = rightRotate(t);
    }else{
      if(t->left->pri > t->right->pri){
	t = rightRotate(t);
      }else{
	t = leftRotate(t);
      }
    }
    return erase(t, key);
  }
  if(key < t->key){
    t->left = erase(t->left, key);
  }else{
    t->right = erase(t->right, key);
  }
  return t;
}


bool find(Node* t, int key) {
  if (t == nullptr) return false;
  if (key == t->key) return true;
  if (key < t->key) return find(t->left, key);
  return find(t->right, key);
}


void printInorder(Node* t) {
  if (!t) return;
  printInorder(t->left);
  std::cout << " " << t->key;
  printInorder(t->right);
}

void printPreorder(Node* t) {
  if (!t) return;
  std::cout << " " << t->key;
  printPreorder(t->left);
  printPreorder(t->right);
}

int main() {
  int m, k, p;
  std::string operate;
  std::cin >> m;

  Node* root = nullptr;
  std::srand(std::time(nullptr));

  for (int i = 0; i < m; ++i) {
    std::cin >> operate;
    if (operate == "insert") {
      std::cin >> k >> p;
      root = insert(root, k, p);
    } else if (operate == "find") {
      std::cin >> k;
      std::cout << (find(root, k) ? "yes" : "no") << std::endl;
    } else if (operate == "delete") {
      std::cin >> k;
      root = erase(root, k);
    } else if (operate == "print") {
        printInorder(root);
	std::cout << std::endl;
	printPreorder(root);
	std::cout << std::endl;
    }
  }

  return 0;
}
accuracy: 1
time_ms: 190
memory_kb: 9596
final_rank: 8

code_snippet: #include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

struct Node{
  int key;
  int pri;
  Node* left;
  Node* right;
  Node(int k, int p) : key(k), pri(p), left(nullptr), right(nullptr) {}
};

Node* rightRotate(Node* &t){
  Node* s = t->left;
  t->left = s->right;
  s->right = t;
  return s;
}

Node* leftRotate(Node* &t){
  Node* s = t->right;
  t->right = s->left;
  s->left = t;
  return s;
}

Node* insert(Node* &t, int key, int pri) {
  if (t == nullptr) {
    t = new Node(key, pri);
  } else if (key < t->key) {
    insert(t->left, key, pri);
    if (t->left->pri > t->pri) t = rightRotate(t);
  } else {
    insert(t->right, key, pri);
    if (t->right->pri > t->pri) t = leftRotate(t);
  }
  return t;
}

void erase(Node* &t, int key) {
  if (t == nullptr) return;
  if (key < t->key) {
    erase(t->left, key);
  } else if (key > t->key) {
    erase(t->right, key);
  } else {
    if (!t->left && !t->right) {
      delete t;
      t = nullptr;
    } else if (!t->left) {
      t = leftRotate(t);
      erase(t->left, key);
    } else if (!t->right) {
      t = rightRotate(t);
      erase(t->right, key);
    } else {
      if (t->left->pri > t->right->pri) {
	t = rightRotate(t);
	erase(t->right, key);
      } else {
	t = leftRotate(t);
	erase(t->left, key);
      }
    }
  }
}

bool find(Node* t, int key) {
  if (t == nullptr) return false;
  if (key == t->key) return true;
  if (key < t->key) return find(t->left, key);
  return find(t->right, key);
}


void printInorder(Node* t) {
  if (!t) return;
  printInorder(t->left);
  std::cout << " " << t->key;
  printInorder(t->right);
}

void printPreorder(Node* t) {
  if (!t) return;
  std::cout << " " << t->key;
  printPreorder(t->left);
  printPreorder(t->right);
}

int main() {
  int m, k, p;
  std::string operate;
  std::cin >> m;
  
  Node* root = nullptr;
  std::srand(std::time(nullptr));
  
  for (int i = 0; i < m; ++i) {
    std::cin >> operate;
    if (operate == "insert") {
      std::cin >> k >> p;
      root = insert(root, k, p);
    } else if (operate == "find") {
      std::cin >> k;
      std::cout << (find(root, k) ? "yes" : "no") << std::endl;
    } else if (operate == "delete") {
      std::cin >> k;
      erase(root, k);
    } else if (operate == "print") {
        printInorder(root);
	std::cout << std::endl;
	printPreorder(root);
	std::cout << std::endl;
    }
  }
  
  return 0;
}
accuracy: 1
time_ms: 190
memory_kb: 9608
final_rank: 10

code_snippet: #include <iostream>
#include <iomanip>
#include <cassert>
#include <vector>
#include <algorithm>
#include <utility>
#include <numeric>
#include <tuple>
#include <ranges>
namespace ranges = std::ranges;
namespace views = std::views;
// #include "Src/Number/IntegerDivision.hpp"
// #include "Src/Utility/BinarySearch.hpp"
// #include "Src/Sequence/CompressedSequence.hpp"
// #include "Src/Sequence/RunLengthEncoding.hpp"
// #include "Src/Algebra/Group/AdditiveGroup.hpp"
// #include "Src/DataStructure/FenwickTree/FenwickTree.hpp"
// #include "Src/DataStructure/SegmentTree/SegmentTree.hpp"
// #include "Src/DataStructure/DisjointSetUnion/DisjointSetUnion.hpp"
// using namespace zawa;
// #include "atcoder/modint"
// using mint = atcoder::modint998244353;

struct Node {
    int val = -1, pri = -1, par = -1, lch = -1, rch = -1;
};
std::vector<Node> BST;
int& val(int n) {
    return BST.at(n).val;
}
int& par(int n) {
    return BST.at(n).par;
}
int& pri(int n) {
    return BST.at(n).pri;
}
int& lch(int n) {
    return BST.at(n).lch;
}
int& rch(int n) {
    return BST.at(n).rch;
}

int right_rotate(int t) {
    int s = lch(t);
    lch(t) = rch(s);
    rch(s) = t;
    return s;
}

int left_rotate(int t) {
    int s = rch(t);
    rch(t) = lch(s);
    lch(s) = t;
    return s;
}

int make_node(int va = -1, int pr = -1, int p = -1) {
    int res = std::ssize(BST);
    BST.push_back({va, pr, p, -1, -1});
    return res;
}

int root = -1;

void insert(int va, int pr) {
    auto internal_insert = [&](auto rec, int t, int par) -> int {
        if (t == -1) return make_node(va, pr, par);
        if (val(t) == va) return t;
        if (va < val(t)) {
            lch(t) = rec(rec, lch(t), t);
            if (pri(t) < pri(lch(t))) {
                t = right_rotate(t);
            }
        }
        else {
            rch(t) = rec(rec, rch(t), t);
            if (pri(t) < pri(rch(t))) {
                t = left_rotate(t);
            }
        }
        return t;
    };
    root = internal_insert(internal_insert, root, -1);
}

int find(int v) {
    int node = root;
    while (node != -1) {
        if (v < val(node)) node = lch(node);
        else if (v == val(node)) return node;
        else node = rch(node);
    }
    return node;
}

bool contains(int v) {
    return find(v) != -1;
}

int erase(int, int);
int erase_(int, int);

int erase(int t, int va) {
    // std::cout << t << ' ' << val(t) << ' ' << va << std::endl;
    if (t == -1) return -1;
    if (va < val(t)) lch(t) = erase(lch(t), va);
    else if (va > val(t)) rch(t) = erase(rch(t), va);
    else return erase_(t, va);
    return t;
}

int erase_(int t, int va) {
    if (lch(t) == -1 and rch(t) == -1) return -1;
    else if (lch(t) == -1) t = left_rotate(t);
    else if (rch(t) == -1) t = right_rotate(t);
    else {
        if (pri(lch(t)) > pri(rch(t))) {
            t = right_rotate(t);
        }
        else {
            t = left_rotate(t);
        }
    }
    return erase(t, va);
}

void inorder_tree_walk(int n) {
    if (n == -1) return;
    std::cout << ' ' << val(n);
    inorder_tree_walk(lch(n));
    inorder_tree_walk(rch(n));
}

void preorder_tree_walk(int n) {
    if (n == -1) return;
    preorder_tree_walk(lch(n));
    std::cout << ' ' << val(n);
    preorder_tree_walk(rch(n));
}

int main() {
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);
    std::ios::sync_with_stdio(false);
    int Q;
    std::cin >> Q;
    while (Q--) {
        std::string s;
        std::cin >> s;
        if (s[0] == 'i') {
            int v, p;
            std::cin >> v >> p;
            // std::cout << "insert " << v << ' ' << p << std::endl;
            insert(v, p);
        }
        else if (s[0] == 'p') {
            preorder_tree_walk(root);
            std::cout << '\n';
            inorder_tree_walk(root);
            std::cout << '\n';
            // std::cout.flush();
        }
        else if (s[0] == 'f') {
            int v;
            std::cin >> v;
            std::cout << (contains(v) ? "yes\n" : "no\n");
            // std::cout.flush();
        }
        else if (s[0] == 'd') {
            int v;
            std::cin >> v;
            // std::cout << "delete " << v << std::endl;
            root = erase(root, v);
            std::cout.flush();
        }
        else assert(false);
    }
}
accuracy: 1
time_ms: 100
memory_kb: 9708
final_rank: 1

code_snippet: #include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
#define loop(a,b) for (int a = 0;a < b;a++)
#define rep(a,b,c) for(int a = b;a < c;a++)
#define vec(a) vector<a>
//SEGSIVの原因がつかめなかったので、AIに修正してもらったコードを再実装しています。修正箇所をコメントで記載しています。

// === 修正箇所1: Node構造体の定義は同じ ===
struct Node{
    int key,priority;
    Node *left,*right;
    Node(int key,int priority = rand()%1000000):key(key),priority(priority),left(nullptr),right(nullptr){};
   
};

class Treap {
private:
    Node* root;
    
    // === 修正箇所2: 回転操作は同じ ===
    Node* rightRotate(Node* y) {
        Node* x = y->left;
        y->left = x->right;
        x->right = y;
        return x;
    }
    
    Node* leftRotate(Node* x) {
        Node* y = x->right;
        x->right = y->left;
        y->left = x;
        return y;
    }

    // === 修正箇所3: insert操作は同じ ===
    Node* insert(Node* t, int key,int pri) {
        if (t == nullptr) return new Node(key,pri);
        
        if (key <= t->key) {
            t->left = insert(t->left, key,pri);
            if (t->left->priority > t->priority)
                t = rightRotate(t);
        } else {
            t->right = insert(t->right, key,pri);
            if (t->right->priority > t->priority)
                t = leftRotate(t);
        }
        return t;
    }
    
    // === 修正箇所4: remove操作 - 元のコードにコメントを追加 ===
    Node* remove(Node* t, int key) {
        if (!t) return nullptr;
        
        if (key < t->key) {
            t->left = remove(t->left, key);
        } else if (key > t->key) {
            t->right = remove(t->right, key);
        } else {
            // キーが見つかった場合の削除処理
            
            // 葉ノードの場合
            if (!t->left && !t->right) {
                delete t;
                return nullptr;
            }
            // 左子のみ存在する場合
            else if (!t->right) {
                Node* temp = t->left;
                delete t;
                return temp;
            }
            // 右子のみ存在する場合
            else if (!t->left) {
                Node* temp = t->right;
                delete t;
                return temp;
            }
            // 両方の子が存在する場合
            else {
                if (t->left->priority > t->right->priority) {
                    t = rightRotate(t);
                    t->right = remove(t->right, key);
                } else {
                    t = leftRotate(t);
                    t->left = remove(t->left, key);
                }
            }
        }
        return t;
    }
    
    // === 修正箇所5: search操作は同じ ===
    bool search(Node* t, int key) {
        if (!t) return false;
        if (t->key == key) return true;
        if (key < t->key) return search(t->left, key);
        return search(t->right, key);
    }
    
    // === 修正箇所6: inorder操作 - 元のコードにコメントを追加 ===
    void inorder(Node* t) {
        if (t) { // 引数tをチェック
            inorder(t->left);
            cout << " " << t->key;
            inorder(t->right);
        }
    }
    
    // === 修正箇所7: preorder操作 - 元のコードにコメントを追加 === 
    void preorder(Node* t) {
        if (t) {  // 引数tをチェック
            cout << " " << t->key;
            preorder(t->left);
            preorder(t->right);
        }
    }
    
public:
    Treap() : root(nullptr) {}
    
    void insert(int key,int pri) { root = insert(root, key,pri); }
    void remove(int key) { root = remove(root, key); }
    bool search(int key) { return search(root, key); }
    void print() {
        inorder(root);
        cout << endl;
        preorder(root);
        cout << endl;
    }
};


int main(){
    int m;
    cin >> m;
    Treap treap;
    loop(i,m){
        string com;
        cin >> com;
        if(com == "insert") {
            int key, pri;
            cin >> key >> pri;
            treap.insert(key,pri);
        }
        else if(com == "find") {
            int key;
            cin >> key;
            bool result = treap.search(key);
            cout << (result ? "yes" : "no") << endl;
        }
        else if(com == "delete") {
            int key;
            cin >> key;
            treap.remove(key);
        }
        else if(com == "print") {
            treap.print();
        }
    }
}
accuracy: 1
time_ms: 190
memory_kb: 9628
final_rank: 11

code_snippet: // https://onlinejudge.u-aizu.ac.jp/beta/ice/?problemId=ALDS1_8_D
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; (i) < (n); ++(i))

typedef struct Node
{
    int key;
    int priority;
    Node *left;
    Node *right;

    Node(int k, int pri)
    {
        key = k;
        priority = pri;
        left = NULL;
        right = NULL;
    }
};

Node *t = NULL;

Node *erase(Node *, int);
Node *_delete(Node *, int);

Node *rightRotate(Node *t)
{
    Node *s = t->left;
    t->left = s->right;
    s->right = t;
    return s;
}

Node *leftRotate(Node *t)
{
    Node *s = t->right;
    t->right = s->left;
    s->left = t;
    return s;
}

Node *insert(Node *t, int key, int priority)
{
    if (t == NULL)
    {
        return new 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;
}

Node *erase(Node *t, int k)
{
    if (t == NULL)
    {
        return NULL;
    }

    if (k < t->key)
    {
        t->left = erase(t->left, k);
    }
    else if (k > t->key)
    {
        t->right = erase(t->right, k);
    }
    else
    {
        return _delete(t, k);
    }
    return t;
    // if (k == t->key)
    // {
    //     if (t->left == NULL && t->right == NULL)
    //     {
    //         return NULL;
    //     }
    //     else if (t->left == NULL)
    //     {
    //         t = leftRotate(t);
    //     }
    //     else if (t->right == NULL)
    //     {
    //         t = rightRotate(t);
    //     }
    //     else
    //     {
    //         if (t->left->priority > t->right->priority)
    //         {
    //             t = rightRotate(t);
    //         }
    //         else
    //         {
    //             t = leftRotate(t);
    //         }
    //         return erase(t, k);
    //     }
    // }
    // if (k < t->key)
    // {
    //     t->left = erase(t->left, k);
    // }
    // else
    // {
    //     t->right = erase(t->right, k);
    // }
    // return t;
}

Node *_delete(Node *t, int k)
{
    if (t->left == NULL && t->right == NULL)
    {
        return NULL;
    }
    else if (t->left == NULL)
    {
        t = leftRotate(t);
    }
    else if (t->right == NULL)
    {
        t = rightRotate(t);
    }
    else
    {
        if ((t->left->priority) > (t->right->priority))
        {
            t = rightRotate(t);
        }
        else
        {
            t = leftRotate(t);
        }
    }
    return erase(t, k);
}

Node *find(Node *x, int key)
{
    while (x != NULL && key != x->key)
    {
        if (key < x->key)
        {
            x = x->left;
        }
        else
        {
            x = x->right;
        }
    }
    return x;
}

void inorder(Node *x)
{
    if (x == NULL)
    {
        return;
    }
    inorder(x->left);
    cout << " " << x->key;
    inorder(x->right);
}

void preorder(Node *x)
{
    if (x == NULL)
    {
        return;
    }
    cout << " " << x->key;
    preorder(x->left);
    preorder(x->right);
}

int main()
{
    int order_num;

    cin >> order_num;

    rep(i, order_num)
    {
        string order;
        int k, p;
        cin >> order;
        if (order == "insert")
        {
            cin >> k >> p;
            t = insert(t, k, p);
        }
        else if (order == "delete")
        {
            cin >> k;
            t = erase(t, k);
        }
        else if (order == "find")
        {
            cin >> k;
            Node *result = find(t, k);
            if (result != NULL)
            {
                cout << "yes" << endl;
            }
            else
            {
                cout << "no" << endl;
            }
        }
        else if (order == "print")
        {
            inorder(t);
            cout << endl;
            preorder(t);

            cout << endl;
        }
    }
}
accuracy: 1
time_ms: 190
memory_kb: 9692
final_rank: 12

code_snippet: // ALDS1_8_D
#include <bits/stdc++.h>
#define endl '\n'
#define ll long long
using namespace std;

struct Node {
    int key, priority;
    Node *left, *right;
    Node(int k, int p) : key(k), priority(p), left(nullptr), right(nullptr) {}
};

Node* deleteNode(Node* root, int key);
Node* _deleteNode(Node* root, int key);


Node* rightRotate(Node* root) {
    Node* newRoot = root->left;
    root->left = newRoot->right;
    newRoot->right = root;
    return newRoot;
}

Node* leftRotate(Node* root) {
    Node* newRoot = root->right;
    root->right = newRoot->left;
    newRoot->left = root;
    return newRoot;
}

Node* insert(Node* root, int key, int priority) {
    if(root == nullptr) return new Node(key, priority);

    if(key == root->key) return root;

    if(key < root->key) {
        root->left = insert(root->left, key, priority);
        if(root->priority < root->left->priority) root = rightRotate(root);
    }   
    else {
        root->right = insert(root->right, key, priority);
        if(root->priority < root->right->priority) root = leftRotate(root);
    }

    return root;
}

Node* deleteNode(Node* root, int key) {
    if(root == nullptr) return nullptr;
    // 削除対象を検索
    if(key < root->key) {
        root->left = deleteNode(root->left, key);
    }
    else if(key > root->key) {
        root->right = deleteNode(root->right, key);
    }
    else {
        return _deleteNode(root, key);
    }
    return root;
}

Node* _deleteNode(Node* root, int key) {
    if(root->left == nullptr && root->right == nullptr) {
        return nullptr;
    }
    else if(root->left == nullptr) {
        root = leftRotate(root);
    } 
    else if(root->right == nullptr) {
        root = rightRotate(root);
    }
    else {
        if(root->left->priority > root->right->priority) {
            root = rightRotate(root);
        }
        else {
            root = leftRotate(root);
        }
    }
    return deleteNode(root, key);
}

bool find(Node* root, int key) {
    if(root == nullptr) return false;
    if(key == root->key) return true;
    if(key < root->key) return find(root->left, key);
    return find(root->right, key);
}

void inOrder(Node* root) {
    if(root == nullptr) return;
    if(root->left != nullptr) inOrder(root->left);
    cout << " " << root->key;
    if(root->right != nullptr) inOrder(root->right);
}

void preOrder(Node* root) {
    if(root == nullptr) return;
    cout << " " << root->key;
    if(root->left != nullptr) preOrder(root->left);
    if(root->right != nullptr) preOrder(root->right);
}


int main() {
    int m;
    cin >> m;

    Node* root = nullptr;
    for(int i=0; i<m; ++i) {
        string com;
        cin >> com;
        if(com == "insert") {
            int k, p;
            cin >> k >> p;
            root = insert(root, k, p);
        }
        else if(com == "find") {
            int k;
            cin >> k;
            if(find(root, k)) {
                cout << "yes" << endl;
            } 
            else {
                cout << "no" << endl;
            }

        }
        else if(com == "delete") {
            int k;
            cin >> k;
            root = deleteNode(root, k);
        }
        else if(com == "print") {
            inOrder(root);
            cout << endl;
            preOrder(root);
            cout << endl;
        }
    }
    return 0;
}
accuracy: 1
time_ms: 190
memory_kb: 9696
final_rank: 14

code_snippet: //Treap
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
using namespace std;

//NodePointerの定義
typedef struct node * NodePointer;

//node型構造体の定義
struct node{
  int k;
  int pri;
  NodePointer p,l,r;
};

//外部変数で定義する二分探索木の根
NodePointer root;


//各関数のプロトタイプ宣言
NodePointer insert(NodePointer,int,int);
NodePointer ddelete(NodePointer,int);
NodePointer cdelete(NodePointer,int);
NodePointer find(NodePointer,int);
NodePointer right(NodePointer);
NodePointer left(NodePointer);
void in(NodePointer);
void pre(NodePointer);

int main(){
  int i,j,pri,n,m;
  NodePointer t;
  char com[8];

 cin >> m;

  for(i=0;i<m;i++){
    cin >> com;
    if(strcmp("insert", com)==0){
      cin >> n >> pri;
      t = insert(root,n,pri);
    }else if(strcmp("find", com)==0){
      cin >> n;
      if(find(root,n)!=NULL){
        cout << "yes" << endl;
      }else{
        cout << "no" << endl;
      }
    }else if(strcmp("print", com)==0){
      in(root);
      cout << endl;
      pre(root);
      cout << endl;
    }else if(strcmp("delete", com)==0){
      cin >> n;
      t = ddelete(root,n);
    }


  }

  return 0;
}

NodePointer insert(NodePointer t,int k,int pri){
  NodePointer z,x;
  if(t == NULL){
    x = root;
    z = (NodePointer)malloc(sizeof(struct node));
    z->k = k;
    z->pri = pri;
    z->l = NULL;
    z->r = NULL;
    
    while(x!=NULL){
      t = x;
      if(z->k < x->k){
	x = x->l;
      }else{
	x = x->r;
      }
    }
    z->p = t;
    
    if(t==NULL){
      root = z;
    }else if(z->k < t->k){
      t->l = z;
    }else{
      t->r = z;
    }
    return z;
  }

  if(k==t->k)return t;

  if(k < t->k){
    t->l = insert(t->l, k, pri);
    if(t->pri < t->l->pri)t = right(t);
  }else{
    t->r = insert(t->r, k, pri);
    if(t->pri < t->r->pri)t = left(t);
  }

  return t;
}

NodePointer right(NodePointer t){
  NodePointer s;
  s = t->l;
  if(t==root)root = s;
  t->l = s->r;
  s->r = t;
  return s;
}

NodePointer left(NodePointer t){
  NodePointer s;
  s = t->r;
  if(t==root)root = s;
  t->r = s->l;
  s->l = t;
  return s;
}

NodePointer ddelete(NodePointer t,int ky){
  NodePointer x,y;
  if(t==NULL)return NULL;
  
  if(ky < t->k)t->l = ddelete(t->l,ky);
  else if(ky > t->k)t->r = ddelete(t->r,ky);
  else return cdelete(t,ky);
    
  return t;
}

NodePointer cdelete(NodePointer t,int k){
  if(t->l == NULL && t->r == NULL){
    return NULL;
  }else if(t->l == NULL){
    t = left(t);
  }else if(t->r == NULL){
    t = right(t);
  }else{
    if(t->l->pri > t->r->pri){
      t = right(t);
    }else{
      t = left(t);
    }
  }
  
  return ddelete(t,k);
}

NodePointer find(NodePointer x,int k){
  while(x!=NULL && k!=x->k){
    if(k < x->k)x = x->l;
    else x = x->r;
  }
  return x;
}

void in(NodePointer u){
  if(u==NULL)return;

  in(u->l);
  cout << " " << u->k;
  in(u->r);
}

void pre(NodePointer u){
  if(u==NULL)return;

  cout << " " << u->k;
  pre(u->l);
  pre(u->r);
}
accuracy: 1
time_ms: 200
memory_kb: 12736
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
using ll = long long;

struct Node {
    int key;
    int priority;
    Node *left, *right;
    Node(int key, int priority)
        : key(key), priority(priority), left(nullptr), right(nullptr) {}
};

Node* rightRotate(Node*& t) {
    Node* s = t->left;
    t->left = s->right;
    s->right = t;
    return s;  // root of the subtree
}
Node* leftRotate(Node*& t) {
    Node* s = t->right;
    t->right = s->left;
    s->left = t;
    return s;  // root of the subtree
}
Node* insert(Node*& t, int key, int priority) {  // 再帰的に探索
    if (t == nullptr)
        return new Node(key,
                        priority);  // 葉に到達したら新しい節点を生成して返す
    if (key == t->key) return t;    // 重複したkeyは無視
    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;
}
Node* _deleten(Node*& t, int key);
Node* deleten(Node*& t, int key) {
    if (t == nullptr) return nullptr;
    if (key < t->key)  // 削除対象を検索
        t->left = deleten(t->left, key);
    else if (key > t->key)
        t->right = deleten(t->right, key);
    else
        return _deleten(t, key);
    return t;
}
Node* _deleten(Node*& t, int key) {  // 削除対象の節点の場合
    if ((t->left == nullptr) && (t->right == nullptr)) {
        delete t;
        return nullptr;
    } else if (t->left == nullptr)  // 右の子のみ持つ場合左回転
        t = leftRotate(t);
    else if (t->right == nullptr)  // 左の子のみ持つ場合左回転
        t = rightRotate(t);
    else {                                           // 左右両方の子を持つ場合
        if (t->left->priority > t->right->priority)  // 高優先度を上へ
            t = rightRotate(t);
        else
            t = leftRotate(t);
    }
    return deleten(t, key);
}

bool find(Node*& t, int key) {
    if (t == nullptr) return false;
    bool ret = false;
    if (t->key > key)
        ret = find(t->left, key);
    else if (t->key < key)
        ret = find(t->right, key);
    else
        ret = true;
    return ret;
}

vector<int> ino, preo;
void dfs(Node* t) {
    if (t == nullptr) return;
    preo.push_back(t->key);
    dfs(t->left);
    ino.push_back(t->key);
    dfs(t->right);
}
void print(Node* t) {
    ino.clear();
    preo.clear();
    dfs(t);
    int n = ino.size();
    rep(i, n) cout << " " << ino[i];
    cout << endl;
    rep(i, n) cout << " " << preo[i];
    cout << endl;
}

Node* root;
void solve() {
    string s;
    cin >> s;
    if (s == "insert") {
        int k, p;
        cin >> k >> p;
        root = insert(root, k, p);
    } else if (s == "find") {
        int k;
        cin >> k;
        if (find(root, k))
            cout << "yes" << endl;
        else
            cout << "no" << endl;
    } else if (s == "delete") {
        int k;
        cin >> k;
        deleten(root, k);
    } else {
        print(root);
    }
}
int main() {
    int m;
    cin >> m;
    root = nullptr;
    rep(im, m) solve();
}
accuracy: 1
time_ms: 190
memory_kb: 11132
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
using ll = long long;

struct Node {
  ll value;
  ll priority;
  Node *left = nullptr;
  Node *right = nullptr;
  Node *parent = nullptr;
};

class Treap {
 private:
  Node *root = nullptr;

  void destroy(Node *node) {
    if (!node) return;
    destroy(node->left);
    destroy(node->right);
    delete node;
  }

 public:
  Treap() {}

  ~Treap() { destroy(root); }

  // nodeの左子をnodeの親にする回転
  Node *right_rotate(Node *node) {
    Node *node2 = node->left;
    node2->parent = node->parent;
    if (node2->parent && node2->parent->left == node) {
      node2->parent->left = node2;
    }
    if (node2->parent && node2->parent->right == node) {
      node2->parent->right = node2;
    }
    node->left = node2->right;
    if (node2->right) node2->right->parent = node;
    node2->right = node;
    node->parent = node2;
    if (!node2->parent) {
      root = node2;
    }
    return node2;
  }
  // nodeの右子をnodeの親にする回転
  Node *left_rotate(Node *node) {
    Node *node2 = node->right;
    node2->parent = node->parent;
    if (node2->parent && node2->parent->left == node) {
      node2->parent->left = node2;
    }
    if (node2->parent && node2->parent->right == node) {
      node2->parent->right = node2;
    }
    node->right = node2->left;
    if (node2->left) node2->left->parent = node;
    node2->left = node;
    node->parent = node2;
    if (!node2->parent) {
      root = node2;
    }
    return node2;
  }

  void insert(const ll value, ll priority = -1) {
    auto f = [&](auto rec, Node *&node, Node *p) -> Node * {
      if (!node) {
        node = new Node();
        node->value = value;
        node->priority = priority;
        node->parent = p;
        return node;
      } else if (value < node->value) {
        node->left = rec(rec, node->left, node);
        node->left->parent = node;
        // 子の方が優先度が高い場合、回転して上に上げる
        if (node->priority < node->left->priority) {
          node = right_rotate(node);
        }
        return node;
      } else {
        node->right = rec(rec, node->right, node);
        node->right->parent = node;
        if (node->priority < node->right->priority) {
          node = left_rotate(node);
        }
        return node;
      }
    };
    f(f, root, nullptr);
  }

  Node *find(const ll value, Node *subtree_root = nullptr) {
    auto f = [](auto rec, Node *&node, ll v) -> Node * {
      if (!node) return nullptr;
      if (node->value == v) return node;
      if (node->value > v) {
        return rec(rec, node->left, v);
      } else {
        return rec(rec, node->right, v);
      }
    };
    return f(f, subtree_root ? subtree_root : root, value);
  }

  void erase(const ll value) {
    Node *delete_node = find(value);
    // 木にvalueが存在しなかった
    if (!delete_node) return;

    // 葉まで移動させてから削除する
    auto f = [&](auto rec, Node *node) -> void {
      // debug_node(node);
      // 両方の子がない場合
      if (!node->left && !node->right) {
        // これを削除する
        if (node->parent && node == node->parent->left)
          node->parent->left = nullptr;
        if (node->parent && node == node->parent->right)
          node->parent->right = nullptr;
      } else if (!node->left) {
        // 右の子を上に持ち上げる（=左回転）
        left_rotate(node);
        rec(rec, node);
      } else if (!node->right) {
        right_rotate(node);
        rec(rec, node);
      } else {
        // 両方の子を持つ場合、優先度が高い方を持ち上げる
        if (node->left->priority > node->right->priority) {
          right_rotate(node);
          rec(rec, node);
        } else {
          left_rotate(node);
          rec(rec, node);
        }
      }
    };
    f(f, delete_node);
    delete delete_node;
  }

  // 指定したnodeを根とする部分木の最も小さい値を返す。デフォルトは木全体
  Node *tree_minimum(Node *root_node = nullptr) {
    auto f = [&](auto rec, Node *&node) -> Node * {
      return node->left ? rec(rec, node->left) : node;
    };
    return f(f, root_node ? root_node : root);
  }
  Node *tree_maximum(Node *root_node = nullptr) {
    auto f = [&](auto rec, Node *&node) -> Node * {
      return node->right ? rec(rec, node->right) : node;
    };
    return f(f, root_node ? root_node : root);
  }

  Node *tree_successor(Node *node) {
    assert(node);
    // 右の子がいるならその中で一番小さい値
    if (node->right) return tree_minimum(node->right);
    // 祖先のうち右上方向に登る最初のもの
    Node *ret = node;
    while (ret->parent) {
      if (ret->parent->left == ret) {
        // 初めて右上に登ったらそれがsuccessor
        ret = ret->parent;
        break;
      } else {
        // 左上に登っている間は登り続ける
        ret = ret->parent;
      }
    }
    return ret;
  }
  Node *tree_predecessor(Node *node) {
    assert(node);
    // 左の子がいるならその中で一番大きい値
    if (node->left) return tree_maximum(node->left);
    // 祖先のうち左上方向に登る最初のもの
    Node *ret = node;
    while (ret->parent) {
      if (ret->parent->right == ret) {
        // 初めて左上に登ったらそれがsuccessor
        ret = ret->parent;
        break;
      } else {
        // 右上に登っている間は登り続ける
        ret = ret->parent;
      }
    }
    return ret;
  }

  vector<ll> inorder_tree_walk() {
    vector<ll> ret;
    auto dfs = [&](auto rec, Node *&node) -> void {
      if (!node) return;
      rec(rec, node->left);
      ret.push_back(node->value);
      rec(rec, node->right);
    };
    dfs(dfs, root);
    return ret;
  }

  vector<ll> preorder_tree_walk() {
    vector<ll> ret;
    auto dfs = [&](auto rec, Node *&node) -> void {
      if (!node) return;
      ret.push_back(node->value);
      rec(rec, node->left);
      rec(rec, node->right);
    };
    dfs(dfs, root);
    return ret;
  }

  void print() {
    auto res = inorder_tree_walk();
    for (auto v : res) cout << " " << v;
    cout << endl;
    res = preorder_tree_walk();
    for (auto v : res) cout << " " << v;
    cout << endl;
  }

  // 可視化用（https://binomialsheep.github.io/sheep-visualize-graph-beta/）
  void print_edges() {
    cerr << "print_edge" << endl;
    vector<pair<int, int>> edges;
    auto dfs = [&](auto rec, Node *&node) -> void {
      if (!node) return;
      debug_node(node);
      if (node->parent) edges.emplace_back(node->parent->value, node->value);
      rec(rec, node->left);
      rec(rec, node->right);
    };
    dfs(dfs, root);
    int n = 0;
    for (auto [v, u] : edges) {
      n = max(n, v);
      n = max(n, u);
    }
    int m = (int)edges.size();
    // ラベルは圧縮しない
    cout << n << " " << m << endl;
    for (auto [v, u] : edges) {
      cout << v << " " << u << endl;
    }
  }
  // デバッグ用

  void debug_node(Node *node) {
    ll value = node->value;
    ll left = node->left ? node->left->value : -1;
    ll right = node->right ? node->right->value : -1;
    ll parent = node->parent ? node->parent->value : -1;
    cerr << "v,l,r.p: " << value << ", " << left << ", " << right << ", "
         << parent << endl;
  }

  void assert_valid_tree() {
    auto dfs = [&](auto rec, Node *&node) -> void {
      if (!node) return;
      if (node->left) {
        assert(node == node->left->parent);
        assert(node->value >= node->left->value);
      }
      if (node->right) {
        assert(node == node->right->parent);
        assert(node->value <= node->right->value);
      }
      rec(rec, node->left);
      rec(rec, node->right);
    };
    dfs(dfs, root);
  }
};

void test() {
  {
    // minimum, maximu, successor, predecessorのテスト
    Treap tree;
    tree.insert(2);
    tree.insert(1);
    tree.insert(5);
    tree.insert(4);
    tree.insert(3);
    tree.insert(6);

    Node *node = tree.tree_minimum();
    for (int i = 1; i <= 6; i++) {
      if (node->value != i) {
        cerr << i << " " << node->value << endl;
        assert(false);
      }
      node = tree.tree_successor(node);
    }
    node = tree.tree_maximum();
    for (int i = 6; i >= 1; i--) {
      if (node->value != i) {
        cerr << i << " " << node->value << endl;
        assert(false);
      }
      node = tree.tree_predecessor(node);
    }
  }
}

int main() {
  test();
  Treap tree;

  int N;
  cin >> N;
  rep(i, N) {
    string command;
    cin >> command;
    if (command == "insert") {
      int v, p;
      cin >> v >> p;
      tree.insert(v, p);
    } else if (command == "find") {
      int v;
      cin >> v;
      cout << (tree.find(v) ? "yes" : "no") << endl;
    } else if (command == "delete") {
      int v;
      cin >> v;
      tree.erase(v);
      // tree.print_edges();
    } else if (command == "print") {
      tree.print();
      // tree.print_edges();
    } else if (command == "minmax") {
      Node *min = tree.tree_minimum();
      Node *max = tree.tree_maximum();
      cout << min->value << " " << max->value << endl;
    } else {
      assert(false);
    }
  }
}
accuracy: 1
time_ms: 210
memory_kb: 17200
final_rank: 20

code_snippet: #include <bits/stdc++.h>
#include <iomanip>

struct Treap {
    int value = 0;
    int priority = 0;
    Treap* left = nullptr;
    Treap* right = nullptr;
    Treap(int value, int priority, Treap* left = nullptr, Treap* right = nullptr) {
        this->value = value;
        this->priority = priority;
        this->left = left;
        this->right = right;
    }
};

Treap* right_rotate(Treap* now) {
    Treap* new_root = now->left;
    now->left = new_root->right;
    new_root->right = now;
    return new_root;
}

Treap* left_rotate(Treap* now) {
    Treap* new_root = now->right;
    now->right = new_root->left;
    new_root->left = now;
    return new_root;
}
Treap* insert(Treap* now, int x, int p) {
    if (now == nullptr) {
        return new Treap(x, p);
    }
    if (x < now->value) {
        now->left = insert(now->left, x, p);
        if (now->left->priority > now->priority) {
            now = right_rotate(now);
        }
    } else {
        now->right = insert(now->right, x, p);
        if (now->right->priority > now->priority) {
            now = left_rotate(now);
        }
    }
    return now;
}

bool find(Treap* now, int x) {
    if (now == nullptr) {
        return false;
    }
    if (x == now->value) {
        return true;
    } else if (x < now->value) {
        return find(now->left, x);
    } else { // x > now->value
        return find(now->right, x);
    }
}

Treap* remove(Treap* now, int x);

Treap* _remove(Treap* now, int x) {
    if (now->left == nullptr && now->right == nullptr) {
        delete now;
        return nullptr;
    } else if (now->right == nullptr) { // has left subtree
        now = right_rotate(now);
    } else if (now->left == nullptr) {  // has right subtree
        now = left_rotate(now);
    } else { // has left and right subtree
        if (now->left->priority > now->right->priority) {
            now = right_rotate(now);
        } else {
            now = left_rotate(now);
        }
    }
    Treap* ret = remove(now, x);
    return ret;
//    return remove(now, x);
}

Treap* remove(Treap* now, int x) {
    if (now == nullptr) {
        return nullptr;
    }
    if (x == now->value) {
        Treap* ret = _remove(now, x);
        return ret;
//        return _remove(now, x);
    } else if (x < now->value) {
        now->left = remove(now->left, x);
    } else { // x > now->value
        now->right = remove(now->right, x);
    }
    return now;
}

void inorder(Treap* now) {
    if (now == nullptr) {
        return;
    }
    inorder(now->left);
    std::cout << " " << now->value;
    inorder(now->right);
}

void preorder(Treap* now) {
    if (now == nullptr) {
        return;
    }
    std::cout << " " << now->value;
    preorder(now->left);
    preorder(now->right);
}

void print(Treap* root) {
    inorder(root);
    std::cout << std::endl;
    preorder(root);
    std::cout << std::endl;
}

void my_release(Treap* now) {
    if (now == nullptr) {
        return;
    }
    my_release(now->left);
    my_release(now->right);
    delete now;
}

int main() {
    int n;
    std::cin >> n;

    std::string command;
    int x, p;
    Treap* root = nullptr;
    for (int i = 0; i < n; i++) {
        std::cin >> command;
        if (command == "insert") {
            std::cin >> x >> p;
            root = insert(root, x, p);
        } else if (command == "find") {
            std::cin >> x;
            std::cout << (find(root, x) ? "yes" : "no") << std::endl;
        } else if (command == "delete") {
            std::cin >> x;
            root = remove(root, x);
        } else if (command == "print") {
            print(root);
        }
    }

    my_release(root);
}
accuracy: 1
time_ms: 200
memory_kb: 9692
final_rank: 15

code_snippet: #include <iostream>

class Treap {
    class Node_t {
    public:
        int key;
        int pri;
        Node_t* left;
        Node_t* right;
    };

    Node_t* arr;
    Node_t* root = NULL;
    int idx = 0;
    int pri_max = -2000000001;

public:
    Treap(int n) {
        arr = new Node_t[n];
    }

    ~Treap() {
        delete[] arr;
    }

    Node_t* get_root() {
        return root;
    }

    void set_root(Node_t* t) {
        root = t;
    }

    Node_t* insert(Node_t* t, int in_key, int in_pri) {
        if (t == NULL) {
            arr[idx].key = in_key;
            arr[idx].pri = in_pri;
            arr[idx].left = NULL;
            arr[idx].right = NULL;
            t = &arr[idx];
            if (in_pri > pri_max) {
                pri_max = in_pri;
                root = &arr[idx];
            }
            ++idx;
            return t;
        }

        if (in_key == t->key)
            return t;

        if (in_key < t->key) {
            t->left = insert(t->left, in_key, in_pri);
            if (t->pri < t->left->pri)
                t = rightRotate(t);
        }
        else {
            t->right = insert(t->right, in_key, in_pri);
            if (t->pri < t->right->pri)
                t = leftRotate(t);
        }
      
        return t;
    }

    Node_t* rightRotate(Node_t* t) {
        Node_t* s = t->left;
        t->left = s->right;
        s->right = t;

        return s;
    }

    Node_t* leftRotate(Node_t* t) {
        Node_t* s = t->right;
        t->right = s->left;
        s->left = t;

        return s;
    }

    Node_t* deleteNode(Node_t* t, int in_key) {
        if (t == NULL)
            return NULL;
        else if (in_key < t->key)
            t->left = deleteNode(t->left, in_key);
        else if (in_key > t->key)
            t->right = deleteNode(t->right, in_key);
        else
            return _deleteNode(t, in_key);

        return t;
    }

    Node_t* _deleteNode(Node_t* t, int in_key) {
        if (t->left == NULL && t->right == NULL)
            return NULL;
        else if (t->left == NULL)
            t = leftRotate(t);
        else if (t->right == NULL)
            t = rightRotate(t);
        else
            if (t->left->pri > t->right->pri)
                t = rightRotate(t);
            else
                t = leftRotate(t);
        
        return deleteNode(t, in_key);
    }
    
    bool find(int in_key) {
        bool flg = false;
        Node_t* temp = root;

        while (!flg && temp != NULL)
            if (in_key == temp->key)
                flg = true;
            else if (in_key < temp->key)
                temp = temp->left;
            else
                temp = temp->right;

        return flg;
    }

    void inorder_tree_walk(Node_t* in_parent) {
        if (in_parent != NULL) {
            inorder_tree_walk(in_parent->left);
            std::cout << " " << in_parent->key;
            inorder_tree_walk(in_parent->right);
        }
    }

    void preorder_tree_walk(Node_t* in_parent) {
        if (in_parent != NULL) {
            std::cout << " " << in_parent->key;
            preorder_tree_walk(in_parent->left);
            preorder_tree_walk(in_parent->right);
        }
    }

};

int main()
{
    int n;
    std::string cmd;
    int in_key, in_pri;
 
    std::cin >> n;
    Treap tr = Treap(n);
    
    for (int i = 0; i < n; i++) {
        std::cin >> cmd;
        if (cmd == "insert") {
            std::cin >> in_key >> in_pri;
            tr.insert(tr.get_root(), in_key, in_pri);
        }
        else if (cmd == "delete") {
            std::cin >> in_key;
            tr.set_root(tr.deleteNode(tr.get_root(), in_key));
        }
        else if (cmd == "find") {
            std::cin >> in_key;
            if (tr.find(in_key))
                std::cout << "yes" << std::endl;
            else
                std::cout << "no" << std::endl;
        }
        else if (cmd == "print") {
            tr.inorder_tree_walk(tr.get_root());
            std::cout << std::endl;
            tr.preorder_tree_walk(tr.get_root());
            std::cout << std::endl;
        }
    }
}
accuracy: 1
time_ms: 180
memory_kb: 7776
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;

struct Node {
	int key;
	int priority;
	Node* left;
	Node* right;
	Node(int k, int p) : key(k), priority(p), left(nullptr), right(nullptr) {}
};

Node* rightRotate(Node* t) {
	Node* s = t->left;
	t->left = s->right;
	s->right = t;
	return s;
}

Node* leftRotate(Node* t) {
	Node* s = t->right;
	t->right = s->left;
	s->left = t;
	return s;
}

Node* insert(Node* t, int key, int priority) {
	if (t == nullptr) {
		return new 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;
}

Node* deleteNode(Node* t, int key) {
	if (t == nullptr) {
		return nullptr;
	}
	if (key < t->key) {
		t->left = deleteNode(t->left, key);
	} else if (key > t->key) {
		t->right = deleteNode(t->right, key);
	} else {
		if (t->left == nullptr && t->right == nullptr) {
			delete t;
			return nullptr;
		}
		if (t->left == nullptr) {
			Node* temp = t->right;
			delete t;
			t = temp;
		} else if (t->right == nullptr) {
			Node* temp = t->left;
			delete t;
			t = temp;
		} else {
			if (t->left->priority > t->right->priority) {
				t = rightRotate(t);
				t->right = deleteNode(t->right, key);
			} else {
				t = leftRotate(t);
				t->left = deleteNode(t->left, key);
			}
		}
	}
	return t;
}

bool find(Node* t, int key) {
	if (t == nullptr) return false;
	if (key == t->key) return true;
	if (key < t->key) return find(t->left, key);
	else return find(t->right, key);
}

void inorder(Node* t) {
	if (t == nullptr) return;
	inorder(t->left);
	cout << " " << t->key;
	inorder(t->right);
}

void preorder(Node* t) {
	if (t == nullptr) return;
	cout << " " << t->key;
	preorder(t->left);
	preorder(t->right);
}

int main() {
	int m; cin >> m;
	Node* root = nullptr;
	while (m--) {
		string op;
		cin >> op;
		if (op == "insert") {
			int k, p;
			cin >> k >> p;
			root = insert(root, k, p);
		} else if (op == "find") {
			int k;
			cin >> k;
			if (find(root, k)) cout << "yes" << endl;
			else cout << "no" << endl;
		} else if (op == "delete") {
			int k;
			cin >> k;
			root = deleteNode(root, k);
		} else {
			inorder(root);
			cout << "\n";
			preorder(root);
			cout << "\n";
		}
	}
	return 0;
}
accuracy: 1
time_ms: 190
memory_kb: 9692
final_rank: 12