Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.buffer.readline

#sys.setrecursionlimit(10**9)
#from functools import lru_cache

def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))
# rstrip().decode()


class BIT:
	def __init__(self, n):
		self.n = n
		self.data = [0]*(n+1)
		self.el = [0]*(n+1)
	def sum(self, i):
		s = 0
		while i > 0:
			s += self.data[i]
			i -= i & -i
		return s
	def add(self, i, x):
		# assert i > 0
		self.el[i] += x
		while i <= self.n:
			self.data[i] += x
			i += i & -i
	def get(self, i, j=None):
		if j is None:
			return self.el[i]
		return self.sum(j) - self.sum(i)


def main():
	n,q=MI()
	C=[0]+LI()

	lr=[]

	for i in range(q):
		l,r=MI()
		lr.append([l,r,i])
	lr.sort(key=lambda x:x[1])
	#print(lr)
	R=[-1]*(n+1)

	B=BIT(n)
	now=1
	ans=[-1]*(q)
	for l,r,i in lr:
		while now<=r:
			if R[C[now]]!=-1:
				B.add(R[C[now]],-1)
			R[C[now]]=now
			B.add(R[C[now]],1)
			now+=1
		#print(R)
		#print(B.el)
		ans[i]=B.get(l-1,r)
	print(*ans,sep="\n")



if __name__ == "__main__":
	main()


Fixed submission:
import sys
input = sys.stdin.buffer.readline

#sys.setrecursionlimit(10**9)
#from functools import lru_cache

def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))
# rstrip().decode()


class BIT:
	def __init__(self, n):
		self.n = n
		self.data = [0]*(n+1)
		self.el = [0]*(n+1)
	def sum(self, i):
		s = 0
		while i > 0:
			s += self.data[i]
			i -= i & -i
		return s
	def add(self, i, x):
		# assert i > 0
		self.el[i] += x
		while i <= self.n:
			self.data[i] += x
			i += i & -i
	def get(self, i, j=None):
		if j is None:
			return self.el[i]
		return self.sum(j) - self.sum(i)


def main():
	n,q=MI()
	C=[0]+LI()

	lr=[]

	for i in range(q):
		l,r=MI()
		lr.append([l,r,i])
	lr.sort(key=lambda x:x[1])
	#print(lr)
	R=[-1]*(n+1)

	B=BIT(n)
	now=1
	ans=[-1]*(q)
	for l,r,i in lr:
		while now<=r:
			if R[C[now]]!=-1:
				B.add(R[C[now]],-1)
			R[C[now]]=now
			B.add(R[C[now]],1)
			now+=1
		#print(R)
		#print(B.el)
		ans[i]=B.get(l-1,r)
	print(*ans,sep="\n")



if __name__ == "__main__":
	main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.buffer.readline

#sys.setrecursionlimit(10**9)
#from functools import lru_cache

def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))
# rstrip().decode()


class SegTree:
    """
    init(init_val, ide_ele): 配列init_valで初期化 O(N)
    update(k, x): k番目の値をxに更新 O(logN)
    query(l, r): 区間[l, r)をsegfuncしたものを返す O(logN)
    """
    def __init__(self, init_val, segfunc, ide_ele):
        """
        init_val: 配列の初期値
        segfunc: 区間にしたい操作
        ide_ele: 単位元
        n: 要素数
        num: n以上の最小の2のべき乗
        tree: セグメント木(1-index)
        """
        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
        # 配列の値を葉にセット
        for i in range(n):
            self.tree[self.num + i] = init_val[i]
        # 構築していく
        for i in range(self.num - 1, 0, -1):
            self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1])

    def update(self, k, x):
        """
        k番目の値をxに更新
        k: index(0-index)
        x: update value
        """
        k += self.num
        self.tree[k] = x
        while k > 1:
            self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1])
            k >>= 1

    def query(self, l, r):
        """
        [l, r)のsegfuncしたものを得る
        l: index(0-index)
        r: index(0-index)
        """
        res = self.ide_ele

        l += self.num
        r += self.num
        while l < r:
            if l & 1:
                res = self.segfunc(res, self.tree[l])
                l += 1
            if r & 1:
                res = self.segfunc(res, self.tree[r - 1])
            l >>= 1
            r >>= 1
        return res



