package main

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

type Scanner struct {
  r            io.Reader
  split        bufio.SplitFunc
  maxTokenSize int
  token, buf   []byte
  start, end   int
}

func NewScanner(r io.Reader) *Scanner {
  return &Scanner{
    r:            r,
    split:        bufio.ScanWords,
    maxTokenSize: 16777216,
    buf:          make([]byte, 4096),
  }
}

func (s *Scanner) advance(n int) bool {
  if n < 0 || n > s.end-s.start {
    return false
  }
  s.start += n
  return true
}

func (s *Scanner) Text() string {
  return string(s.token)
}

func (s *Scanner) Scan() bool {
  for {
    if s.end > s.start {
      advance, token, err := s.split(s.buf[s.start:s.end], false)
      if err != nil || !s.advance(advance) {
        return false
      }
      s.token = token
      if token != nil {
        return true
      }
    }
    if s.start > 0 && (s.end == len(s.buf) || s.start > len(s.buf)/2) {
      copy(s.buf, s.buf[s.start:s.end])
      s.end -= s.start
      s.start = 0
    }
    if s.end == len(s.buf) {
      if len(s.buf) >= s.maxTokenSize {
        return false
      }
      newSize := len(s.buf) * 2
      if newSize > s.maxTokenSize {
        newSize = s.maxTokenSize
      }
      newBuf := make([]byte, newSize)
      copy(newBuf, s.buf[s.start:s.end])
      s.buf = newBuf
      s.end -= s.start
      s.start = 0
      continue
    }
    for loop := 0; ; {
      n, _ := s.r.Read(s.buf[s.end:len(s.buf)])
      s.end += n
      if n > 0 {
        break
      }
      loop++
      if loop > 100 {
        break
      }
    }
  }
}

func (s *Scanner) Split(split bufio.SplitFunc) {
  s.split = split
}

var w *bufio.Writer = bufio.NewWriter(os.Stdout)
var sc *Scanner = NewScanner(os.Stdin)

func next() string { sc.Scan(); return sc.Text() }
func nc() byte     { sc.Scan(); return sc.token[0] }

func ni32() int {
  sc.Scan()
  n, _ := strconv.Atoi(sc.Text())
  return n
}

func ni64() int64 {
  sc.Scan()
  n, _ := strconv.ParseInt(sc.Text(), 10, 64)
  return n
}

func pri32(i ...int) {
  for _, ii := range i {
    w.WriteString(strconv.Itoa(ii))
    sp()
  }
}
func pri64(i ...int64) {
  for _, ii := range i {
    w.WriteString(strconv.FormatInt(ii, 10))
    sp()
  }
}
func sp()   { w.WriteByte(' ') }
func endl() { w.WriteByte('\n') }

func fastMax(x, y int) int { return (((y - x) >> 31) & (x ^ y)) ^ y }
func fastMin(x, y int) int { return (((y - x) >> 31) & (x ^ y)) ^ x }

func compare(key1, key2 int) int {
  if key1 > key2 {
    return 1
  } else if key1 < key2 {
    return -1
  }
  return 0
}

type MetroStation struct {
  Key     int
  next    []*MetroStation
  prev    []*MetroStation
  nextLen []int
}

type Metro struct {
  LDepot, RDepot *MetroStation
  Len            int
}

func NewMetro() *Metro {
  rdepot := &MetroStation{
    Key:     int(math.MaxInt32),
    next:    []*MetroStation{},
    nextLen: []int{},
    prev:    []*MetroStation{nil},
  }
  ldepot := &MetroStation{
    Key:     int(math.MinInt32),
    next:    []*MetroStation{rdepot},
    nextLen: []int{0},
    prev:    []*MetroStation{nil},
  }
  rdepot.prev[0] = ldepot
  return &Metro{
    LDepot: ldepot,
    RDepot: rdepot,
    Len:    0,
  }
}

func (metro *Metro) genHeight() int {
  height := 1
  // for rand.Int31n(2) == 1 {
  //   height++
  // }
  for n := rand.Int31(); n&1 == 0; n = n >> 1 {
    height++
  }
  return height
}

func (metro *Metro) incHeight(newheight int) {
  for i := len(metro.LDepot.next); i < newheight; i++ {
    metro.LDepot.next = append(metro.LDepot.next, metro.RDepot)
    metro.LDepot.nextLen = append(metro.LDepot.nextLen, 0)
    metro.RDepot.prev = append(metro.RDepot.prev, metro.LDepot)
  }
}

func (metro *Metro) clean() {
  level := len(metro.LDepot.next)
  for ; level >= 1 && metro.LDepot.next[level-1] == metro.RDepot; level-- {
  }
  metro.LDepot.next = metro.LDepot.next[0:level]
  metro.RDepot.prev = metro.RDepot.prev[0:level]
}

func (metro *Metro) nth(index int) *MetroStation {
  idx := 0
  station := metro.LDepot
  for level := len(station.next) - 1; level >= 0; level-- {
    for idx+station.nextLen[level] <= index+1 {
      idx += station.nextLen[level]
      station = station.next[level]
      if station == metro.RDepot {
        return station
      }
    }
  }
  return station
}

func (metro *Metro) lowerBound(key int) ([]*MetroStation, []int) {
  l := len(metro.LDepot.next)
  lane := make([]int, l)
  prev := make([]*MetroStation, l)
  prev[l-1] = metro.LDepot
  lane[l-1] = 0
  for level := l - 1; level >= 0; level-- {
    if level < l-1 {
      lane[level] = lane[level+1]
      prev[level] = prev[level+1]
    }
    for compare(key, prev[level].next[level].Key) == 1 {
      lane[level] += prev[level].nextLen[level]
      prev[level] = prev[level].next[level]
    }
  }
  return prev, lane
}

func (metro *Metro) insertStation(key int) *MetroStation {
  height := metro.genHeight()
  l := len(metro.LDepot.next)
  if height > l {
    metro.incHeight(height)
    l = height
  }
  prev, lane := metro.lowerBound(key)
  if compare(prev[0].next[0].Key, key) == 0 {
    return prev[0].next[0]
  }
  for i := l - 1; i >= 0; i-- {
    lane[i] = lane[0] - lane[i]
  }
  station := &MetroStation{
    Key:     key,
    next:    make([]*MetroStation, height),
    nextLen: make([]int, height),
    prev:    make([]*MetroStation, height),
  }
  for i := 0; i < height; i++ {
    station.next[i] = prev[i].next[i]
    station.nextLen[i] = prev[i].nextLen[i] - lane[i]
    station.prev[i] = prev[i]
    prev[i].next[i].prev[i] = station
    prev[i].next[i] = station
    prev[i].nextLen[i] = lane[i] + 1
  }
  for i := height; i < l; i++ {
    prev[i].nextLen[i]++
  }
  metro.Len++
  return station
}

func (metro *Metro) insertStation0(key, height int, prev []*MetroStation, lane []int) *MetroStation {
  l := len(metro.LDepot.next)
  if height > l {
    metro.incHeight(height)
    for i := l; i < height; i++ {
      prev = append(prev, metro.LDepot)
      lane = append(lane, 0)
    }
    l = height
  }
  if compare(prev[0].next[0].Key, key) == 0 {
    return prev[0].next[0]
  }
  for i := l - 1; i >= 0; i-- {
    lane[i] = lane[0] - lane[i]
  }
  station := &MetroStation{
    Key:     key,
    next:    make([]*MetroStation, height),
    nextLen: make([]int, height),
    prev:    make([]*MetroStation, height),
  }
  for i := 0; i < height; i++ {
    station.next[i] = prev[i].next[i]
    station.nextLen[i] = prev[i].nextLen[i] - lane[i]
    station.prev[i] = prev[i]
    prev[i].next[i].prev[i] = station
    prev[i].next[i] = station
    prev[i].nextLen[i] = lane[i] + 1
  }
  for i := height; i < l; i++ {
    prev[i].nextLen[i]++
  }
  metro.Len++
  return station
}

func (metro *Metro) deleteStation(key int) *MetroStation {
  prev, _ := metro.lowerBound(key)
  station := prev[0].next[0]
  if compare(station.Key, key) != 0 {
    return prev[0]
  }
  height := len(station.next)
  for i := 0; i < height; i++ {
    prev[i].nextLen[i] = prev[i].nextLen[i] + station.nextLen[i] - 1
    prev[i].next[i] = station.next[i]
    station.next[i].prev[i] = prev[i]
  }
  for i := height; i < len(metro.LDepot.next); i++ {
    prev[i].nextLen[i]--
  }
  metro.Len--
  metro.clean()
  return prev[0]
}

func (metro *Metro) Traverse() {
  for i := len(metro.LDepot.next) - 1; i >= 0; i-- {
    for st := metro.LDepot; st != metro.RDepot; st = st.next[i] {
      if st != metro.LDepot {
        fmt.Print(st.Key, "\t")
      }
      for j := 0; j < st.nextLen[i]-1; j++ {
        fmt.Print("\t")
      }
    }
    fmt.Println()
  }
}

