package main

import (
	"bufio"
	"fmt"
	"math/rand"
	"os"
	"strconv"
)

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(nextString())
	if err != nil {
		fmt.Printf("strconv.Atoi failed: %v\n", err)
	}
	return n
}

// NewTreapNode is a constructor of TreapNode
func NewTreapNode(key int, priority int) *TreapNode {
	tp := TreapNode{}
	tp.key = key
	tp.priority = priority
	return &tp
}

// TreapNode contains integer
type TreapNode struct {
	key      int
	priority int
	left     *TreapNode
	right    *TreapNode
}

func (node *TreapNode) setLeftChild(key int, priority int) {
	child := NewTreapNode(key, priority)
	node.left = child
}

func (node *TreapNode) setRightChild(key int, priority int) {
	child := NewTreapNode(key, priority)
	node.right = child
}

// NewTreap is a constructor of Treap
func NewTreap() *Treap {
	treap := Treap{}
	return &treap
}

// Treap described at
//  https://onlinejudge.u-aizu.ac.jp/courses/lesson/1/ALDS1/all/ALDS1_8_D
type Treap struct {
	root *TreapNode
}

func (t *Treap) insert(key int, priority int) {
	if t.root == nil {
		node := NewTreapNode(key, priority)
		t.root = node
	} else {
		t.root = t.insertLoop(t.root, key, priority)
	}
}

func (t *Treap) insertLoop(current *TreapNode, key int, priority int) *TreapNode {
	if current == nil {
		newNode := NewTreapNode(key, priority)
		return newNode
	}

	if key < current.key {
		current.left = t.insertLoop(current.left, key, priority)
		if current.priority < current.left.priority {
			current = t.rightRotate(current)
		}
	} else {
		current.right = t.insertLoop(current.right, key, priority)
		if current.priority < current.right.priority {
			current = t.leftRotate(current)
		}
	}

	return current
}

func (t *Treap) leftRotate(x *TreapNode) *TreapNode {
	y := x.right
	x.right = y.left
	y.left = x
	return y
}

func (t *Treap) rightRotate(y *TreapNode) *TreapNode {
	x := y.left
	y.left = x.right
	x.right = y
	return x
}

func (t *Treap) find(key int) int {
	return t.findLoop(t.root, key)
}

func (t *Treap) findLoop(current *TreapNode, key int) int {
	if current == nil {
		return 0
	}

	nFitted := 0
	if key <= current.key {
		nFitted += t.findLoop(current.left, key)
	}
	if current.key == key {
		nFitted++
	}
	if key >= current.key {
		nFitted += t.findLoop(current.right, key)
	}

	return nFitted
}

func (t *Treap) delete(key int) {
	t.root = t.deleteLoop(t.root, key)
}

func (t *Treap) deleteLoop(current *TreapNode, key int) *TreapNode {
	if current == nil {
		return nil
	}
	if key <= current.key {
		current.left = t.deleteLoop(current.left, key)
	} else if key >= current.key {
		current.right = t.deleteLoop(current.right, key)
	}
	if current.key == key {
		current = t.deleteNode(current, key)
	}
	return current
}

func (t *Treap) deleteNode(node *TreapNode, key int) *TreapNode {
	if node.left == nil && node.right == nil {
		return nil
	} else if node.left == nil {
		node = t.leftRotate(node)
	} else if node.right == nil {
		node = t.rightRotate(node)
	} else {
		if node.left.priority > node.right.priority {
			node = t.rightRotate(node)
		} else {
			node = t.leftRotate(node)
		}
	}
	return t.deleteLoop(node, key)
}

func (t *Treap) inorderTreeWalkV2(p *TreapNode) string {
	if p == nil {
		return "."
	}

	l := t.inorderTreeWalkV2(p.left)
	r := t.inorderTreeWalkV2(p.right)
	return fmt.Sprintf("(%v %v %v)", p.key, l, r)
}

func (t *Treap) printInorderWalk(l, r int) {
	if t.root != nil {
		fmt.Print(t.printInorderWalkRoop(t.root, l, r))
	}
}

func (t *Treap) printInorderWalkRoop(mid *TreapNode, l, r int) string {
	concated := ""
	if mid.left != nil && l <= mid.key {
		concated += t.printInorderWalkRoop(mid.left, l, r)
	}
	if l <= mid.key && mid.key <= r {
		concated += strconv.Itoa(mid.key) + "\n"
	}
	if mid.right != nil && mid.key <= r {
		concated += t.printInorderWalkRoop(mid.right, l, r)
	}
	return concated
}

// NewSet is constructer of Set
func NewSet() *Set {
	s := new(Set)
	s.tree = *NewTreap()
	s.size = 0
	return s
}

// Set for integer
type Set struct {
	tree Treap
	size int
}

func (s *Set) insert(x int) int {
	priority := rand.Int()
	s.tree.insert(x, priority)
	s.size++
	return s.size
}

func (s *Set) find(x int) int {
	return s.tree.find(x)
}

func (s *Set) delete(x int) {
	nFitted := s.tree.find(x)
	if nFitted > 0 {
		s.tree.delete(x)
		s.size -= nFitted
	}
}

func (s *Set) printAll() {
	fmt.Println(s.tree.inorderTreeWalkV2(s.tree.root))
}

func (s *Set) printRange(l, r int) {
	s.tree.printInorderWalk(l, r)
}

func main() {
	scanner.Split(bufio.ScanWords)

	nQueries := nextInt()
	set := NewSet()
	for iQuery := 0; iQuery < nQueries; iQuery++ {
		queryType := nextInt()
		x := nextInt()
		if queryType == 0 { // insert
			setSize := set.insert(x)
			fmt.Println(setSize)
		} else if queryType == 1 { // find
			fmt.Println(set.find(x))
		} else if queryType == 2 { // delete
			// fmt.Println("delete", x)
			set.delete(x)
		} else if queryType == 3 { // dump(L, R)
			l := x
			r := nextInt()
			// set.printAll()
			set.printRange(l, r)
		}
	}
}



Measured memory_bytes: 19516