Language: C

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

#define BUF_LENGTH 100
#define OPE_LENGTH 10

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;
	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;
	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 * 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(t, key);
		return t;
	}

	if (key < t->key) {
		if (t->left == NULL) {
			return NULL;
		}
		delete(t->left, key);
		return t;
	}
	else {
		if (t->right == NULL) {
			return NULL;
		}
		delete(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;
	char buf[BUF_LENGTH];
	char * str;
	int k, p;
	node * root = NULL;

	scanf_s("%d ", &m);

	for (int i = 0; i < m; i++) {
		fgets(&buf, BUF_LENGTH, stdin);
		char * context;
		str = strtok_s(buf, " \n", &context);
		if (strcmp(str, "insert") == 0) {
			str = strtok_s(NULL, " \n", &context);
			k = atoi(str);
			str = strtok_s(NULL, " \n", &context);
			p = atoi(str);
			//printf("--insert %d %d\n", k, p);
			root = insert(root, k, p);
		}
		else if (strcmp(str, "find") == 0) {
			str = strtok_s(NULL, " \n", &context);
			k = atoi(str);
			//printf("--find %d\n", k);
			int result = find(root, k);
			if (result == 1) {
				printf("yes\n");
			}
			else {
				printf("no\n");
			}
		}
		else if (strcmp(str, "delete") == 0) {
			str = strtok_s(NULL, " \n", &context);
			k = atoi(str);
			//printf("--delete %d\n", k);
			root = delete(root, k);
		}
		else if (strcmp(str, "print") == 0) {
			//printf("--print\n");
			print_inorder(root);
			printf("\n");
			print_preorder(root);
			printf("\n");
		}
		else {
			//printf("error\n");
		}
	}
	
	return 0;
}


Compiler output:
main.c: In function 'main':
main.c:205:9: error: implicit declaration of function 'scanf_s'; did you mean 'scanf'? [-Wimplicit-function-declaration]
  205 |         scanf_s("%d ", &m);
      |         ^~~~~~~
      |         scanf
main.c:208:23: error: passing argument 1 of 'fgets' from incompatible pointer type [-Wincompatible-pointer-types]
  208 |                 fgets(&buf, BUF_LENGTH, stdin);
      |                       ^~~~
      |                       |
      |                       char (*)[100]
In file included from main.c:1:
/usr/include/stdio.h:654:38: note: expected 'char * restrict' but argument is of type 'char (*)[100]'
  654 | extern char *fgets (char *__restrict __s, int __n, FILE *__restrict __stream)
      |                     ~~~~~~~~~~~~~~~~~^~~
main.c:210:23: error: implicit declaration of function 'strtok_s'; did you mean 'strtok_r'? [-Wimplicit-function-declaration]
  210 |                 str = strtok_s(buf, " \n", &context);
      |                       ^~~~~~~~
      |                       strtok_r
main.c:210:21: error: assignment to 'char *' from 'int' makes pointer from integer without a cast [-Wint-conversion]
  210 |                 str = strtok_s(buf, " \n", &context);
      |                     ^
main.c:212:29: error: assignment to 'char *' from 'int' makes pointer from integer without a cast [-Wint-conversion]
  212 |                         str = strtok_s(NULL, " \n", &context);
      |                             ^
main.c:214:29: error: assignment to 'char *' from 'int' makes pointer from integer without a cast [-Wint-conversion]
  214 |                         str = strtok_s(NULL, " \n", &context);
      |                             ^
main.c:220:29: error: assignment to 'char *' from 'int' makes pointer from integer without a cast [-Wint-conversion]
  220 |                         str = strtok_s(NULL, " \n", &context);
      |                             ^
main.c:232:29: error: assignment to 'char *' from 'int' makes pointer from integer without a cast [-Wint-conversion]
  232 |                         str = strtok_s(NULL, " \n", &context);
      |                             ^