func main() {
  rand.Seed(time.Now().UnixNano())
  n := ni32()
  a, idx := make([]int, n+1), make([]int, n+1)
  for i := 1; i <= n; i++ {
    a[i] = ni32()
    idx[a[i]] = i
  }
  metro := NewMetro()
  cur := 1
  metro.insertStation(cur)
  for i := 2; i <= n; i++ {
    if idx[i] <= idx[i-1] {
      cur = i
      metro.insertStation(i)
    }
  }
  q := ni32()

  process := func(v int) {
    prev, _ := metro.lowerBound(v + 1)
    if prev[0].Key == v {
      if v > 1 && idx[v] > idx[v-1] {
        prev[0] = prev[0].prev[0]
        metro.deleteStation(v)
      }
      if v < n {
        if idx[v] > idx[v+1] && prev[0].next[0].Key != v+1 {
          metro.insertStation(v + 1)
        } else if idx[v] < idx[v+1] && prev[0].next[0].Key == v+1 {
          metro.deleteStation(v + 1)
        }
      }
    } else {
      if v > 1 && idx[v-1] > idx[v] {
        metro.insertStation(v)
        prev[0] = prev[0].next[0]
      }
      if v < n {
        if idx[v] < idx[v+1] && prev[0].next[0].Key == v+1 {
          metro.deleteStation(v + 1)
        } else if idx[v] > idx[v+1] && prev[0].next[0].Key != v+1 {
          metro.insertStation(v + 1)
        }
      }
    }
  }

  for i := 0; i < q; i++ {
    p, x, y := ni32(), ni32(), ni32()
    if p == 1 {
      p1, lane1 := metro.lowerBound(x)
      p2, lane2 := metro.lowerBound(y)
      if p1[0].next[0].Key != x {
        lane1[0]--
      }
      if p2[0].next[0].Key != y {
        lane2[0]--
      }
      ans := lane1[0] - lane2[0]
      if ans >= 0 {
        pri32(ans + 1)
      } else {
        pri32(1 - ans)
      }
      endl()
    }
    if p == 2 {
      idx[a[x]], idx[a[y]] = idx[a[y]], idx[a[x]]
      a[x], a[y] = a[y], a[x]

      process(a[x])
      process(a[y])
    }
  }
  w.Flush()
}

package main

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

type Scanner struct {
  r            io.Reader
  split        bufio.SplitFunc
  maxTokenSize int
  token, buf   []byte
  start, end   int
}

func NewScanner(r io.Reader) *Scanner {
  return &Scanner{
    r:            r,
    split:        bufio.ScanWords,
    maxTokenSize: 16777216,
    buf:          make([]byte, 4096),
  }
}

func (s *Scanner) advance(n int) bool {
  if n < 0 || n > s.end-s.start {
    return false
  }
  s.start += n
  return true
}

func (s *Scanner) Text() string {
  return string(s.token)
}

func (s *Scanner) Scan() bool {
  for {
    if s.end > s.start {
      advance, token, err := s.split(s.buf[s.start:s.end], false)
      if err != nil || !s.advance(advance) {
        return false
      }
      s.token = token
      if token != nil {
        return true
      }
    }
    if s.start > 0 && (s.end == len(s.buf) || s.start > len(s.buf)/2) {
      copy(s.buf, s.buf[s.start:s.end])
      s.end -= s.start
      s.start = 0
    }
    if s.end == len(s.buf) {
      if len(s.buf) >= s.maxTokenSize {
        return false
      }
      newSize := len(s.buf) * 2
      if newSize > s.maxTokenSize {
        newSize = s.maxTokenSize
      }
      newBuf := make([]byte, newSize)
      copy(newBuf, s.buf[s.start:s.end])
      s.buf = newBuf
      s.end -= s.start
      s.start = 0
      continue
    }
    for loop := 0; ; {
      n, _ := s.r.Read(s.buf[s.end:len(s.buf)])
      s.end += n
      if n > 0 {
        break
      }
      loop++
      if loop > 100 {
        break
      }
    }
  }
}

func (s *Scanner) Split(split bufio.SplitFunc) {
  s.split = split
}

var w *bufio.Writer = bufio.NewWriter(os.Stdout)
var sc *Scanner = NewScanner(os.Stdin)

func next() string { sc.Scan(); return sc.Text() }
func nc() byte     { sc.Scan(); return sc.token[0] }

func ni32() int {
  sc.Scan()
  n, _ := strconv.Atoi(sc.Text())
  return n
}

func ni64() int64 {
  sc.Scan()
  n, _ := strconv.ParseInt(sc.Text(), 10, 64)
  return n
}

func pri32(i ...int) {
  for _, ii := range i {
    w.WriteString(strconv.Itoa(ii))
    sp()
  }
}
func pri64(i ...int64) {
  for _, ii := range i {
    w.WriteString(strconv.FormatInt(ii, 10))
    sp()
  }
}
func sp()   { w.WriteByte(' ') }
func endl() { w.WriteByte('\n') }

func fastMax(x, y int) int { return (((y - x) >> 31) & (x ^ y)) ^ y }
func fastMin(x, y int) int { return (((y - x) >> 31) & (x ^ y)) ^ x }

func compare(key1, key2 int) int {
  if key1 > key2 {
    return 1
  } else if key1 < key2 {
    return -1
  }
  return 0
}

type MetroStation struct {
  Key     int
  next    []*MetroStation
  prev    []*MetroStation
  nextLen []int
}

type Metro struct {
  LDepot, RDepot *MetroStation
  Len            int
}

func NewMetro() *Metro {
  rdepot := &MetroStation{
    Key:     int(math.MaxInt32),
    next:    []*MetroStation{},
    nextLen: []int{},
    prev:    []*MetroStation{nil},
  }
  ldepot := &MetroStation{
    Key:     int(math.MinInt32),
    next:    []*MetroStation{rdepot},
    nextLen: []int{0},
    prev:    []*MetroStation{nil},
  }
  rdepot.prev[0] = ldepot
  return &Metro{
    LDepot: ldepot,
    RDepot: rdepot,
    Len:    0,
  }
}

func (metro *Metro) genHeight() int {
  height := 1
  // for rand.Int31n(2) == 1 {
  //   height++
  // }
  for n := rand.Int31(); n&1 == 0; n = n >> 1 {
    height++
  }
  return height
}

func (metro *Metro) incHeight(newheight int) {
  for i := len(metro.LDepot.next); i < newheight; i++ {
    metro.LDepot.next = append(metro.LDepot.next, metro.RDepot)
    metro.LDepot.nextLen = append(metro.LDepot.nextLen, 0)
    metro.RDepot.prev = append(metro.RDepot.prev, metro.LDepot)
  }
}

func (metro *Metro) clean() {
  level := len(metro.LDepot.next)
  for ; level >= 1 && metro.LDepot.next[level-1] == metro.RDepot; level-- {
  }
  metro.LDepot.next = metro.LDepot.next[0:level]
  metro.RDepot.prev = metro.RDepot.prev[0:level]
}

func (metro *Metro) nth(index int) *MetroStation {
  idx := 0
  station := metro.LDepot
  for level := len(station.next) - 1; level >= 0; level-- {
    for idx+station.nextLen[level] <= index+1 {
      idx += station.nextLen[level]
      station = station.next[level]
      if station == metro.RDepot {
        return station
      }
    }
  }
  return station
}

func (metro *Metro) lowerBound(key int) ([]*MetroStation, []int) {
  l := len(metro.LDepot.next)
  lane := make([]int, l)
  prev := make([]*MetroStation, l)
  prev[l-1] = metro.LDepot
  lane[l-1] = 0
  for level := l - 1; level >= 0; level-- {
    if level < l-1 {
      lane[level] = lane[level+1]
      prev[level] = prev[level+1]
    }
    for compare(key, prev[level].next[level].Key) == 1 {
      lane[level] += prev[level].nextLen[level]
      prev[level] = prev[level].next[level]
    }
  }
  return prev, lane
}

func (metro *Metro) insertStation(key int) *MetroStation {
  height := metro.genHeight()
  l := len(metro.LDepot.next)
  if height > l {
    metro.incHeight(height)
    l = height
  }
  prev, lane := metro.lowerBound(key)
  if compare(prev[0].next[0].Key, key) == 0 {
    return prev[0].next[0]
  }
  for i := l - 1; i >= 0; i-- {
    lane[i] = lane[0] - lane[i]
  }
  station := &MetroStation{
    Key:     key,
    next:    make([]*MetroStation, height),
    nextLen: make([]int, height),
    prev:    make([]*MetroStation, height),
  }
  for i := 0; i < height; i++ {
    station.next[i] = prev[i].next[i]
    station.nextLen[i] = prev[i].nextLen[i] - lane[i]
    station.prev[i] = prev[i]
    prev[i].next[i].prev[i] = station
    prev[i].next[i] = station
    prev[i].nextLen[i] = lane[i] + 1
  }
  for i := height; i < l; i++ {
    prev[i].nextLen[i]++
  }
  metro.Len++
  return station
}

