class SegmentTree: def __init__(self, init_val, segfunc, ide_ele): n = len(init_val) self.segfunc = segfunc self.ide_ele = ide_ele self.num = 1 << (n - 1).bit_length() self.tree = [ide_ele] * 2 * self.num self.range = [(-1,n)] * 2 * self.num # 配列の値を葉にセット for i in range(n): self.tree[self.num + i] = init_val[i] self.range[self.num + i] = (i,i) # 構築していく for i in range(self.num - 1, 0, -1): self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1]) self.range[i] = (self.range[2 * i][0],self.range[2 * i + 1][1]) def update(self, k, x): k += self.num self.tree[k] = x while k > 1: if not k%2: self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1]) else: self.tree[k >> 1] = self.segfunc(self.tree[k ^ 1], self.tree[k]) k >>= 1 def query(self,l,r): l+=self.num r+=self.num res=self.ide_ele L = [] R = [] while l>=1 r>>=1 if L and R and R[-1]==L[-1]: R.pop() R=R[::-1] m = L+R for idx in m: res = self.segfunc(res,self.tree[idx]) return res def segfunc(x,y): if x[0]<=y[0]: return (y[0],x[1]+y[1]) else: return x ide_ele = (0,0) import sys input = sys.stdin.readline N,Q = map(int,input().split()) a = list(map(int,input().split())) a = [(a[i],1) for i in range(N)] Seg = SegmentTree(a,segfunc,ide_ele) query = [list(map(int,input().split()))+[i] for i in range(Q)] ans = [-1 for i in range(Q)] query.sort(key=lambda x:-x[1]) pos = 0 while query: a,l,r,idx = query.pop() while l-1>pos: Seg.update(pos,(0,0)) pos += 1 ans[idx] = Seg.query(l-1,r)[1] for i in range(Q): print(ans[i])