" Reference: https://qiita.com/tatyam/items/492c70ac4c955c055602" # ※ 計算量が O(sqrt(N)) per query なので, 過度な期待はしないこと. from bisect import bisect_left, bisect_right, insort class Sorted_Multiset: BUCKET_RATIO=50 REBUILD_RATIO=170 def __init__(self, A=[]): A=list(A) if not all(A[i]<=A[i+1] for i in range(len(A)-1)): A=sorted(A) self.__build(A) return def __build(self, A=None): if A is None: A=list(self) self.N=N=len(A) K=1 while self.BUCKET_RATIO*K*Klen(self.list)*self.REBUILD_RATIO: self.__build() def discard(self, x): if self.N==0: return False A=self.__find_bucket(x) i=bisect_left(A, x) if not(i!=len(A) and A[i]==x): return False # x が存在しないので... A.pop(i) self.N-=1 if len(A)==0: self.__build() return True def remove(self, x): if not self.discard(x): raise KeyError(x) #=== get, pop def __getitem__(self, index): if index<0: index+=self.N if index<0: raise IndexError("index out of range") for A in self.list: if index=value: return A[bisect_left(A,value)] else: for A in self.list: if A[-1]>value: return A[bisect_right(A,value)] #=== count def less_count(self, value, equal=False): """ a < value となる S の元 a の個数を求める. equal=True ならば, a < value が a <= value になる. """ count=0 if equal: for A in self.list: if A[-1]>value: return count+bisect_right(A, value) count+=len(A) else: for A in self.list: if A[-1]>=value: return count+bisect_left(A, value) count+=len(A) return count def more_count(self, value, equal=False): """ a > value となる S の元 a の個数を求める. equal=True ならば, a > value が a >= value になる. """ return self.N-self.less_count(value, not equal) def count(self, value): """ a = value となる S の元 a の個数を求める. """ return self.less_count(value, True)-self.less_count(value, False) #=== def is_upper_bound(self, x, equal=True): if self.N: a=self.list[-1][-1] return (avalue: i=bisect_left(A, value) if A[i]==value: return index+i else: raise ValueError("{} is not in Multiset".format(value)) index+=len(A) raise ValueError("{} is not in Multiset".format(value)) #================================================== def solve(): N, A = map(int, input().split()) X = list(map(int, input().split())) T = int(input()) E = set(X) Sound = [None] * T for t in range(T): l, r = map(int, input().split()) Sound[t] = (l,r) E.add(l); E.add(r) E_inv = {e:i for i,e in enumerate(sorted(E))} K = len(E_inv) person = [[] for _ in range(K)] for i in range(N): xj = E_inv[X[i]] person[xj].append(i) begin = [[] for _ in range(K)] end = [[] for _ in range(K)] for t,(l,r) in enumerate(Sound,1): lj = E_inv[l]; rj = E_inv[r] begin[lj].append(t) end[rj].append(t) S = Sorted_Multiset() ans = [0] * N for j in range(K): for t in begin[j]: S.add(t) for i in person[j]: if S: ans[i] = S.get_max() else: ans[i] = -1 for t in end[j]: S.discard(t) return ans #================================================== import sys input=sys.stdin.readline write=sys.stdout.write write("\n".join(map(str, solve())))