Buggy python submission (verdict: Time Limit Exceeded):
n,q=map(int,input().split())
N=n

data = [0]*(N+1)
def add(k, x):
    while k <= N:
        data[k] += x
        k += k & -k

def get(k):
    s = 0
    while k:
        s += data[k]
        k -= k & -k
    return s

# 二分探索
N0 = 2**(N-1).bit_length()
def lower_bound(x):
    w = i = 0
    k = N0
    while k:
        if i+k <= N and w + data[i+k] <= x:
            w += data[i+k]
            i += k
        k >>= 1
    return i+1

c=[int(i) for i in input().split()]
Q=[]
for i in range(q):
    l,r=map(int,input().split())
    Q.append((l,r,i))
Ans=[0 for i in range(q)]
F=[-1 for i in range(5*10**5+1)]
Q.sort(key=lambda x:x[1])
Q.append((10**6,10**6,10**6))

for i in range(n):
    add(i+1,1)

quen=0
for i in range(n):
    if F[c[i]]>=0:
        add(F[c[i]]+1,-1)
    F[c[i]]=i
    while Q[quen][1]==i+1:
        Ans[Q[quen][2]]=-get(Q[quen][0]-1)+get(Q[quen][1])
        #print(Tre.query(Q[quen][0]-1,Q[quen][1]))
        quen+=1

for ele in Ans:
    print(ele)

Fixed submission:
n,q=map(int,input().split())
N=n

data = [0]*(N+1)
def add(k, x):
    while k <= N:
        data[k] += x
        k += k & -k

def get(k):
    s = 0
    while k:
        s += data[k]
        k -= k & -k
    return s

c=[int(i) for i in input().split()]
Q=[]
for i in range(q):
    l,r=map(int,input().split())
    Q.append((l,r,i))
Ans=[0 for i in range(q)]
F=[-1 for i in range(5*10**5+1)]
Q.sort(key=lambda x:x[1])
#Q.append((10**6,10**6,10**6))

for i in range(n):
    add(i+1,1)

"""
quen=0
nowQ=Q[quen]
for i in range(n):
    ci=c[i]
    if F[ci]>=0:
        add(F[ci]+1,-1)
    F[ci]=i
    while nowQ[1]==i+1:
        Ans[nowQ[2]]=-get(nowQ[0]-1)+get(nowQ[1])
        quen+=1
        nowQ=Q[quen]
    if nowQ[1]==10**6:
        break
"""

i=0
for nowQ in Q:
    while nowQ[1]!=i:
        ci=c[i]
        if F[ci]>=0:
            add(F[ci]+1,-1)
        F[ci]=i
        i+=1
    Ans[nowQ[2]]=-get(nowQ[0]-1)+get(nowQ[1])


print("\n".join([str(i) for i in Ans]))

Buggy python submission (verdict: Time Limit Exceeded):

#####segfunc#####
def segfunc(x, y):
    return x+y
#################

#####ide_ele　単位元#####
ide_ele = 0
#################



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

#appapp[i]=SegTree(app[i], segfunc, ide_ele) #こんなかんじ

n,q=map(int,input().split())
c=[int(i) for i in input().split()]
Q=[]
for i in range(q):
    l,r=map(int,input().split())
    Q.append((l,r,i))
Ans=[0 for i in range(q)]
Tre=SegTree([1 for i in range(n+1)],segfunc,ide_ele)
F=[-1 for i in range(5*10**5+1)]
Q.sort(key=lambda x:x[1])
Q.append((10**6,10**6,10**6))

quen=0
for i in range(n):
    if F[c[i]]>=0:
        Tre.update(F[c[i]],0)
    F[c[i]]=i
    while Q[quen][1]==i+1:
        Ans[Q[quen][2]]=Tre.query(Q[quen][0]-1,Q[quen][1])
        #print(Tre.query(Q[quen][0]-1,Q[quen][1]))
        quen+=1

for ele in Ans:
    print(ele)

Fixed submission:
n,q=map(int,input().split())
N=n

data = [0]*(N+1)
def add(k, x):
    while k <= N:
        data[k] += x
        k += k & -k

def get(k):
    s = 0
    while k:
        s += data[k]
        k -= k & -k
    return s

c=[int(i) for i in input().split()]
Q=[]
for i in range(q):
    l,r=map(int,input().split())
    Q.append((l,r,i))
Ans=[0 for i in range(q)]
F=[-1 for i in range(5*10**5+1)]
Q.sort(key=lambda x:x[1])
#Q.append((10**6,10**6,10**6))

