mod = 1000000007 eps = 10**-9 def main(): import sys input = sys.stdin.buffer.readline class Bit: def __init__(self, n): self.size = n self.tree = [0] * (n + 1) def sum(self, i): s = 0 while i > 0: s += self.tree[i] i -= i & -i return s def add(self, i, x): while i <= self.size: self.tree[i] += x i += i & -i def lower_bound(self, w): if w <= 0: return 0 x = 0 k = 1 << (self.size.bit_length() - 1) while k: if x + k <= self.size and self.tree[x + k] < w: w -= self.tree[x + k] x += k k >>= 1 return x + 1 N, Q = map(int, input().split()) A = list(map(int, input().split())) A.append(N+1) bit = Bit(N+1) idx = [0] * (N+2) for i, a in enumerate(A): idx[a] = i+1 nxt = [[N+1] * 18 for _ in range(N+2)] bit.add(N+1, 1) for a in range(N, 0, -1): i = idx[a] nxt[i][0] = bit.lower_bound(bit.sum(i) + 1) bit.add(i, 1) for lv in range(1, 18): for i in range(1, N+2): nxt[i][lv] = nxt[nxt[i][lv-1]][lv-1] for _ in range(Q): _, l, r = map(int, input().split()) ans = 1 for lv in range(17, -1, -1): if nxt[l][lv] <= r: ans += 1<