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 {
		return current
	}

	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) *TreapNode {
	return t.findLoop(t.root, key)
}

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

	if key < current.key {
		return t.findLoop(current.left, key)
	} else if current.key == key {
		return current
	}
	return t.findLoop(current.right, key)
}

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)
	} else {
		return 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 {
	// fmt.Println("*", l, r, mid.key)
	// if mid.left != nil {
	// 	fmt.Println("left:", mid.left.key)
	// }
	// if mid.right != nil {
	// 	fmt.Println("right:", mid.right.key)
	// }

	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 {
	xNode := s.tree.find(x)
	if xNode == nil {
		priority := rand.Int()
		s.tree.insert(x, priority)
		s.size++
	}
	return s.size
}

func (s *Set) find(x int) int {
	target := s.tree.find(x)
	if target != nil {
		return 1
	}
	return 0
}

func (s *Set) delete(x int) {
	target := s.tree.find(x)
	if target != nil {
		s.tree.delete(x)
		s.size--
	}
}

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()
			// fmt.Println(l, r)
			// fmt.Println(set.tree.inorderTreeWalkV2(set.tree.root))
			set.printRange(l, r)
		}
	}
}



Measured memory_bytes: 22252