import heapq 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 n,q = map(int,input().split()) a = [int(i) for i in input().split()] qry = [[int(i) for i in input().split()]+[j] for j in range(q)] ru = Bit(n+1) #print(qry) qry.sort(key=lambda x:-x[1]) _,l,r,IDX = qry[0] l -= 1 r -= 1 hp = [] heapq.heapify(hp) ans = [0]*(q) for i in range(n-1,l-1,-1): ru.add(i+1,1) val = a[i] while len(hp): tmp,idx = heapq.heappop(hp) if val > tmp: ru.add(idx,-1) else: heapq.heappush(hp,[tmp,idx]) break heapq.heappush(hp,[val,i+1]) ans[IDX] = ru.sum(r+1)-ru.sum(l) #print(ans) #exit() L = l for i in range(1,q): _,l,r,IDX = qry[i] #print(qry[i],"i") l -= 1 r -= 1 if L > l: for j in range(L-1,l-1,-1): ru.add(j+1,1) val = a[j] while len(hp): tmp,idx = heapq.heappop(hp) if val > tmp: ru.add(idx,-1) else: heapq.heappush(hp,[tmp,idx]) break heapq.heappush(hp,[val,j+1]) L = l #print(ru.sum(r+1),i,"ii") ans[IDX] = ru.sum(r+1)-ru.sum(l) #print(idx,"idx") print(*ans,sep="\n")