def main():
	n,q=MI()
	c=LI()
	for i in range(n):
		c[i]={c[i]}
	#####segfunc#####
	def segfunc(x, y):
		return x.union(y)

	ide_ele =set()
	S=SegTree(c,segfunc,ide_ele)

	for _ in range(q):
		ll,rr=MI()
		print(len(S.query(ll-1,rr)))


if __name__ == "__main__":
	main()


Fixed submission:
import sys
input = sys.stdin.buffer.readline

#sys.setrecursionlimit(10**9)
#from functools import lru_cache

def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))
# rstrip().decode()


class BIT:
	def __init__(self, n):
		self.n = n
		self.data = [0]*(n+1)
		self.el = [0]*(n+1)
	def sum(self, i):
		s = 0
		while i > 0:
			s += self.data[i]
			i -= i & -i
		return s
	def add(self, i, x):
		# assert i > 0
		self.el[i] += x
		while i <= self.n:
			self.data[i] += x
			i += i & -i
	def get(self, i, j=None):
		if j is None:
			return self.el[i]
		return self.sum(j) - self.sum(i)


def main():
	n,q=MI()
	C=[0]+LI()

	lr=[]

	for i in range(q):
		l,r=MI()
		lr.append([l,r,i])
	lr.sort(key=lambda x:x[1])
	#print(lr)
	R=[-1]*(n+1)

	B=BIT(n)
	now=1
	ans=[-1]*(q)
	for l,r,i in lr:
		while now<=r:
			if R[C[now]]!=-1:
				B.add(R[C[now]],-1)
			R[C[now]]=now
			B.add(R[C[now]],1)
			now+=1
		#print(R)
		#print(B.el)
		ans[i]=B.get(l-1,r)
	print(*ans,sep="\n")



if __name__ == "__main__":
	main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.buffer.readline

#sys.setrecursionlimit(10**9)
#from functools import lru_cache

def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))
# rstrip().decode()


class SegTree:
    """
    init(init_val, ide_ele): 配列init_valで初期化 O(N)
    update(k, x): k番目の値をxに更新 O(logN)
    query(l, r): 区間[l, r)をsegfuncしたものを返す O(logN)
    """
    def __init__(self, init_val, segfunc, ide_ele):
        """
        init_val: 配列の初期値
        segfunc: 区間にしたい操作
        ide_ele: 単位元
        n: 要素数
        num: n以上の最小の2のべき乗
        tree: セグメント木(1-index)
        """
        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
        # 配列の値を葉にセット
        for i in range(n):
            self.tree[self.num + i] = init_val[i]
        # 構築していく
        for i in range(self.num - 1, 0, -1):
            self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1])

    def update(self, k, x):
        """
        k番目の値をxに更新
        k: index(0-index)
        x: update value
        """
        k += self.num
        self.tree[k] = x
        while k > 1:
            self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1])
            k >>= 1

    def query(self, l, r):
        """
        [l, r)のsegfuncしたものを得る
        l: index(0-index)
        r: index(0-index)
        """
        res = self.ide_ele

        l += self.num
        r += self.num
        while l < r:
            if l & 1:
                res = self.segfunc(res, self.tree[l])
                l += 1
            if r & 1:
                res = self.segfunc(res, self.tree[r - 1])
            l >>= 1
            r >>= 1
        return res



def main():
	n,q=MI()
	c=LI()
	for i in range(n):
		c[i]={c[i]}
	#####segfunc#####
	def segfunc(x, y):
		return x.union(y)

	ide_ele =set()
	S=SegTree(c,segfunc,ide_ele)

	for _ in range(q):
		ll,rr=MI()
		print(len(S.query(ll-1,rr)))


if __name__ == "__main__":
	main()


Fixed submission:
import sys
input = sys.stdin.buffer.readline