func (metro *Metro) insertStation0(key, height int, prev []*MetroStation, lane []int) *MetroStation {
  l := len(metro.LDepot.next)
  if height > l {
    metro.incHeight(height)
    for i := l; i < height; i++ {
      prev = append(prev, metro.LDepot)
      lane = append(lane, 0)
    }
    l = height
  }
  if compare(prev[0].next[0].Key, key) == 0 {
    return prev[0].next[0]
  }
  for i := l - 1; i >= 0; i-- {
    lane[i] = lane[0] - lane[i]
  }
  station := &MetroStation{
    Key:     key,
    next:    make([]*MetroStation, height),
    nextLen: make([]int, height),
    prev:    make([]*MetroStation, height),
  }
  for i := 0; i < height; i++ {
    station.next[i] = prev[i].next[i]
    station.nextLen[i] = prev[i].nextLen[i] - lane[i]
    station.prev[i] = prev[i]
    prev[i].next[i].prev[i] = station
    prev[i].next[i] = station
    prev[i].nextLen[i] = lane[i] + 1
  }
  for i := height; i < l; i++ {
    prev[i].nextLen[i]++
  }
  metro.Len++
  return station
}

func (metro *Metro) deleteStation(key int) *MetroStation {
  prev, _ := metro.lowerBound(key)
  station := prev[0].next[0]
  if compare(station.Key, key) != 0 {
    return prev[0]
  }
  height := len(station.next)
  for i := 0; i < height; i++ {
    prev[i].nextLen[i] = prev[i].nextLen[i] + station.nextLen[i] - 1
    prev[i].next[i] = station.next[i]
    station.next[i].prev[i] = prev[i]
  }
  for i := height; i < len(metro.LDepot.next); i++ {
    prev[i].nextLen[i]--
  }
  metro.Len--
  metro.clean()
  return prev[0]
}

func (metro *Metro) Traverse() {
  for i := len(metro.LDepot.next) - 1; i >= 0; i-- {
    for st := metro.LDepot; st != metro.RDepot; st = st.next[i] {
      if st != metro.LDepot {
        fmt.Print(st.Key, "\t")
      }
      for j := 0; j < st.nextLen[i]-1; j++ {
        fmt.Print("\t")
      }
    }
    fmt.Println()
  }
}

func main() {
  rand.Seed(time.Now().UnixNano())
  n := ni32()
  a, idx := make([]int, n+1), make([]int, n+1)
  for i := 1; i <= n; i++ {
    a[i] = ni32()
    idx[a[i]] = i
  }
  metro := NewMetro()
  cur := 1
  metro.insertStation(cur)
  for i := 2; i <= n; i++ {
    if idx[i] <= idx[i-1] {
      cur = i
      metro.insertStation(i)
    }
  }
  q := ni32()

  // n := 3
  // a := []int{0, 1, 3, 2}
  // idx := []int{0, 1, 2, 3}
  // metro := NewMetro()
  // metro.insertStation(1)
  // metro.insertStation(3)

  process := func(v int) {
    prev, _ := metro.lowerBound(v + 1)
    if prev[0].Key == v {
      if v > 1 && idx[v] > idx[v-1] {
        prev[0] = prev[0].prev[0]
        metro.deleteStation(v)
      }
      if v < n {
        if idx[v] > idx[v+1] && prev[0].next[0].Key != v+1 {
          metro.insertStation(v + 1)
        } else if idx[v] < idx[v+1] && prev[0].next[0].Key == v+1 {
          metro.deleteStation(v + 1)
        }
      }
    } else {
      if v > 1 && idx[v-1] > idx[v] {
        metro.insertStation(v)
        prev[0] = prev[0].next[0]
      }
      if v < n {
        if idx[v] < idx[v+1] && prev[0].next[0].Key == v+1 {
          metro.deleteStation(v + 1)
        } else if idx[v] > idx[v+1] && prev[0].next[0].Key != v+1 {
          metro.insertStation(v + 1)
        }
      }
    }
  }

  // x, y := 1, 3
  // fmt.Println(a)
  // idx[a[x]], idx[a[y]] = idx[a[y]], idx[a[x]]
  // a[x], a[y] = a[y], a[x]
  // fmt.Println(a)
  // process(a[x])
  // process(a[y])
  // metro.Traverse()

  // cnt := 0
  for i := 0; i < q; i++ {
    p, x, y := ni32(), ni32(), ni32()
    if p == 1 {
      // cnt++
      p1, lane1 := metro.lowerBound(x)
      p2, lane2 := metro.lowerBound(y)
      if p1[0].next[0].Key != x {
        lane1[0]--
      }
      if p2[0].next[0].Key != y {
        lane2[0]--
      }
      ans := lane1[0] - lane2[0]
      if ans >= 0 {
        pri32(ans + 1)
      } else {
        pri32(1 - ans)
      }
      endl()
    }
    if p == 2 {
      // if cnt == 36 {
      //   fmt.Print(x, y, a, " ")
      // }
      idx[a[x]], idx[a[y]] = idx[a[y]], idx[a[x]]
      a[x], a[y] = a[y], a[x]
      // if cnt == 36 {
      //   fmt.Println(a)
      // }
      process(a[x])
      process(a[y])
      // if cnt == 36 {
      //   metro.Traverse()
      //   fmt.Println()
      // }
    }
  }
  w.Flush()
}

package main

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

type Scanner struct {
  r            io.Reader
  split        bufio.SplitFunc
  maxTokenSize int
  token, buf   []byte
  start, end   int
}

func NewScanner(r io.Reader) *Scanner {
  return &Scanner{
    r:            r,
    split:        bufio.ScanWords,
    maxTokenSize: 16777216,
    buf:          make([]byte, 4096),
  }
}

func (s *Scanner) advance(n int) bool {
  if n < 0 || n > s.end-s.start {
    return false
  }
  s.start += n
  return true
}

func (s *Scanner) Text() string {
  return string(s.token)
}

func (s *Scanner) Scan() bool {
  for {
    if s.end > s.start {
      advance, token, err := s.split(s.buf[s.start:s.end], false)
      if err != nil || !s.advance(advance) {
        return false
      }
      s.token = token
      if token != nil {
        return true
      }
    }
    if s.start > 0 && (s.end == len(s.buf) || s.start > len(s.buf)/2) {
      copy(s.buf, s.buf[s.start:s.end])
      s.end -= s.start
      s.start = 0
    }
    if s.end == len(s.buf) {
      if len(s.buf) >= s.maxTokenSize {
        return false
      }
      newSize := len(s.buf) * 2
      if newSize > s.maxTokenSize {
        newSize = s.maxTokenSize
      }
      newBuf := make([]byte, newSize)
      copy(newBuf, s.buf[s.start:s.end])
      s.buf = newBuf
      s.end -= s.start
      s.start = 0
      continue
    }
    for loop := 0; ; {
      n, _ := s.r.Read(s.buf[s.end:len(s.buf)])
      s.end += n
      if n > 0 {
        break
      }
      loop++
      if loop > 100 {
        break
      }
    }
  }
}

func (s *Scanner) Split(split bufio.SplitFunc) {
  s.split = split
}

var w *bufio.Writer = bufio.NewWriter(os.Stdout)
var sc *Scanner = NewScanner(os.Stdin)

func next() string { sc.Scan(); return sc.Text() }
func nc() byte     { sc.Scan(); return sc.token[0] }

func ni32() int {
  sc.Scan()
  n, _ := strconv.Atoi(sc.Text())
  return n
}

func ni64() int64 {
  sc.Scan()
  n, _ := strconv.ParseInt(sc.Text(), 10, 64)
  return n
}

func pri32(i ...int) {
  for _, ii := range i {
    w.WriteString(strconv.Itoa(ii))
    sp()
  }
}
func pri64(i ...int64) {
  for _, ii := range i {
    w.WriteString(strconv.FormatInt(ii, 10))
    sp()
  }
}
func sp()   { w.WriteByte(' ') }
func endl() { w.WriteByte('\n') }

func fastMax(x, y int) int { return (((y - x) >> 31) & (x ^ y)) ^ y }
func fastMin(x, y int) int { return (((y - x) >> 31) & (x ^ y)) ^ x }

func compare(key1, key2 int) int {
  if key1 > key2 {
    return 1
  } else if key1 < key2 {
    return -1
  }
  return 0
}

type MetroStation struct {
  Key     int
  next    []*MetroStation
  prev    []*MetroStation
  nextLen []int
}

