Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
N, Q = map(int, input().split())
a = list(map(int, input().split()))

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):
    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 lowerbound(self, s):
    x = 0
    y = 0
    for i in range(self.n.bit_length(), -1, -1):
      k = x + (1 << i)
      if k <= self.n and (y + self.data[k] < s):
        y += self.data[k]
        x += 1 << i
    return x + 1
qs = []
for i in range(Q):
  l, r = map(int, input().split())
  qs.append((l, r, i))
qs.sort(key = lambda x: x[1])
last = [0] * (N + 1)
fwk = BIT(N)
for i in range(qs[0][1]): last[a[i]] = max(last[a[i]], i + 1)
for x in last:
  if x: fwk.add(x, 1)
#print(last)
res = [0] * Q
rlast = qs[0][1]
for l, r, i in qs:
  #print(l, r)
  if rlast == r:
    res[i] = fwk.get(l - 1, r)
  else:
    for x in range(rlast, r):
      if last[a[x]]: fwk.add(last[a[x]], -1)
      fwk.add(x + 1, 1)
      last[a[x]] = x + 1
    rlast = r
    res[i] = fwk.get(l - 1, r)
for r in res: print(r)

Fixed submission:
import sys
input = sys.stdin.readline
N, Q = map(int, input().split())
a = list(map(int, input().split()))

class SegTree:
  def segfunc(self, x, y):
    return x + y
  def __init__(self, n, ide_ele, init_val):
    #####単位元######
    self.ide_ele = ide_ele
    #num:n以上の最小の2のべき乗
    self.num = 2 ** (n - 1).bit_length()
    self.seg = [self.ide_ele] * 2 * self.num
    #set_val
    for i in range(n):
      self.seg[i + self.num - 1] = init_val[i]    
    #built
    for i in range(self.num - 2, -1, -1) :
      self.seg[i] = self.segfunc(self.seg[2 * i + 1], self.seg[2 * i + 2]) 
  def update(self, k, x):
    k += self.num - 1
    self.seg[k] = x
    while k + 1:
      k = (k - 1) // 2
      self.seg[k] = self.segfunc(self.seg[k * 2 + 1], self.seg[k * 2 + 2]) 
  def query(self, p, q):
    if q <= p:
      return self.ide_ele
    p += self.num - 1
    q += self.num - 2
    res = self.ide_ele
    while q - p > 1:
      if p & 1 == 0:
        res = self.segfunc(res, self.seg[p])
      if q & 1 == 1:
        res = self.segfunc(res, self.seg[q])
        q -= 1
      p = p // 2
      q = (q - 1) // 2
    if p == q:
      res = self.segfunc(res, self.seg[p])
    else:
      res = self.segfunc(self.segfunc(res, self.seg[p]), self.seg[q])
    return res
qs = []
for i in range(Q):
  l, r = map(int, input().split())
  qs.append((l, r, i))
qs.sort(key = lambda x: x[1])
last = [0] * (N + 1)
fwk = SegTree(N + 1, 0, last)
for i in range(qs[0][1]): last[a[i]] = i + 1
for x in last:
  if x: fwk.update(x, fwk.query(x, x + 1) + 1)
#print(last)
res = [0] * Q
rlast = qs[0][1]
for l, r, i in qs:
  #print(l, r)
  if rlast == r:
    res[i] = fwk.query(l, r + 1)
  else:
    for x in range(rlast, r):
      if last[a[x]]: fwk.update(last[a[x]], fwk.query(last[a[x]], last[a[x]] + 1) - 1)
      fwk.update(x + 1, fwk.query(x + 1, x + 2) + 1)
      last[a[x]] = x + 1
    rlast = r
    res[i] = fwk.query(l, r + 1)
for r in res: print(r)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
N, Q = map(int, input().split())
a = list(map(int, input().split()))

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):
    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 lowerbound(self, s):
    x = 0
    y = 0
    for i in range(self.n.bit_length(), -1, -1):
      k = x + (1 << i)
      if k <= self.n and (y + self.data[k] < s):
        y += self.data[k]
        x += 1 << i
    return x + 1
qs = []
for i in range(Q):
  l, r = map(int, input().split())
  qs.append((l, r, i))
qs.sort(key = lambda x: x[1])
last = [0] * (N + 1)
fwk = BIT(N)
for i in range(qs[0][1]): last[a[i]] = max(last[a[i]], i + 1)
for x in last:
  if x: fwk.add(x, 1)
#print(last)
res = [0] * Q
rlast = qs[0][1]
for l, r, i in qs:
  #print(l, r)
  if rlast == r:
    res[i] = fwk.get(l - 1, r)
  else:
    for x in range(rlast, r):
      if last[a[x]]: fwk.add(last[a[x]], -1)
      fwk.add(x + 1, 1)
      last[a[x]] = x + 1
    rlast = r
    res[i] = fwk.get(l - 1, r)
for r in res: print(r)

Fixed submission:
import sys
input = sys.stdin.readline
N, Q = map(int, input().split())
a = list(map(int, input().split()))

class SegTree:
  def segfunc(self, x, y):
    return x + y
  def __init__(self, n, ide_ele, init_val):
    #####単位元######
    self.ide_ele = ide_ele
    #num:n以上の最小の2のべき乗
    self.num = 2 ** (n - 1).bit_length()
    self.seg = [self.ide_ele] * 2 * self.num
    #set_val
    for i in range(n):
      self.seg[i + self.num - 1] = init_val[i]    
    #built
    for i in range(self.num - 2, -1, -1) :
      self.seg[i] = self.segfunc(self.seg[2 * i + 1], self.seg[2 * i + 2]) 
  def update(self, k, x):
    k += self.num - 1
    self.seg[k] = x
    while k + 1:
      k = (k - 1) // 2
      self.seg[k] = self.segfunc(self.seg[k * 2 + 1], self.seg[k * 2 + 2]) 
  def query(self, p, q):
    if q <= p:
      return self.ide_ele
    p += self.num - 1
    q += self.num - 2
    res = self.ide_ele
    while q - p > 1:
      if p & 1 == 0:
        res = self.segfunc(res, self.seg[p])
      if q & 1 == 1:
        res = self.segfunc(res, self.seg[q])
        q -= 1
      p = p // 2
      q = (q - 1) // 2
    if p == q:
      res = self.segfunc(res, self.seg[p])
    else:
      res = self.segfunc(self.segfunc(res, self.seg[p]), self.seg[q])
    return res
qs = []
for i in range(Q):
  l, r = map(int, input().split())
  qs.append((l, r, i))
qs.sort(key = lambda x: x[1])
last = [0] * (N + 1)
fwk = SegTree(N + 1, 0, last)
for i in range(qs[0][1]): last[a[i]] = i + 1
for x in last:
  if x: fwk.update(x, fwk.query(x, x + 1) + 1)
#print(last)
res = [0] * Q
rlast = qs[0][1]
for l, r, i in qs:
  #print(l, r)
  if rlast == r:
    res[i] = fwk.query(l, r + 1)
  else:
    for x in range(rlast, r):
      if last[a[x]]: fwk.update(last[a[x]], fwk.query(last[a[x]], last[a[x]] + 1) - 1)
      fwk.update(x + 1, fwk.query(x + 1, x + 2) + 1)
      last[a[x]] = x + 1
    rlast = r
    res[i] = fwk.query(l, r + 1)
for r in res: print(r)