#sys.setrecursionlimit(10**9)
#from functools import lru_cache

def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))
# rstrip().decode()


class BIT:
	def __init__(self, n):
		self.n = n
		self.data = [0]*(n+1)
		self.el = [0]*(n+1)
	def sum(self, i):
		s = 0
		while i > 0:
			s += self.data[i]
			i -= i & -i
		return s
	def add(self, i, x):
		# assert i > 0
		self.el[i] += x
		while i <= self.n:
			self.data[i] += x
			i += i & -i
	def get(self, i, j=None):
		if j is None:
			return self.el[i]
		return self.sum(j) - self.sum(i)


def main():
	n,q=MI()
	C=[0]+LI()

	lr=[]

	for i in range(q):
		l,r=MI()
		lr.append([l,r,i])
	lr.sort(key=lambda x:x[1])
	#print(lr)
	R=[-1]*(n+1)

	B=BIT(n)
	now=1
	ans=[-1]*(q)
	for l,r,i in lr:
		while now<=r:
			if R[C[now]]!=-1:
				B.add(R[C[now]],-1)
			R[C[now]]=now
			B.add(R[C[now]],1)
			now+=1
		#print(R)
		#print(B.el)
		ans[i]=B.get(l-1,r)
	print(*ans,sep="\n")



if __name__ == "__main__":
	main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.buffer.readline

#sys.setrecursionlimit(10**9)
#from functools import lru_cache

def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))
# rstrip().decode()

#A1 ... AnのBIT(1-indexed)



class SegTree:
    """
    init(init_val, ide_ele): 配列init_valで初期化 O(N)
    update(k, x): k番目の値をxに更新 O(logN)
    query(l, r): 区間[l, r)をsegfuncしたものを返す O(logN)
    """
    def __init__(self, init_val, segfunc, ide_ele):
        """
        init_val: 配列の初期値
        segfunc: 区間にしたい操作
        ide_ele: 単位元
        n: 要素数
        num: n以上の最小の2のべき乗
        tree: セグメント木(1-index)
        """
        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
        # 配列の値を葉にセット
        for i in range(n):
            self.tree[self.num + i] = init_val[i]
        # 構築していく
        for i in range(self.num - 1, 0, -1):
            self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1])

    def update(self, k, x):
        """
        k番目の値をxに更新
        k: index(0-index)
        x: update value
        """
        k += self.num
        self.tree[k] = x
        while k > 1:
            self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1])
            k >>= 1

    def query(self, l, r):
        """
        [l, r)のsegfuncしたものを得る
        l: index(0-index)
        r: index(0-index)
        """
        res = self.ide_ele

        l += self.num
        r += self.num
        while l < r:
            if l & 1:
                res = self.segfunc(res, self.tree[l])
                l += 1
            if r & 1:
                res = self.segfunc(res, self.tree[r - 1])
            l >>= 1
            r >>= 1
        return res



def main():
	n,q=MI()
	c=LI()
	for i in range(n):
		c[i]={c[i]}
	#####segfunc#####
	def segfunc(x, y):
		return x | y

	ide_ele =set()
	S=SegTree(c,segfunc,ide_ele)

	for _ in range(q):
		ll,rr=MI()
		print(len(S.query(ll-1,rr)))









if __name__ == "__main__":
	main()


Fixed submission:
import sys
input = sys.stdin.buffer.readline

#sys.setrecursionlimit(10**9)
#from functools import lru_cache

def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))
# rstrip().decode()


class BIT:
	def __init__(self, n):
		self.n = n
		self.data = [0]*(n+1)
		self.el = [0]*(n+1)
	def sum(self, i):
		s = 0
		while i > 0:
			s += self.data[i]
			i -= i & -i
		return s
	def add(self, i, x):
		# assert i > 0
		self.el[i] += x
		while i <= self.n:
			self.data[i] += x
			i += i & -i
	def get(self, i, j=None):
		if j is None:
			return self.el[i]
		return self.sum(j) - self.sum(i)