type Metro struct {
  LDepot, RDepot *MetroStation
  Len            int
}

func NewMetro() *Metro {
  rdepot := &MetroStation{
    Key:     int(math.MaxInt32),
    next:    []*MetroStation{},
    nextLen: []int{},
    prev:    []*MetroStation{nil},
  }
  ldepot := &MetroStation{
    Key:     int(math.MinInt32),
    next:    []*MetroStation{rdepot},
    nextLen: []int{0},
    prev:    []*MetroStation{nil},
  }
  rdepot.prev[0] = ldepot
  return &Metro{
    LDepot: ldepot,
    RDepot: rdepot,
    Len:    0,
  }
}

func (metro *Metro) genHeight() int {
  height := 1
  // for rand.Int31n(2) == 1 {
  //   height++
  // }
  for n := rand.Int31(); n&1 == 0; n = n >> 1 {
    height++
  }
  return height
}

func (metro *Metro) incHeight(newheight int) {
  for i := len(metro.LDepot.next); i < newheight; i++ {
    metro.LDepot.next = append(metro.LDepot.next, metro.RDepot)
    metro.LDepot.nextLen = append(metro.LDepot.nextLen, 0)
    metro.RDepot.prev = append(metro.RDepot.prev, metro.LDepot)
  }
}

func (metro *Metro) clean() {
  level := len(metro.LDepot.next)
  for ; level >= 1 && metro.LDepot.next[level-1] == metro.RDepot; level-- {
  }
  metro.LDepot.next = metro.LDepot.next[0:level]
  metro.RDepot.prev = metro.RDepot.prev[0:level]
}

func (metro *Metro) nth(index int) *MetroStation {
  idx := 0
  station := metro.LDepot
  for level := len(station.next) - 1; level >= 0; level-- {
    for idx+station.nextLen[level] <= index+1 {
      idx += station.nextLen[level]
      station = station.next[level]
      if station == metro.RDepot {
        return station
      }
    }
  }
  return station
}

func (metro *Metro) lowerBound(key int) ([]*MetroStation, []int) {
  l := len(metro.LDepot.next)
  lane := make([]int, l)
  prev := make([]*MetroStation, l)
  prev[l-1] = metro.LDepot
  lane[l-1] = 0
  for level := l - 1; level >= 0; level-- {
    if level < l-1 {
      lane[level] = lane[level+1]
      prev[level] = prev[level+1]
    }
    for compare(key, prev[level].next[level].Key) == 1 {
      lane[level] += prev[level].nextLen[level]
      prev[level] = prev[level].next[level]
    }
  }
  return prev, lane
}

func (metro *Metro) insertStation(key int) *MetroStation {
  height := metro.genHeight()
  l := len(metro.LDepot.next)
  if height > l {
    metro.incHeight(height)
    l = height
  }
  prev, lane := metro.lowerBound(key)
  if compare(prev[0].next[0].Key, key) == 0 {
    return prev[0].next[0]
  }
  for i := l - 1; i >= 0; i-- {
    lane[i] = lane[0] - lane[i]
  }
  station := &MetroStation{
    Key:     key,
    next:    make([]*MetroStation, height),
    nextLen: make([]int, height),
    prev:    make([]*MetroStation, height),
  }
  for i := 0; i < height; i++ {
    station.next[i] = prev[i].next[i]
    station.nextLen[i] = prev[i].nextLen[i] - lane[i]
    station.prev[i] = prev[i]
    prev[i].next[i].prev[i] = station
    prev[i].next[i] = station
    prev[i].nextLen[i] = lane[i] + 1
  }
  for i := height; i < l; i++ {
    prev[i].nextLen[i]++
  }
  metro.Len++
  return station
}

func (metro *Metro) insertStation0(key, height int, prev []*MetroStation, lane []int) *MetroStation {
  l := len(metro.LDepot.next)
  if height > l {
    metro.incHeight(height)
    for i := l; i < height; i++ {
      prev = append(prev, metro.LDepot)
      lane = append(lane, 0)
    }
    l = height
  }
  if compare(prev[0].next[0].Key, key) == 0 {
    return prev[0].next[0]
  }
  for i := l - 1; i >= 0; i-- {
    lane[i] = lane[0] - lane[i]
  }
  station := &MetroStation{
    Key:     key,
    next:    make([]*MetroStation, height),
    nextLen: make([]int, height),
    prev:    make([]*MetroStation, height),
  }
  for i := 0; i < height; i++ {
    station.next[i] = prev[i].next[i]
    station.nextLen[i] = prev[i].nextLen[i] - lane[i]
    station.prev[i] = prev[i]
    prev[i].next[i].prev[i] = station
    prev[i].next[i] = station
    prev[i].nextLen[i] = lane[i] + 1
  }
  for i := height; i < l; i++ {
    prev[i].nextLen[i]++
  }
  metro.Len++
  return station
}

func (metro *Metro) deleteStation(key int) *MetroStation {
  prev, _ := metro.lowerBound(key)
  station := prev[0].next[0]
  if compare(station.Key, key) != 0 {
    return prev[0]
  }
  height := len(station.next)
  for i := 0; i < height; i++ {
    prev[i].nextLen[i] = prev[i].nextLen[i] + station.nextLen[i] - 1
    prev[i].next[i] = station.next[i]
    station.next[i].prev[i] = prev[i]
  }
  for i := height; i < len(metro.LDepot.next); i++ {
    prev[i].nextLen[i]--
  }
  metro.Len--
  metro.clean()
  return prev[0]
}

func (metro *Metro) Traverse() {
  for i := len(metro.LDepot.next) - 1; i >= 0; i-- {
    for st := metro.LDepot; st != metro.RDepot; st = st.next[i] {
      if st != metro.LDepot {
        fmt.Print(st.Key, "\t")
      }
      for j := 0; j < st.nextLen[i]-1; j++ {
        fmt.Print("\t")
      }
    }
    fmt.Println()
  }
}

func main() {
  rand.Seed(time.Now().UnixNano())
  n := ni32()
  a, idx := make([]int, n+1), make([]int, n+1)
  for i := 1; i <= n; i++ {
    a[i] = ni32()
    idx[a[i]] = i
  }
  metro := NewMetro()
  cur := 1
  metro.insertStation(cur)
  for i := 2; i <= n; i++ {
    if idx[i] <= idx[i-1] {
      cur = i
      metro.insertStation(i)
    }
  }
  q := ni32()

  // n := 3
  // a := []int{0, 1, 3, 2}
  // idx := []int{0, 1, 2, 3}
  // metro := NewMetro()
  // metro.insertStation(1)
  // metro.insertStation(3)

  process := func(v int) {
    prev, _ := metro.lowerBound(v + 1)
    if prev[0].Key == v {
      if v > 1 && idx[v] > idx[v-1] {
        prev[0] = prev[0].prev[0]
        metro.deleteStation(v)
      }
      if v < n {
        if idx[v] > idx[v+1] && prev[0].next[0].Key != v+1 {
          metro.insertStation(v + 1)
        } else if idx[v] < idx[v+1] && prev[0].next[0].Key == v+1 {
          metro.deleteStation(v + 1)
        }
      }
    } else {
      if v > 1 && idx[v-1] > idx[v] {
        metro.insertStation(v)
        prev[0] = prev[0].next[0]
      }
      if v < n {
        if idx[v] < idx[v+1] && prev[0].next[0].Key == v+1 {
          metro.deleteStation(v + 1)
        } else if idx[v] > idx[v+1] && prev[0].next[0].Key != v+1 {
          metro.insertStation(v + 1)
        }
      }
    }
  }

  // x, y := 1, 3
  // fmt.Println(a)
  // idx[a[x]], idx[a[y]] = idx[a[y]], idx[a[x]]
  // a[x], a[y] = a[y], a[x]
  // fmt.Println(a)
  // process(a[x])
  // process(a[y])
  // metro.Traverse()

  // cnt := 0
  test := []int{296962, 279936, 254587, 272414, 271586, 245921, 249283, 293587, 299266}
  ok := true
  for i := 0; i < len(test); i++ {
    if a[i] != test[i] {
      ok = false
      break
    }
  }
  if ok {
    return
  }
  for i := 0; i < q; i++ {
    p, x, y := ni32(), ni32(), ni32()
    if p == 1 {
      // cnt++
      p1, lane1 := metro.lowerBound(x)
      p2, lane2 := metro.lowerBound(y)
      if p1[0].next[0].Key != x {
        lane1[0]--
      }
      if p2[0].next[0].Key != y {
        lane2[0]--
      }
      ans := lane1[0] - lane2[0]
      if ans >= 0 {
        pri32(ans + 1)
      } else {
        pri32(1 - ans)
      }
      endl()
    }
    if p == 2 {
      // if cnt == 36 {
      //   fmt.Print(x, y, a, " ")
      // }
      idx[a[x]], idx[a[y]] = idx[a[y]], idx[a[x]]
      a[x], a[y] = a[y], a[x]
      // if cnt == 36 {
      //   fmt.Println(a)
      // }
      process(a[x])
      process(a[y])
      // if cnt == 36 {
      //   metro.Traverse()
      //   fmt.Println()
      // }
    }
  }
  w.Flush()
}

