The Smart Beaver has recently designed and built an innovative nanotechnologic all-purpose beaver mass shaving machine, "Beavershave 5000". Beavershave 5000 can shave beavers by families! How does it work? Very easily!There are n beavers, each of them has a unique id from 1 to n. Consider a permutation a1, a2, ..., an of n these beavers. Beavershave 5000 needs one session to shave beavers with ids from x to y (inclusive) if and only if there are such indices i1 < i2 < ... < ik, that ai1 = x, ai2 = x + 1, ..., aik - 1 = y - 1, aik = y. And that is really convenient. For example, it needs one session to shave a permutation of beavers 1, 2, 3, ..., n.If we can't shave beavers from x to y in one session, then we can split these beavers into groups [x, p1], [p1 + 1, p2], ..., [pm + 1, y] (x ≤ p1 < p2 < ... < pm < y), in such a way that the machine can shave beavers in each group in one session. But then Beavershave 5000 needs m + 1 working sessions to shave beavers from x to y.All beavers are restless and they keep trying to swap. So if we consider the problem more formally, we can consider queries of two types:   what is the minimum number of sessions that Beavershave 5000 needs to shave beavers with ids from x to y, inclusive?  two beavers on positions x and y (the beavers ax and ay) swapped. You can assume that any beaver can be shaved any number of times.

The first line contains integer n — the total number of beavers, 2 ≤ n. The second line contains n space-separated integers — the initial beaver permutation. The third line contains integer q — the number of queries, 1 ≤ q ≤ 105. The next q lines contain the queries. Each query i looks as pi xi yi, where pi is the query type (1 is to shave beavers from xi to yi, inclusive, 2 is to swap beavers on positions xi and yi). All queries meet the condition: 1 ≤ xi < yi ≤ n.   to get 30 points, you need to solve the problem with constraints: n ≤ 100 (subproblem B1);  to get 100 points, you need to solve the problem with constraints: n ≤ 3·105 (subproblems B1+B2).  Note that the number of queries q is limited 1 ≤ q ≤ 105 in both subproblem B1 and subproblem B2.

For each query with pi = 1, print the minimum number of Beavershave 5000 sessions.

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] {
        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()

  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()
}


Creatnx has $$$n$$$ mirrors, numbered from $$$1$$$ to $$$n$$$. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The $$$i$$$-th mirror will tell Creatnx that he is beautiful with probability $$$\frac{p_i}{100}$$$ for all $$$1 \le i \le n$$$.Creatnx asks the mirrors one by one, starting from the $$$1$$$-st mirror. Every day, if he asks $$$i$$$-th mirror, there are two possibilities:  The $$$i$$$-th mirror tells Creatnx that he is beautiful. In this case, if $$$i = n$$$ Creatnx will stop and become happy, otherwise he will continue asking the $$$i+1$$$-th mirror next day;  In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the $$$1$$$-st mirror again. You need to calculate the expected number of days until Creatnx becomes happy.This number should be found by modulo $$$998244353$$$. Formally, let $$$M = 998244353$$$. It can be shown that the answer can be expressed as an irreducible fraction $$$\frac{p}{q}$$$, where $$$p$$$ and $$$q$$$ are integers and $$$q \not \equiv 0 \pmod{M}$$$. Output the integer equal to $$$p \cdot q^{-1} \bmod M$$$. In other words, output such an integer $$$x$$$ that $$$0 \le x < M$$$ and $$$x \cdot q \equiv p \pmod{M}$$$.

The first line contains one integer $$$n$$$ ($$$1\le n\le 2\cdot 10^5$$$) — the number of mirrors. The second line contains $$$n$$$ integers $$$p_1, p_2, \ldots, p_n$$$ ($$$1 \leq p_i \leq 100$$$).

Print the answer modulo $$$998244353$$$ in a single line.

NoteIn the first test, there is only one mirror and it tells, that Creatnx is beautiful with probability $$$\frac{1}{2}$$$. So, the expected number of days until Creatnx becomes happy is $$$2$$$.

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)
}


Polycarp has n dice d1, d2, ..., dn. The i-th dice shows numbers from 1 to di. Polycarp rolled all the dice and the sum of numbers they showed is A. Agrippina didn't see which dice showed what number, she knows only the sum A and the values d1, d2, ..., dn. However, she finds it enough to make a series of statements of the following type: dice i couldn't show number r. For example, if Polycarp had two six-faced dice and the total sum is A = 11, then Agrippina can state that each of the two dice couldn't show a value less than five (otherwise, the remaining dice must have a value of at least seven, which is impossible).For each dice find the number of values for which it can be guaranteed that the dice couldn't show these values if the sum of the shown values is A.

The first line contains two integers n, A (1 ≤ n ≤ 2·105, n ≤ A ≤ s) — the number of dice and the sum of shown values where s = d1 + d2 + ... + dn. The second line contains n integers d1, d2, ..., dn (1 ≤ di ≤ 106), where di is the maximum value that the i-th dice can show.

Print n integers b1, b2, ..., bn, where bi is the number of values for which it is guaranteed that the i-th dice couldn't show them.

NoteIn the first sample from the statement A equal to 8 could be obtained in the only case when both the first and the second dice show 4. Correspondingly, both dice couldn't show values 1, 2 or 3.In the second sample from the statement A equal to 3 could be obtained when the single dice shows 3. Correspondingly, it couldn't show 1, 2, 4 or 5.In the third sample from the statement A equal to 3 could be obtained when one dice shows 1 and the other dice shows 2. That's why the first dice doesn't have any values it couldn't show and the second dice couldn't show 3.

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 (
	"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 := 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("")
}


Programmer Rostislav got seriously interested in the Link/Cut Tree data structure, which is based on Splay trees. Specifically, he is now studying the expose procedure.Unfortunately, Rostislav is unable to understand the definition of this procedure, so he decided to ask programmer Serezha to help him. Serezha agreed to help if Rostislav solves a simple task (and if he doesn't, then why would he need Splay trees anyway?)Given integers l, r and k, you need to print all powers of number k within range from l to r inclusive. However, Rostislav doesn't want to spent time doing this, as he got interested in playing a network game called Agar with Gleb. Help him!

The first line of the input contains three space-separated integers l, r and k (1 ≤ l ≤ r ≤ 1018, 2 ≤ k ≤ 109).

Print all powers of number k, that lie within range from l to r in the increasing order. If there are no such numbers, print "-1" (without the quotes).

NoteNote to the first sample: numbers 20 = 1, 21 = 2, 22 = 4, 23 = 8 lie within the specified range. The number 24 = 16 is greater then 10, thus it shouldn't be printed.

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"
         //"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"
       )

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)
        }
    }
}