def main():
	n,q=MI()
	C=[0]+LI()

	lr=[]

	for i in range(q):
		l,r=MI()
		lr.append([l,r,i])
	lr.sort(key=lambda x:x[1])
	#print(lr)
	R=[-1]*(n+1)

	B=BIT(n)
	now=1
	ans=[-1]*(q)
	for l,r,i in lr:
		while now<=r:
			if R[C[now]]!=-1:
				B.add(R[C[now]],-1)
			R[C[now]]=now
			B.add(R[C[now]],1)
			now+=1
		#print(R)
		#print(B.el)
		ans[i]=B.get(l-1,r)
	print(*ans,sep="\n")



if __name__ == "__main__":
	main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.buffer.readline

#sys.setrecursionlimit(10**9)
#from functools import lru_cache

def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))
# rstrip().decode()

#A1 ... AnのBIT(1-indexed)



class SegTree:
    """
    init(init_val, ide_ele): 配列init_valで初期化 O(N)
    update(k, x): k番目の値をxに更新 O(logN)
    query(l, r): 区間[l, r)をsegfuncしたものを返す O(logN)
    """
    def __init__(self, init_val, segfunc, ide_ele):
        """
        init_val: 配列の初期値
        segfunc: 区間にしたい操作
        ide_ele: 単位元
        n: 要素数
        num: n以上の最小の2のべき乗
        tree: セグメント木(1-index)
        """
        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
        # 配列の値を葉にセット
        for i in range(n):
            self.tree[self.num + i] = init_val[i]
        # 構築していく
        for i in range(self.num - 1, 0, -1):
            self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1])

    def update(self, k, x):
        """
        k番目の値をxに更新
        k: index(0-index)
        x: update value
        """
        k += self.num
        self.tree[k] = x
        while k > 1:
            self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1])
            k >>= 1

    def query(self, l, r):
        """
        [l, r)のsegfuncしたものを得る
        l: index(0-index)
        r: index(0-index)
        """
        res = self.ide_ele

        l += self.num
        r += self.num
        while l < r:
            if l & 1:
                res = self.segfunc(res, self.tree[l])
                l += 1
            if r & 1:
                res = self.segfunc(res, self.tree[r - 1])
            l >>= 1
            r >>= 1
        return res



def main():
	n,q=MI()
	c=LI()
	for i in range(n):
		c[i]={c[i]}
	#####segfunc#####
	def segfunc(x, y):
		return x | y

	ide_ele =set()
	S=SegTree(c,segfunc,ide_ele)

	for _ in range(q):
		ll,rr=MI()
		print(len(S.query(ll-1,rr)))









if __name__ == "__main__":
	main()


Fixed submission:
import sys
input = sys.stdin.buffer.readline

#sys.setrecursionlimit(10**9)
#from functools import lru_cache

def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))
# rstrip().decode()


class BIT:
	def __init__(self, n):
		self.n = n
		self.data = [0]*(n+1)
		self.el = [0]*(n+1)
	def sum(self, i):
		s = 0
		while i > 0:
			s += self.data[i]
			i -= i & -i
		return s
	def add(self, i, x):
		# assert i > 0
		self.el[i] += x
		while i <= self.n:
			self.data[i] += x
			i += i & -i
	def get(self, i, j=None):
		if j is None:
			return self.el[i]
		return self.sum(j) - self.sum(i)


def main():
	n,q=MI()
	C=[0]+LI()

	lr=[]

	for i in range(q):
		l,r=MI()
		lr.append([l,r,i])
	lr.sort(key=lambda x:x[1])
	#print(lr)
	R=[-1]*(n+1)

	B=BIT(n)
	now=1
	ans=[-1]*(q)
	for l,r,i in lr:
		while now<=r:
			if R[C[now]]!=-1:
				B.add(R[C[now]],-1)
			R[C[now]]=now
			B.add(R[C[now]],1)
			now+=1
		#print(R)
		#print(B.el)
		ans[i]=B.get(l-1,r)
	print(*ans,sep="\n")



if __name__ == "__main__":
	main()