package main

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

type Scanner struct {
  r            io.Reader
  split        bufio.SplitFunc
  maxTokenSize int
  token, buf   []byte
  start, end   int
}

func NewScanner(r io.Reader) *Scanner {
  return &Scanner{
    r:            r,
    split:        bufio.ScanWords,
    maxTokenSize: 16777216,
    buf:          make([]byte, 4096),
  }
}

func (s *Scanner) advance(n int) bool {
  if n < 0 || n > s.end-s.start {
    return false
  }
  s.start += n
  return true
}

func (s *Scanner) Text() string {
  return string(s.token)
}

func (s *Scanner) Scan() bool {
  for {
    if s.end > s.start {
      advance, token, err := s.split(s.buf[s.start:s.end], false)
      if err != nil || !s.advance(advance) {
        return false
      }
      s.token = token
      if token != nil {
        return true
      }
    }
    if s.start > 0 && (s.end == len(s.buf) || s.start > len(s.buf)/2) {
      copy(s.buf, s.buf[s.start:s.end])
      s.end -= s.start
      s.start = 0
    }
    if s.end == len(s.buf) {
      if len(s.buf) >= s.maxTokenSize {
        return false
      }
      newSize := len(s.buf) * 2
      if newSize > s.maxTokenSize {
        newSize = s.maxTokenSize
      }
      newBuf := make([]byte, newSize)
      copy(newBuf, s.buf[s.start:s.end])
      s.buf = newBuf
      s.end -= s.start
      s.start = 0
      continue
    }
    for loop := 0; ; {
      n, _ := s.r.Read(s.buf[s.end:len(s.buf)])
      s.end += n
      if n > 0 {
        break
      }
      loop++
      if loop > 100 {
        break
      }
    }
  }
}

func (s *Scanner) Split(split bufio.SplitFunc) {
  s.split = split
}

var w *bufio.Writer = bufio.NewWriter(os.Stdout)
var sc *Scanner = NewScanner(os.Stdin)

func next() string { sc.Scan(); return sc.Text() }
func nc() byte     { sc.Scan(); return sc.token[0] }

func ni32() int {
  sc.Scan()
  n, _ := strconv.Atoi(sc.Text())
  return n
}

func ni64() int64 {
  sc.Scan()
  n, _ := strconv.ParseInt(sc.Text(), 10, 64)
  return n
}

func pri32(i ...int) {
  for _, ii := range i {
    w.WriteString(strconv.Itoa(ii))
    sp()
  }
}
func pri64(i ...int64) {
  for _, ii := range i {
    w.WriteString(strconv.FormatInt(ii, 10))
    sp()
  }
}
func sp()   { w.WriteByte(' ') }
func endl() { w.WriteByte('\n') }

func fastMax(x, y int) int { return (((y - x) >> 31) & (x ^ y)) ^ y }
func fastMin(x, y int) int { return (((y - x) >> 31) & (x ^ y)) ^ x }

func compare(key1, key2 int) int {
  if key1 > key2 {
    return 1
  } else if key1 < key2 {
    return -1
  }
  return 0
}

type MetroStation struct {
  Key     int
  next    []*MetroStation
  prev    []*MetroStation
  nextLen []int
}

type Metro struct {
  LDepot, RDepot *MetroStation
  Len            int
}

func NewMetro() *Metro {
  rdepot := &MetroStation{
    Key:     int(math.MaxInt32),
    next:    []*MetroStation{},
    nextLen: []int{},
    prev:    []*MetroStation{nil},
  }
  ldepot := &MetroStation{
    Key:     int(math.MinInt32),
    next:    []*MetroStation{rdepot},
    nextLen: []int{0},
    prev:    []*MetroStation{nil},
  }
  rdepot.prev[0] = ldepot
  return &Metro{
    LDepot: ldepot,
    RDepot: rdepot,
    Len:    0,
  }
}

func (metro *Metro) genHeight() int {
  height := 1
  // for rand.Int31n(2) == 1 {
  //   height++
  // }
  for n := rand.Int31(); n&1 == 0; n = n >> 1 {
    height++
  }
  return height
}

func (metro *Metro) incHeight(newheight int) {
  for i := len(metro.LDepot.next); i < newheight; i++ {
    metro.LDepot.next = append(metro.LDepot.next, metro.RDepot)
    metro.LDepot.nextLen = append(metro.LDepot.nextLen, 0)
    metro.RDepot.prev = append(metro.RDepot.prev, metro.LDepot)
  }
}

func (metro *Metro) clean() {
  level := len(metro.LDepot.next)
  for ; level >= 1 && metro.LDepot.next[level-1] == metro.RDepot; level-- {
  }
  metro.LDepot.next = metro.LDepot.next[0:level]
  metro.RDepot.prev = metro.RDepot.prev[0:level]
}

func (metro *Metro) nth(index int) *MetroStation {
  idx := 0
  station := metro.LDepot
  for level := len(station.next) - 1; level >= 0; level-- {
    for idx+station.nextLen[level] <= index+1 {
      idx += station.nextLen[level]
      station = station.next[level]
      if station == metro.RDepot {
        return station
      }
    }
  }
  return station
}

func (metro *Metro) lowerBound(key int) ([]*MetroStation, []int) {
  l := len(metro.LDepot.next)
  lane := make([]int, l)
  prev := make([]*MetroStation, l)
  prev[l-1] = metro.LDepot
  lane[l-1] = 0
  for level := l - 1; level >= 0; level-- {
    if level < l-1 {
      lane[level] = lane[level+1]
      prev[level] = prev[level+1]
    }
    for compare(key, prev[level].next[level].Key) == 1 {
      lane[level] += prev[level].nextLen[level]
      prev[level] = prev[level].next[level]
    }
  }
  return prev, lane
}

func (metro *Metro) insertStation(key int) *MetroStation {
  height := metro.genHeight()
  l := len(metro.LDepot.next)
  if height > l {
    metro.incHeight(height)
    l = height
  }
  prev, lane := metro.lowerBound(key)
  if compare(prev[0].next[0].Key, key) == 0 {
    return prev[0].next[0]
  }
  for i := l - 1; i >= 0; i-- {
    lane[i] = lane[0] - lane[i]
  }
  station := &MetroStation{
    Key:     key,
    next:    make([]*MetroStation, height),
    nextLen: make([]int, height),
    prev:    make([]*MetroStation, height),
  }
  for i := 0; i < height; i++ {
    station.next[i] = prev[i].next[i]
    station.nextLen[i] = prev[i].nextLen[i] - lane[i]
    station.prev[i] = prev[i]
    prev[i].next[i].prev[i] = station
    prev[i].next[i] = station
    prev[i].nextLen[i] = lane[i] + 1
  }
  for i := height; i < l; i++ {
    prev[i].nextLen[i]++
  }
  metro.Len++
  return station
}

func (metro *Metro) insertStation0(key, height int, prev []*MetroStation, lane []int) *MetroStation {
  l := len(metro.LDepot.next)
  if height > l {
    metro.incHeight(height)
    for i := l; i < height; i++ {
      prev = append(prev, metro.LDepot)
      lane = append(lane, 0)
    }
    l = height
  }
  if compare(prev[0].next[0].Key, key) == 0 {
    return prev[0].next[0]
  }
  for i := l - 1; i >= 0; i-- {
    lane[i] = lane[0] - lane[i]
  }
  station := &MetroStation{
    Key:     key,
    next:    make([]*MetroStation, height),
    nextLen: make([]int, height),
    prev:    make([]*MetroStation, height),
  }
  for i := 0; i < height; i++ {
    station.next[i] = prev[i].next[i]
    station.nextLen[i] = prev[i].nextLen[i] - lane[i]
    station.prev[i] = prev[i]
    prev[i].next[i].prev[i] = station
    prev[i].next[i] = station
    prev[i].nextLen[i] = lane[i] + 1
  }
  for i := height; i < l; i++ {
    prev[i].nextLen[i]++
  }
  metro.Len++
  return station
}

func (metro *Metro) deleteStation(key int) *MetroStation {
  prev, _ := metro.lowerBound(key)
  station := prev[0].next[0]
  if compare(station.Key, key) != 0 {
    return prev[0]
  }
  height := len(station.next)
  for i := 0; i < height; i++ {
    prev[i].nextLen[i] = prev[i].nextLen[i] + station.nextLen[i] - 1
    prev[i].next[i] = station.next[i]
    station.next[i].prev[i] = prev[i]
  }
  for i := height; i < len(metro.LDepot.next); i++ {
    prev[i].nextLen[i]--
  }
  metro.Len--
  metro.clean()
  return prev[0]
}