for i in range(n):
    add(i+1,1)

"""
quen=0
nowQ=Q[quen]
for i in range(n):
    ci=c[i]
    if F[ci]>=0:
        add(F[ci]+1,-1)
    F[ci]=i
    while nowQ[1]==i+1:
        Ans[nowQ[2]]=-get(nowQ[0]-1)+get(nowQ[1])
        quen+=1
        nowQ=Q[quen]
    if nowQ[1]==10**6:
        break
"""

i=0
for nowQ in Q:
    while nowQ[1]!=i:
        ci=c[i]
        if F[ci]>=0:
            add(F[ci]+1,-1)
        F[ci]=i
        i+=1
    Ans[nowQ[2]]=-get(nowQ[0]-1)+get(nowQ[1])


print("\n".join([str(i) for i in Ans]))

Buggy python submission (verdict: Time Limit Exceeded):

#####segfunc#####
def segfunc(x, y):
    return x|y
#################

#####ide_ele　単位元#####
ide_ele = set()
#################



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



n,q=map(int,input().split())
C=[{int(i)} for i in input().split()]
T=SegTree(C, segfunc, ide_ele)
for i in range(q):
    l,r=map(int,input().split())
    g=T.query(l-1,r)
    print(len(g))



Fixed submission:
n,q=map(int,input().split())
N=n

data = [0]*(N+1)
def add(k, x):
    while k <= N:
        data[k] += x
        k += k & -k

def get(k):
    s = 0
    while k:
        s += data[k]
        k -= k & -k
    return s

c=[int(i) for i in input().split()]
Q=[]
for i in range(q):
    l,r=map(int,input().split())
    Q.append((l,r,i))
Ans=[0 for i in range(q)]
F=[-1 for i in range(5*10**5+1)]
Q.sort(key=lambda x:x[1])
#Q.append((10**6,10**6,10**6))

for i in range(n):
    add(i+1,1)

"""
quen=0
nowQ=Q[quen]
for i in range(n):
    ci=c[i]
    if F[ci]>=0:
        add(F[ci]+1,-1)
    F[ci]=i
    while nowQ[1]==i+1:
        Ans[nowQ[2]]=-get(nowQ[0]-1)+get(nowQ[1])
        quen+=1
        nowQ=Q[quen]
    if nowQ[1]==10**6:
        break
"""

i=0
for nowQ in Q:
    while nowQ[1]!=i:
        ci=c[i]
        if F[ci]>=0:
            add(F[ci]+1,-1)
        F[ci]=i
        i+=1
    Ans[nowQ[2]]=-get(nowQ[0]-1)+get(nowQ[1])


print("\n".join([str(i) for i in Ans]))

Buggy python submission (verdict: Time Limit Exceeded):

#####segfunc#####
def segfunc(x, y):
    return x|y
#################

#####ide_ele　単位元#####
ide_ele = set()
#################



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



n,q=map(int,input().split())
C=[{int(i)} for i in input().split()]
T=SegTree(C, segfunc, ide_ele)
for i in range(q):
    l,r=map(int,input().split())
    g=T.query(l-1,r)
    print(len(g))



Fixed submission:
n,q=map(int,input().split())
N=n

data = [0]*(N+1)
def add(k, x):
    while k <= N:
        data[k] += x
        k += k & -k

def get(k):
    s = 0
    while k:
        s += data[k]
        k -= k & -k
    return s

c=[int(i) for i in input().split()]
Q=[]
for i in range(q):
    l,r=map(int,input().split())
    Q.append((l,r,i))
Ans=[0 for i in range(q)]
F=[-1 for i in range(5*10**5+1)]
Q.sort(key=lambda x:x[1])
#Q.append((10**6,10**6,10**6))

for i in range(n):
    add(i+1,1)

"""
quen=0
nowQ=Q[quen]
for i in range(n):
    ci=c[i]
    if F[ci]>=0:
        add(F[ci]+1,-1)
    F[ci]=i
    while nowQ[1]==i+1:
        Ans[nowQ[2]]=-get(nowQ[0]-1)+get(nowQ[1])
        quen+=1
        nowQ=Q[quen]
    if nowQ[1]==10**6:
        break
"""

i=0
for nowQ in Q:
    while nowQ[1]!=i:
        ci=c[i]
        if F[ci]>=0:
            add(F[ci]+1,-1)
        F[ci]=i
        i+=1
    Ans[nowQ[2]]=-get(nowQ[0]-1)+get(nowQ[1])


print("\n".join([str(i) for i in Ans]))