package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
)

func configure(scanner *bufio.Scanner) {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
}
func getNextString(scanner *bufio.Scanner) string {
	scanned := scanner.Scan()
	if !scanned {
		panic("scan failed")
	}
	return scanner.Text()
}
func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}
func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}
func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}
func main() {
	fp := os.Stdin
	wfp := os.Stdout
	extra := 0
	if os.Getenv("I") == "IronMan" {
		fp, _ = os.Open(os.Getenv("END_GAME"))
		extra = 100
	}
	scanner := bufio.NewScanner(fp)
	configure(scanner)
	writer := bufio.NewWriter(wfp)
	defer func() {
		r := recover()
		if r != nil {
			fmt.Fprintln(writer, r)
		}
		writer.Flush()
	}()
	solve(scanner, writer)
	for i := 0; i < extra; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
}
func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	q := getNextInt(scanner)
	qq := make([]query, q)
	aa := make([]int, 0)
	for i := 0; i < q; i++ {
		qq[i].t = getNextInt(scanner)
		qq[i].arg1 = getNextInt(scanner)
		aa = append(aa, qq[i].arg1)
		if qq[i].t != 3 {
			continue
		}
		qq[i].arg2 = getNextInt(scanner)
		aa = append(aa, qq[i].arg2)
	}
	c := newCompress(aa)
	r := map[int]int{}
	for v, i := range c {
		r[i] = v
	}
	seg := newSegment(len(c) + 1)
	for i := 0; i < q; i++ {
		switch qq[i].t {
		case 0:
			seg.update(c.get(qq[i].arg1), 1, seg.add)
			fmt.Fprintln(writer, seg[len(seg)-1][0])
		case 1:
			fmt.Fprintln(writer, seg[0][c.get(qq[i].arg1)])
		case 2:
			seg.update(c.get(qq[i].arg1), 0, seg.add)
		case 3:
			ans := make([]int, 0)
			for j := seg.left(0, c.get(qq[i].arg2), 1); j >= c.get(qq[i].arg1); j = seg.left(0, j-1, 1) {
				ans = append(ans, r[j])
			}
			for j := len(ans) - 1; j >= 0; j-- {
				fmt.Fprintln(writer, ans[j])
			}
		}
	}
}

type query struct {
	t          int
	arg1, arg2 int
}
type segment [][]int

func newSegment(n int) segment {
	seg := make(segment, 0)
	for n > 1 {
		seg = append(seg, make([]int, n))
		n = (n + 1) >> 1
	}
	seg = append(seg, make([]int, 1))
	return seg
}
func (seg segment) update(i int, v int, f func(x, y int) int) {
	seg[0][i] = v
	for h := 1; h < len(seg); h++ {
		i >>= 1
		if i<<1+1 < len(seg[h-1]) {
			seg[h][i] = f(seg[h-1][i<<1], seg[h-1][i<<1+1])
		} else {
			seg[h][i] = seg[h-1][i<<1]
		}
	}
}
func (seg segment) query(l, r int, s int, f func(x, y int) int) int {
	for h := 0; h < len(seg) && l < r; h++ {
		if l&1 == 1 {
			s = f(s, seg[h][l])
			l++
		}
		l >>= 1
		if r&1 == 1 {
			s = f(s, seg[h][r-1])
		}
		r >>= 1
	}
	return s
}
func (seg segment) add(x, y int) int {
	return x + y
}
func (seg segment) min(x, y int) int {
	if x < y {
		return x
	}
	return y
}
func (seg segment) max(x, y int) int {
	return -seg.min(-x, -y)
}
func (seg segment) left(h, i, k int) int {
	if i < 0 {
		return -1
	}
	if seg[h][i] < k {
		k -= seg[h][i]
		i--
		if i&1 == 0 {
			return seg.left(h, i, k)
		}
		return seg.left(h+1, i>>1, k)
	}
	if h == 0 {
		return i
	}
	if k <= seg[h][i]-seg[h-1][i<<1] {
		return seg.left(h-1, i<<1+1, k)
	}
	return seg.left(h-1, i<<1, k-seg[h][i]+seg[h-1][i<<1])
}
func (seg segment) right(h, i, k int) int {
	if h >= len(seg) || i >= len(seg[h]) {
		return len(seg[0])
	}
	if seg[h][i] < k {
		k -= seg[h][i]
		i++
		if i&1 == 1 {
			return seg.right(h, i, k)
		}
		return seg.right(h+1, i>>1, k)
	}
	if h == 0 {
		return i
	}
	if k <= seg[h-1][i<<1] {
		return seg.right(h-1, i<<1, k)
	}
	return seg.right(h-1, i<<1+1, k-seg[h-1][i<<1])
}

type compress map[int]int

func newCompress(aa []int) compress {
	n := len(aa)
	bb := make([]int, n)
	copy(bb, aa)
	sort.SliceStable(bb, func(i, j int) bool {
		return bb[i] < bb[j]
	})
	c := compress{}
	l := 0
	i := 0
	for r := 0; r < n; r++ {
		for r > l && bb[r] != bb[l] {
			l++
		}
		if r == l {
			c[bb[l]] = i
			i++
		}
	}
	return c
}
func (c compress) get(x int) int {
	return c[x]
}



Measured memory_bytes: 44264