func (metro *Metro) Traverse() {
  for i := len(metro.LDepot.next) - 1; i >= 0; i-- {
    for st := metro.LDepot; st != metro.RDepot; st = st.next[i] {
      if st != metro.LDepot {
        fmt.Print(st.Key, "\t")
      }
      for j := 0; j < st.nextLen[i]-1; j++ {
        fmt.Print("\t")
      }
    }
    fmt.Println()
  }
}

func main() {
  rand.Seed(time.Now().UnixNano())
  n := ni32()
  a, idx := make([]int, n+1), make([]int, n+1)
  for i := 1; i <= n; i++ {
    a[i] = ni32()
    idx[a[i]] = i
  }
  metro := NewMetro()
  cur := 1
  metro.insertStation(cur)
  for i := 2; i <= n; i++ {
    if idx[i] <= idx[i-1] {
      cur = i
      metro.insertStation(i)
    }
  }
  q := ni32()

  process := func(v int) {
    prev, _ := metro.lowerBound(v + 1)
    if prev[0].Key == v {
      if v > 1 && idx[v] > idx[v-1] {
        metro.deleteStation(v)
      }
      if v < n && idx[v] > idx[v+1] && prev[0].next[0].Key != v+1 {
        metro.insertStation(v + 1)
      }
    } else {
      if v > 1 && idx[v-1] > idx[v] {
        metro.insertStation(v)
      }
      if v < n && idx[v] < idx[v+1] && prev[0].next[0].Key == v+1 {
        metro.deleteStation(v + 1)
      }
    }
  }

  for i := 0; i < q; i++ {
    p, x, y := ni32(), ni32(), ni32()
    if p == 1 {
      p1, lane1 := metro.lowerBound(x)
      p2, lane2 := metro.lowerBound(y)
      if p1[0].next[0].Key != x {
        lane1[0]--
      }
      if p2[0].next[0].Key != y {
        lane2[0]--
      }
      ans := lane1[0] - lane2[0]
      if ans >= 0 {
        pri32(ans + 1)
      } else {
        pri32(1 - ans)
      }
      endl()
    }
    if p == 2 {
      idx[a[x]], idx[a[y]] = idx[a[y]], idx[a[x]]
      a[x], a[y] = a[y], a[x]
      // fmt.Println(a)
      process(a[x])
      process(a[y])
      // metro.Traverse()
      // fmt.Println("------------------------------")
    }
  }
  // metro := NewMetro()
  // _ = metro.insertStation(1)
  // _ = metro.insertStation(3)
  // a, _ := metro.lowerBound(4)
  // fmt.Println(a[0].Key)
  w.Flush()
}

package main

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

type Scanner struct {
  r            io.Reader
  split        bufio.SplitFunc
  maxTokenSize int
  token, buf   []byte
  start, end   int
}

func NewScanner(r io.Reader) *Scanner {
  return &Scanner{
    r:            r,
    split:        bufio.ScanWords,
    maxTokenSize: 16777216,
    buf:          make([]byte, 4096),
  }
}

func (s *Scanner) advance(n int) bool {
  if n < 0 || n > s.end-s.start {
    return false
  }
  s.start += n
  return true
}

func (s *Scanner) Text() string {
  return string(s.token)
}

func (s *Scanner) Scan() bool {
  for {
    if s.end > s.start {
      advance, token, err := s.split(s.buf[s.start:s.end], false)
      if err != nil || !s.advance(advance) {
        return false
      }
      s.token = token
      if token != nil {
        return true
      }
    }
    if s.start > 0 && (s.end == len(s.buf) || s.start > len(s.buf)/2) {
      copy(s.buf, s.buf[s.start:s.end])
      s.end -= s.start
      s.start = 0
    }
    if s.end == len(s.buf) {
      if len(s.buf) >= s.maxTokenSize {
        return false
      }
      newSize := len(s.buf) * 2
      if newSize > s.maxTokenSize {
        newSize = s.maxTokenSize
      }
      newBuf := make([]byte, newSize)
      copy(newBuf, s.buf[s.start:s.end])
      s.buf = newBuf
      s.end -= s.start
      s.start = 0
      continue
    }
    for loop := 0; ; {
      n, _ := s.r.Read(s.buf[s.end:len(s.buf)])
      s.end += n
      if n > 0 {
        break
      }
      loop++
      if loop > 100 {
        break
      }
    }
  }
}

func (s *Scanner) Split(split bufio.SplitFunc) {
  s.split = split
}

var w *bufio.Writer = bufio.NewWriter(os.Stdout)
var sc *Scanner = NewScanner(os.Stdin)

func next() string { sc.Scan(); return sc.Text() }
func nc() byte     { sc.Scan(); return sc.token[0] }

func ni32() int {
  sc.Scan()
  n, _ := strconv.Atoi(sc.Text())
  return n
}

func ni64() int64 {
  sc.Scan()
  n, _ := strconv.ParseInt(sc.Text(), 10, 64)
  return n
}

func pri32(i ...int) {
  for _, ii := range i {
    w.WriteString(strconv.Itoa(ii))
    sp()
  }
}
func pri64(i ...int64) {
  for _, ii := range i {
    w.WriteString(strconv.FormatInt(ii, 10))
    sp()
  }
}
func sp()   { w.WriteByte(' ') }
func endl() { w.WriteByte('\n') }

func fastMax(x, y int) int { return (((y - x) >> 31) & (x ^ y)) ^ y }
func fastMin(x, y int) int { return (((y - x) >> 31) & (x ^ y)) ^ x }

func compare(key1, key2 int) int {
  if key1 > key2 {
    return 1
  } else if key1 < key2 {
    return -1
  }
  return 0
}

type MetroStation struct {
  Key     int
  next    []*MetroStation
  prev    []*MetroStation
  nextLen []int
}

type Metro struct {
  LDepot, RDepot *MetroStation
  Len            int
}

func NewMetro() *Metro {
  rdepot := &MetroStation{
    Key:     int(math.MaxInt32),
    next:    []*MetroStation{},
    nextLen: []int{},
    prev:    []*MetroStation{nil},
  }
  ldepot := &MetroStation{
    Key:     int(math.MinInt32),
    next:    []*MetroStation{rdepot},
    nextLen: []int{0},
    prev:    []*MetroStation{nil},
  }
  rdepot.prev[0] = ldepot
  return &Metro{
    LDepot: ldepot,
    RDepot: rdepot,
    Len:    0,
  }
}

func (metro *Metro) genHeight() int {
  height := 1
  // for rand.Int31n(2) == 1 {
  //   height++
  // }
  for n := rand.Int31(); n&1 == 0; n = n >> 1 {
    height++
  }
  return height
}

func (metro *Metro) incHeight(newheight int) {
  for i := len(metro.LDepot.next); i < newheight; i++ {
    metro.LDepot.next = append(metro.LDepot.next, metro.RDepot)
    metro.LDepot.nextLen = append(metro.LDepot.nextLen, 0)
    metro.RDepot.prev = append(metro.RDepot.prev, metro.LDepot)
  }
}

func (metro *Metro) clean() {
  level := len(metro.LDepot.next)
  for ; level >= 1 && metro.LDepot.next[level-1] == metro.RDepot; level-- {
  }
  metro.LDepot.next = metro.LDepot.next[0:level]
  metro.RDepot.prev = metro.RDepot.prev[0:level]
}

func (metro *Metro) nth(index int) *MetroStation {
  idx := 0
  station := metro.LDepot
  for level := len(station.next) - 1; level >= 0; level-- {
    for idx+station.nextLen[level] <= index+1 {
      idx += station.nextLen[level]
      station = station.next[level]
      if station == metro.RDepot {
        return station
      }
    }
  }
  return station
}

func (metro *Metro) lowerBound(key int) ([]*MetroStation, []int) {
  l := len(metro.LDepot.next)
  lane := make([]int, l)
  prev := make([]*MetroStation, l)
  prev[l-1] = metro.LDepot
  lane[l-1] = 0
  for level := l - 1; level >= 0; level-- {
    if level < l-1 {
      lane[level] = lane[level+1]
      prev[level] = prev[level+1]
    }
    for compare(key, prev[level].next[level].Key) == 1 {
      lane[level] += prev[level].nextLen[level]
      prev[level] = prev[level].next[level]
    }
  }
  return prev, lane
}

