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

n,q = map(int,rd().split())
c = list(map(int,rd().split()))

# セグ木

#使う操作
#####segfunc#####
def segfunc1(x, y):
    return x | y
#################

#####ide_ele#####
ide_ele1 = 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
        
c = [{i} for i in c]
seg = SegTree(c, segfunc1, ide_ele1)

for i in range(q):
    l,r = map(int,rd().split())
    print(len(seg.query(l-1, r)))

Fixed submission:
import sys
rd = sys.stdin.readline

class BIT:
    def __init__(self,len_A):
        self.N = len_A + 10
        self.bit = [0]*(len_A+10)
        
    # sum(A0 ~ Ai)
    # O(log N)
    def query(self,i):
        res = 0
        idx = i+1
        while idx:
            res += self.bit[idx]
            idx -= idx&(-idx)
        return res

    # Ai += x
    # O(log N)
    def add(self,i,x):
        idx = i+1
        while idx < self.N:
            self.bit[idx] += x
            idx += idx&(-idx)
    
    # min_i satisfying {sum(A0 ~ Ai) >= w} (Ai >= 0)
    # O(log N)
    def lower_left(self,w):
        if (w < 0):
            return -1
        x = 0
        k = 1<<(self.N.bit_length()-1)
        while k > 0:
            if x+k < self.N and self.bit[x+k] < w:
                w -= self.bit[x+k]
                x += k
            k //= 2
        return x

n,q = map(int,rd().split())
c = list(map(int,rd().split()))
lr = []
for i in range(q):
    l,r = map(int,rd().split())
    lr.append([l,r,i])
lr.sort(key = lambda x:x[1])

last = [-1]*(n+1)
bit = BIT(n+1)
ans = [0]*q
current = 1
for i in range(q):
    # rightに達するまで
    while current <= lr[i][1]:
        # 今の色が過去にあった場合、bitの値を0に戻す
        if last[c[current-1]] != -1:
            bit.add(last[c[current-1]], -1)
        last[c[current-1]] = current
        bit.add(current, 1)
        current += 1
    ans[lr[i][2]] = bit.query(lr[i][1]) - bit.query(lr[i][0] - 1)

print(*ans, sep = "\n")

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

# セグ木

#使う操作
#####segfunc#####
def segfunc1(x, y):
    return x | y
#################

#####ide_ele#####
ide_ele1 = 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

c = [{i} for i in c]
seg = SegTree(c, segfunc1, ide_ele1)

for i in range(q):
    l,r = map(int,input().split())
    print(len(seg.query(l-1, r)))

Fixed submission:
import sys
rd = sys.stdin.readline

class BIT:
    def __init__(self,len_A):
        self.N = len_A + 10
        self.bit = [0]*(len_A+10)
        
    # sum(A0 ~ Ai)
    # O(log N)
    def query(self,i):
        res = 0
        idx = i+1
        while idx:
            res += self.bit[idx]
            idx -= idx&(-idx)
        return res

    # Ai += x
    # O(log N)
    def add(self,i,x):
        idx = i+1
        while idx < self.N:
            self.bit[idx] += x
            idx += idx&(-idx)
    
    # min_i satisfying {sum(A0 ~ Ai) >= w} (Ai >= 0)
    # O(log N)
    def lower_left(self,w):
        if (w < 0):
            return -1
        x = 0
        k = 1<<(self.N.bit_length()-1)
        while k > 0:
            if x+k < self.N and self.bit[x+k] < w:
                w -= self.bit[x+k]
                x += k
            k //= 2
        return x

n,q = map(int,rd().split())
c = list(map(int,rd().split()))
lr = []
for i in range(q):
    l,r = map(int,rd().split())
    lr.append([l,r,i])
lr.sort(key = lambda x:x[1])

last = [-1]*(n+1)
bit = BIT(n+1)
ans = [0]*q
current = 1
for i in range(q):
    # rightに達するまで
    while current <= lr[i][1]:
        # 今の色が過去にあった場合、bitの値を0に戻す
        if last[c[current-1]] != -1:
            bit.add(last[c[current-1]], -1)
        last[c[current-1]] = current
        bit.add(current, 1)
        current += 1
    ans[lr[i][2]] = bit.query(lr[i][1]) - bit.query(lr[i][0] - 1)

print(*ans, sep = "\n")