Problem: We have N colored balls arranged in a row from left to right; the color of the i-th ball from the left is c_i.  You are given Q queries. The i-th query is as follows: how many different colors do the l_i-th through r_i-th balls from the left have?

Submission attempts (in order):
N,Q=map(int,input().split())
C=list(map(int,input().split()))
LR=sorted([[i]+list(map(int,input().split())) for i in range(Q)],key=lambda x:x[2])
from numba import*
import numpy as np
@jitclass((('N',i8),('data',i8[:])))
class BIT:
    def __init__(self,N):
        self.N=N
        self.data=np.zeros(N+1,dtype=np.int64)
    def add(self,i,x):
        i+=1
        while i<=self.N:
            self.data[i]+=x
            i+=i&-i
    def sum(self,a,b):
        r=0
        i=b
        while i>0:
            r+=self.data[i]
            i-=i&-i
        i=a
        while i>0:
            r-=self.data[i]
            i-=i&-i
        return r
b=BIT(N)
p=[-1]*N
q=0
a=[0]*Q
for i in range(N):
    if p[C[i]-1]>=0:
        b.add(p[C[i]-1],-1)
    p[C[i]-1]=i
    b.add(i,1)
    while q<Q and LR[q][2]==i+1:
        a[LR[q][0]]=b.sum(LR[q][1]-1,LR[q][2])
        q+=1
print(*a,sep='\n')

def main(N,Q,C,LR):
    data=[0]*(N+1)
    def add(i,x):
        i+=1
        while i<=N:
            data[i]+=x
            i+=i&-i
    def sum(a,b):
        r=0
        i=b
        while i>0:
            r+=data[i]
            i-=i&-i
        i=a
        while i>0:
            r-=data[i]
            i-=i&-i
        return r
    p=[-1]*N
    q=0
    a=[0]*Q
    for i in range(N):
        if p[C[i]-1]>=0:
            add(p[C[i]-1],-1)
        p[C[i]-1]=i
        add(i,1)
        while q<Q and LR[q][2]==i+1:
            a[LR[q][0]]=sum(LR[q][1]-1,LR[q][2])
            q+=1
    return a

import sys
if sys.argv[-1]=='ONLINE_JUDGE':
    from numba import*
    from numba.pycc import CC
    cc=CC('my_module')

    def cc_export(f,signature):
        cc.export(f.__name__,signature)(f)
        return njit(f)

    main=cc_export(main,(i8,i8,i8[:],i8[:,:]))
    cc.compile()
    exit()

N,Q=map(int,input().split())
import numpy as np
C=np.array(input().split(),dtype=np.int64)
LR=sorted([[i]+list(map(int,input().split())) for i in range(Q)],key=lambda x:x[2])
LR=np.array(LR)

from my_module import main
print(*main(N,Q,C,LR),sep='\n')

from numba import*
@njit(cache=1)
def main(N,Q,C,LR,A):
    data=[0]*(N+1)
    def add(i,x):
        i+=1
        while i<=N:
            data[i]+=x
            i+=i&-i
    def sum(a,b):
        r=0
        i=b
        while i>0:
            r+=data[i]
            i-=i&-i
        i=a
        while i>0:
            r-=data[i]
            i-=i&-i
        return r
    p=[-1]*N
    q=0
    a=[0]*Q
    for i in range(N):
        if p[C[i]-1]>=0:
            add(p[C[i]-1],-1)
        p[C[i]-1]=i
        add(i,1)
        while q<Q and LR[A[q]][1]==i+1:
            a[A[q]]=sum(LR[A[q]][0]-1,LR[A[q]][1])
            q+=1
    return a

import sys
def input(): return sys.stdin.buffer.readline()
N,Q=map(int,input().split())
import numpy as np
C=np.array(input().split(),dtype=np.int64)
LR=np.array(sys.stdin.buffer.read().split(),dtype=np.int64).reshape(Q,2)
A=np.argsort(LR[:,1])
print(*main(N,Q,C,LR,A),sep='\n')


Verdicts: Time Limit Exceeded

Time Limit Exceeded

Accepted