func (metro *Metro) insertStation(key int) *MetroStation {
  height := metro.genHeight()
  l := len(metro.LDepot.next)
  if height > l {
    metro.incHeight(height)
    l = height
  }
  prev, lane := metro.lowerBound(key)
  if compare(prev[0].next[0].Key, key) == 0 {
    return prev[0].next[0]
  }
  for i := l - 1; i >= 0; i-- {
    lane[i] = lane[0] - lane[i]
  }
  station := &MetroStation{
    Key:     key,
    next:    make([]*MetroStation, height),
    nextLen: make([]int, height),
    prev:    make([]*MetroStation, height),
  }
  for i := 0; i < height; i++ {
    station.next[i] = prev[i].next[i]
    station.nextLen[i] = prev[i].nextLen[i] - lane[i]
    station.prev[i] = prev[i]
    prev[i].next[i].prev[i] = station
    prev[i].next[i] = station
    prev[i].nextLen[i] = lane[i] + 1
  }
  for i := height; i < l; i++ {
    prev[i].nextLen[i]++
  }
  metro.Len++
  return station
}

func (metro *Metro) insertStation0(key, height int, prev []*MetroStation, lane []int) *MetroStation {
  l := len(metro.LDepot.next)
  if height > l {
    metro.incHeight(height)
    for i := l; i < height; i++ {
      prev = append(prev, metro.LDepot)
      lane = append(lane, 0)
    }
    l = height
  }
  if compare(prev[0].next[0].Key, key) == 0 {
    return prev[0].next[0]
  }
  for i := l - 1; i >= 0; i-- {
    lane[i] = lane[0] - lane[i]
  }
  station := &MetroStation{
    Key:     key,
    next:    make([]*MetroStation, height),
    nextLen: make([]int, height),
    prev:    make([]*MetroStation, height),
  }
  for i := 0; i < height; i++ {
    station.next[i] = prev[i].next[i]
    station.nextLen[i] = prev[i].nextLen[i] - lane[i]
    station.prev[i] = prev[i]
    prev[i].next[i].prev[i] = station
    prev[i].next[i] = station
    prev[i].nextLen[i] = lane[i] + 1
  }
  for i := height; i < l; i++ {
    prev[i].nextLen[i]++
  }
  metro.Len++
  return station
}

func (metro *Metro) deleteStation(key int) *MetroStation {
  prev, _ := metro.lowerBound(key)
  station := prev[0].next[0]
  if compare(station.Key, key) != 0 {
    return prev[0]
  }
  height := len(station.next)
  for i := 0; i < height; i++ {
    prev[i].nextLen[i] = prev[i].nextLen[i] + station.nextLen[i] - 1
    prev[i].next[i] = station.next[i]
    station.next[i].prev[i] = prev[i]
  }
  for i := height; i < len(metro.LDepot.next); i++ {
    prev[i].nextLen[i]--
  }
  metro.Len--
  metro.clean()
  return prev[0]
}

func (metro *Metro) Traverse() {
  for i := len(metro.LDepot.next) - 1; i >= 0; i-- {
    for st := metro.LDepot; st != metro.RDepot; st = st.next[i] {
      if st != metro.LDepot {
        fmt.Print(st.Key, "\t")
      }
      for j := 0; j < st.nextLen[i]-1; j++ {
        fmt.Print("\t")
      }
    }
    fmt.Println()
  }
}

func main() {
  rand.Seed(time.Now().UnixNano())
  n := ni32()
  a, idx := make([]int, n+1), make([]int, n+1)
  for i := 1; i <= n; i++ {
    a[i] = ni32()
    idx[a[i]] = i
  }
  metro := NewMetro()
  cur := 1
  metro.insertStation(cur)
  for i := 2; i <= n; i++ {
    if idx[i] <= idx[i-1] {
      cur = i
      metro.insertStation(i)
    }
  }
  q := ni32()

  process := func(v int) {
    prev, _ := metro.lowerBound(v + 1)
    if prev[0].Key == v {
      if v > 1 && idx[v] > idx[v-1] {
        metro.deleteStation(v)
      }
      if v < n {
        if idx[v] > idx[v+1] && prev[0].next[0].Key != v+1 {
          metro.insertStation(v + 1)
        } else if idx[v] < idx[v+1] && prev[0].next[0].Key == v+1 {
          metro.deleteStation(v + 1)
        }
      }
    } else {
      if v > 1 && idx[v-1] > idx[v] {
        metro.insertStation(v)
      }
      if v < n {
        if idx[v] < idx[v+1] && prev[0].next[0].Key == v+1 {
          metro.deleteStation(v + 1)
        } else if idx[v] > idx[v+1] && prev[0].next[0].Key != v+1 {
          metro.insertStation(v + 1)
        }
      }
    }
  }

  for i := 0; i < q; i++ {
    p, x, y := ni32(), ni32(), ni32()
    if p == 1 {
      p1, lane1 := metro.lowerBound(x)
      p2, lane2 := metro.lowerBound(y)
      if p1[0].next[0].Key != x {
        lane1[0]--
      }
      if p2[0].next[0].Key != y {
        lane2[0]--
      }
      ans := lane1[0] - lane2[0]
      if ans >= 0 {
        pri32(ans + 1)
      } else {
        pri32(1 - ans)
      }
      endl()
    }
    if p == 2 {
      idx[a[x]], idx[a[y]] = idx[a[y]], idx[a[x]]
      a[x], a[y] = a[y], a[x]
      process(a[x])
      process(a[y])
    }
  }
  w.Flush()
}


package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func Sol1265E(reader io.Reader, writer io.Writer) {
	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()
	const mod int64 = 998244353
	var exgcd func(a, b int64) (gcd, x, y int64)
	exgcd = func(a, b int64) (gcd, x, y int64) {
		if b == 0 {
			return a, 1, 0
		}
		gcd, y, x = exgcd(b, a%b)
		y -= a / b * x
		return
	}
	modInverse := func(a, m int64) int64 {
		_, x, _ := exgcd(a, m)
		res := (x%m + m) % m
		return res
	}
	modFrac := func(a, b int64) int64 {
		return a * modInverse(b, mod) % mod
	}

	var n int
	Fscan(in, &n)
	var ans, v int64
	for i := 0; i < n; i++ {
		Fscan(in, &v)
		ans = (ans + 1) * modFrac(100, v) % mod
	}
	Fprint(out, ans)
}

