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

class BIT:
    def __init__(self, n):
        self.size = n
        self.tree = [0]*(n+1)
 
    def sum(self, i):
        # [0, i) の要素の総和を返す
        s = 0
        while i>0:
            s += self.tree[i]
            i -= i & -i
        return s
    # 0 index を 1 index に変更  転倒数を求めるなら1を足していく
    def add(self, i, x):
        i += 1
        while i <= self.size:
            self.tree[i] += x
            i += i & -i


n,q = map(int,input().split())
c = list(map(int,input().split()))
Q = []
for i in range(q):
    l,r = map(int,input().split())
    Q.append([l,r,i])

Q.sort(key=lambda x: x[1])

bit = BIT(n)
ind = [-1]*(n+1)

ans = [0]*q
now = 0
for l,r,num in Q:
    while now < r:
        x = c[now]
        if ind[x] == -1:
            ind[x] = now
            bit.add(now,1)
        else:
            bit.add(ind[x],-1)
            ind[x] = now
            bit.add(now,1)
        now += 1
    ans[num] = bit.sum(r)-bit.sum(l-1)
for i in ans:
    print(i)



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

class BIT:
    def __init__(self, n):
        self.size = n
        self.tree = [0]*(n+1)
 
    def sum(self, i):
        # [0, i) の要素の総和を返す
        s = 0
        while i>0:
            s += self.tree[i]
            i -= i & -i
        return s
    # 0 index を 1 index に変更  転倒数を求めるなら1を足していく
    def add(self, i, x):
        i += 1
        while i <= self.size:
            self.tree[i] += x
            i += i & -i


n,q = map(int,input().split())
c = list(map(int,input().split()))
Q = []
for i in range(q):
    l,r = map(int,input().split())
    Q.append([l,r,i])

Q.sort(key=lambda x: x[1])

bit = BIT(n)
ind = [-1]*(n+1)

ans = [0]*q
now = 0
for l,r,num in Q:
    while now < r:
        x = c[now]
        if ind[x] == -1:
            ind[x] = now
            bit.add(now,1)
        else:
            bit.add(ind[x],-1)
            ind[x] = now
            bit.add(now,1)
        now += 1
    ans[num] = bit.sum(r)-bit.sum(l-1)
for i in ans:
    print(i)

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

def main():       
    def build(list):
        for i,x in enumerate(list,n):
            d = set()
            d.add(x)
            seg[i] = d
            
        for i in range(n-1,0,-1):
            seg[i] = seg[i<<1]|seg[i<<1 | 1]

        ## take the value of [l,r) 
    def get(l,r):
        l += n
        r += n
        res = set()

        while l < r:
            if l & 1:
                res |= seg[l]
                l += 1
            if r & 1:
                res |= seg[r-1]
            l >>= 1
            r >>= 1
        return len(res)

    n,q = map(int,input().split())
    c = list(map(int,input().split()))
    seg = [set()]*(2*n)
    build(c)

    for i in range(q):
        l,r = map(int,input().split())
        print(get(l-1,r))
if __name__ == "__main__":
    main()

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

class BIT:
    def __init__(self, n):
        self.size = n
        self.tree = [0]*(n+1)
 
    def sum(self, i):
        # [0, i) の要素の総和を返す
        s = 0
        while i>0:
            s += self.tree[i]
            i -= i & -i
        return s
    # 0 index を 1 index に変更  転倒数を求めるなら1を足していく
    def add(self, i, x):
        i += 1
        while i <= self.size:
            self.tree[i] += x
            i += i & -i


n,q = map(int,input().split())
c = list(map(int,input().split()))
Q = []
for i in range(q):
    l,r = map(int,input().split())
    Q.append([l,r,i])

Q.sort(key=lambda x: x[1])

bit = BIT(n)
ind = [-1]*(n+1)

ans = [0]*q
now = 0
for l,r,num in Q:
    while now < r:
        x = c[now]
        if ind[x] == -1:
            ind[x] = now
            bit.add(now,1)
        else:
            bit.add(ind[x],-1)
            ind[x] = now
            bit.add(now,1)
        now += 1
    ans[num] = bit.sum(r)-bit.sum(l-1)
for i in ans:
    print(i)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
class segtree:
    ## define what you want to do ,(min, max)
    

    def __init__(self,n):
        self.n = n
        self.tree = [[]]*(2*n)
        
    def build(self, list):
        for i,x in enumerate(list,self.n):
            d = set()
            d.add(x)
            self.tree[i] = d
            
        for i in range(self.n-1,0,-1):
            self.tree[i] = self.tree[i<<1]|self.tree[i<<1 | 1]

    ## take the value of [l,r) 
    def get(self,l,r):
        l += self.n
        r += self.n
        res = set()

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

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

seg = segtree(n)
seg.build(c)

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

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

class BIT:
    def __init__(self, n):
        self.size = n
        self.tree = [0]*(n+1)
 
    def sum(self, i):
        # [0, i) の要素の総和を返す
        s = 0
        while i>0:
            s += self.tree[i]
            i -= i & -i
        return s
    # 0 index を 1 index に変更  転倒数を求めるなら1を足していく
    def add(self, i, x):
        i += 1
        while i <= self.size:
            self.tree[i] += x
            i += i & -i


n,q = map(int,input().split())
c = list(map(int,input().split()))
Q = []
for i in range(q):
    l,r = map(int,input().split())
    Q.append([l,r,i])

Q.sort(key=lambda x: x[1])

bit = BIT(n)
ind = [-1]*(n+1)

ans = [0]*q
now = 0
for l,r,num in Q:
    while now < r:
        x = c[now]
        if ind[x] == -1:
            ind[x] = now
            bit.add(now,1)
        else:
            bit.add(ind[x],-1)
            ind[x] = now
            bit.add(now,1)
        now += 1
    ans[num] = bit.sum(r)-bit.sum(l-1)
for i in ans:
    print(i)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
class segtree:
    ## define what you want to do ,(min, max)
    

    def __init__(self,n):
        self.n = n
        self.tree = [[]]*(2*n)
        
    def build(self, list):
        for i,x in enumerate(list,self.n):
            d = set()
            d.add(x)
            self.tree[i] = d
            
        for i in range(self.n-1,0,-1):
            self.tree[i] = self.tree[i<<1]|self.tree[i<<1 | 1]

    ## take the value of [l,r) 
    def get(self,l,r):
        l += self.n
        r += self.n
        res = set()

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

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

seg = segtree(n)
seg.build(c)

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

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

class BIT:
    def __init__(self, n):
        self.size = n
        self.tree = [0]*(n+1)
 
    def sum(self, i):
        # [0, i) の要素の総和を返す
        s = 0
        while i>0:
            s += self.tree[i]
            i -= i & -i
        return s
    # 0 index を 1 index に変更  転倒数を求めるなら1を足していく
    def add(self, i, x):
        i += 1
        while i <= self.size:
            self.tree[i] += x
            i += i & -i


n,q = map(int,input().split())
c = list(map(int,input().split()))
Q = []
for i in range(q):
    l,r = map(int,input().split())
    Q.append([l,r,i])

Q.sort(key=lambda x: x[1])

bit = BIT(n)
ind = [-1]*(n+1)

ans = [0]*q
now = 0
for l,r,num in Q:
    while now < r:
        x = c[now]
        if ind[x] == -1:
            ind[x] = now
            bit.add(now,1)
        else:
            bit.add(ind[x],-1)
            ind[x] = now
            bit.add(now,1)
        now += 1
    ans[num] = bit.sum(r)-bit.sum(l-1)
for i in ans:
    print(i)