func main() {
	Sol1265E(os.Stdin, os.Stdout)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var reader *bufio.Reader=bufio.NewReader(os.Stdin)
var writer *bufio.Writer=bufio.NewWriter(os.Stdout)
func printf(f string, a ...interface{}){fmt.Fprintf(writer,f,a...)}
func scanf(f string,a...interface{}){fmt.Fscanf(reader,f,a...)}

var mod int64=998244353

func fp(x,n int64)int64{
	var res int64
	res=1
	for n>0{
		if (n&1)!=0{res=res*x%mod}
		x=x*x%mod
		n>>=1
	}
	return res
}

func inv(x int64)int64{return fp(x,mod-2)}

var p,P,Q [200010]int64

func main(){
	defer writer.Flush()
	var n int
	scanf("%d\n",&n)
	inv100:=inv(100)
	for i:=1;i<=n;i++{
		scanf("%d",&p[i])
		P[i]=p[i]*inv100%mod
		Q[i]=(100-p[i])*inv100%mod
	}
	scanf("\n")
	right:=int64(1)
	for i:=	n-1;i>0;i--{
		right=right*P[i]%mod+1
		if right==mod{right=0}
	}
	left:=int64(0)
	for i:=n;i>0;i--{left=(left*P[i]%mod+Q[i])%mod}
	left=1+mod-left
	if left>=mod{left-=mod}
	printf("%d\n",right*inv(left)%mod)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var reader *bufio.Reader=bufio.NewReader(os.Stdin)
var writer *bufio.Writer=bufio.NewWriter(os.Stdout)
func printf(f string, a ...interface{}){fmt.Fprintf(writer,f,a...)}
func scanf(f string,a...interface{}){fmt.Fscanf(reader,f,a...)}

var mod=998244353

func fp(x,n int)int{
	res:=1
	for n>0{
		if (n&1)!=0{res=res*x%mod}
		x=x*x%mod
		n>>=1
	}
	return res
}

func inv(x int)int{return fp(x,mod-2)}

var p,P,Q [200010]int

func main(){
	defer writer.Flush()
	var n int
	scanf("%d\n",&n)
	inv100:=inv(100)
	for i:=1;i<=n;i++{
		scanf("%d",&p[i])
		P[i]=p[i]*inv100%mod
		Q[i]=(100-p[i])*inv100%mod
	}
	scanf("\n")
	right:=1
	for i:=	n-1;i>0;i--{
		right=right*P[i]%mod+1
		if right==mod{right=0}
	}
	left:=0
	for i:=n;i>0;i--{left=(left*P[i]%mod+Q[i])%mod}
	left=1+mod-left
	if left>=mod{left-=mod}
	printf("%d,%d,%d\n",mod,right,left)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var reader *bufio.Reader=bufio.NewReader(os.Stdin)
var writer *bufio.Writer=bufio.NewWriter(os.Stdout)
func printf(f string, a ...interface{}){fmt.Fprintf(writer,f,a...)}
func scanf(f string,a...interface{}){fmt.Fscanf(reader,f,a...)}

var mod=998244353

func fp(x,n int)int{
	res:=1
	for n>0{
		if (n&1)!=0{res=res*x%mod}
		x=x*x%mod
		n>>=1
	}
	return res
}

func inv(x int)int{return fp(x,mod-2)}

var p,P,Q [200010]int

func main(){
	defer writer.Flush()
	var n int
	scanf("%d\n",&n)
	inv100:=inv(100)
	for i:=1;i<=n;i++{
		scanf("%d",&p[i])
		P[i]=p[i]*inv100%mod
		Q[i]=(100-p[i])*inv100%mod
	}
	scanf("\n")
	right:=1
	for i:=	n-1;i>0;i--{
		right=right*P[i]%mod+1
		if right==mod{right=0}
	}
	left:=0
	for i:=n;i>0;i--{left=(left*P[i]%mod+Q[i])%mod}
	left=1+mod-left
	if left>=mod{left-=mod}
	printf("%d\n",right*inv(left)%mod)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var reader *bufio.Reader=bufio.NewReader(os.Stdin)
var writer *bufio.Writer=bufio.NewWriter(os.Stdout)
func printf(f string, a ...interface{}){fmt.Fprintf(writer,f,a...)}
func scanf(f string,a...interface{}){fmt.Fscanf(reader,f,a...)}

var mod int64=998244353

func fp(x,n int64)int64{
	var res int64
	res=1
	for n>0{
		if (n&1)!=0{res=res*x%mod}
		x=x*x%mod
		n>>=1
	}
	return res
}

func inv(x int64)int64{return fp(x,mod-2)}

var p,P,Q [200010]int64

func main(){
	defer writer.Flush()
	var n int
	scanf("%d\n",&n)
	inv100:=inv(100)
	for i:=1;i<=n;i++{
		scanf("%d",&p[i])
		P[i]=p[i]*inv100%mod
		Q[i]=(100-p[i])*inv100%mod
	}
	scanf("\n")
	right:=int64(1)
	for i:=	n-1;i>0;i--{
		right=right*P[i]%mod+1
		if right==mod{right=0}
	}
	left:=int64(0)
	for i:=n;i>0;i--{left=(left*P[i]%mod+Q[i])%mod}
	left=1+mod-left
	if left>=mod{left-=mod}
	printf("%d,%d,%d\n",mod,right,left)
}


package main

import (
	"bufio"
	"fmt"
	"os"
)

func int_abs(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}

func int_max(x, y int64) int64 {
	if x > y {
		return x
	}
	return y
}

func int_min(x, y int64) int64 {
	if x < y {
		return x
	}
	return y
}

var (
	stdin       = bufio.NewReader(os.Stdin)
	n, a, total int64
	d           []int64
)

func main() {
	fmt.Fscan(stdin, &n, &a)
	d = make([]int64, n)
	total = 0
	for i := range d {
		fmt.Fscan(stdin, &d[i])
		total += d[i]
	}
	for _, elem := range d {
		var (
			from, to int64
			ans      int64
		)
		from = int_max(1, a-(total-elem))
		to = a - (n - 1)
		if from <= elem {
			ans = elem - (int_min(to, elem) - from + 1)
		} else {
			ans = 0
		}
		fmt.Printf("%d ", ans)
	}
	fmt.Println("")
}

package main

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

var scanner *bufio.Scanner
var writer *bufio.Writer

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}
func getI() int {
  return int(getI64())
}
func getF() float64 {
  scanner.Scan()
  x, _ := strconv.ParseFloat(scanner.Text(), 64)
  return x
}
func getS() string {
  scanner.Scan()
  return scanner.Text()
}

func main() {
  scanner = bufio.NewScanner(os.Stdin)
  scanner.Split(bufio.ScanWords)
  writer = bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  n, total := getI64(), getI64()
  dice := make([]int64, n)
  sum := int64(0)
  for i := range dice {
    dice[i] = getI64()
    sum += dice[i]
  }
  for i, die := range dice {
    result := int64(0)
    low := total - sum + die
    if low > 0 {
      result += low-1
    }
    high := total - n + 1
    if high < die {
      result += die-high
    }
    if i != 0 {
      writer.WriteString(" ")
    }
    writer.WriteString(fmt.Sprintf("%d", result))
  }
  writer.WriteString("\n")
}

package main

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

var scanner *bufio.Scanner
var writer *bufio.Writer

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}
func getI() int {
  return int(getI64())
}
func getF() float64 {
  scanner.Scan()
  x, _ := strconv.ParseFloat(scanner.Text(), 64)
  return x
}
func getS() string {
  scanner.Scan()
  return scanner.Text()
}

func main() {
  scanner = bufio.NewScanner(os.Stdin)
  scanner.Split(bufio.ScanWords)
  writer = bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  n, total := getI(), getI()
  dice := make([]int, n)
  sum := 0
  for i := range dice {
    dice[i] = getI()
    sum += dice[i]
  }
  for i, die := range dice {
    result := 0
    low := total - sum + die
    if low > 0 {
      result += low-1
    }
    high := total - n + 1
    if high < die {
      result += die-high
    }
    if i != 0 {
      writer.WriteString(" ")
    }
    writer.WriteString(fmt.Sprintf("%d", result))
  }
  writer.WriteString("\n")
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func int_abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

func int_max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

func int_min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

var (
	stdin = bufio.NewReader(os.Stdin)
	n, a  int
	d     []int
)

func main() {
	fmt.Fscan(stdin, &n, &a)
	d = make([]int, n)
	total := 0
	for i := range d {
		fmt.Fscan(stdin, &d[i])
		total += d[i]
	}
	for _, elem := range d {
		var from, to, ans int
		from = int_max(1, a-(total-elem))
		to = a - (n - 1)
		if from <= elem {
			ans = elem - (int_min(to, elem) - from + 1)
		} else {
			ans = 0
		}
		fmt.Printf("%d ", ans)
	}
	fmt.Println("")
}


package main

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

func main() {
    stin := bufio.NewScanner(os.Stdin)
    stin.Scan()
    ss := strings.Fields(stin.Text())
    l,_ := strconv.ParseInt(ss[0], 10, 64)
    r,_ := strconv.ParseInt(ss[1], 10, 64)
    k,_ := strconv.ParseInt(ss[2], 10, 64)

    printed := false
    currentPower := -1
    qprev := int64(1)
    q := int64(1)
    for q <= r {
        currentPower += 1

        /*if math.Abs(math.Log(float64(q))/math.Log(float64(k)) - float64(currentPower)) >= 0.1 {
            break
        }*/

        if qprev != 1 {
            if q/k != qprev {
                break
            }
        }

        if q >= l {
            fmt.Printf("%v ", q)
            printed = true
        }
        qprev = q
        q *= k
    }

    if !printed {
        fmt.Printf("-1")
    }
}

package main

import (
         "bufio"
         "os"
         "fmt"
         "strconv"
         "strings"
         "math"
       )

func main() {
    stin := bufio.NewScanner(os.Stdin)
    stin.Scan()
    ss := strings.Fields(stin.Text())
    l,_ := strconv.ParseInt(ss[0], 10, 64)
    r,_ := strconv.ParseInt(ss[1], 10, 64)
    k,_ := strconv.ParseInt(ss[2], 10, 64)

    printed := false
    currentPower := -1
    for q := int64(1); q <= r; q *= k {
        currentPower += 1

        if math.Abs(math.Log(float64(q))/math.Log(float64(k)) - float64(currentPower)) >= 0.1 {
            break
        }

        if q >= l {
            fmt.Printf("%v ", q)
            printed = true
        }
    }

    if !printed {
        fmt.Printf("-1")
    }
}

package main

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

func main() {
    stin := bufio.NewScanner(os.Stdin)
    stin.Scan()
    ss := strings.Fields(stin.Text())
    l,_ := strconv.ParseInt(ss[0], 10, 64)
    r,_ := strconv.ParseInt(ss[1], 10, 64)
    k,_ := strconv.ParseInt(ss[2], 10, 64)

    printed := false
    for q := int64(1); q <= r; q *= k {
        if q >= l {
            fmt.Printf("%v ", q)
            printed = true
        }
    }

    if !printed {
        fmt.Printf("-1")
    }
}

package main

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

func main() {
    stin := bufio.NewScanner(os.Stdin)
    stin.Scan()
    ss := strings.Fields(stin.Text())
    l,_ := strconv.ParseInt(ss[0], 10, 64)
    r,_ := strconv.ParseInt(ss[1], 10, 64)
    k,_ := strconv.ParseInt(ss[2], 10, 64)

    if k > r {
        fmt.Printf("-1")
        return
    }
    for q := int64(1); q <= r; q *= k {
        if q >= l {
            fmt.Printf("%v ", q)
        }
    }
}