fork download
  1. from sys import stdin,stdout
  2. from collections import *
  3. from math import *
  4. st=lambda:list(stdin.readline().strip())
  5. li=lambda:list(map(int,stdin.readline().split()))
  6. mp=lambda:map(int,stdin.readline().split())
  7. inp=lambda:int(stdin.readline())
  8. pr=lambda n: stdout.write(str(n)+"\n")
  9.  
  10. mod=1000000007
  11. INF=float('inf')
  12. d={}
  13.  
  14. length=0
  15.  
  16. def ADD(x):
  17. global length
  18. if x not in d:
  19. length+=1
  20. d[x]=1
  21. else:
  22. d[x]+=1
  23.  
  24. def REMOVE(x):
  25. global length
  26. if d[x]==1:
  27. length-=1
  28. d.pop(x)
  29. else:
  30. d[x]-=1
  31.  
  32.  
  33.  
  34. def solve():
  35. n,q=mp()
  36. l=li()
  37. query=[li()+[i] for i in range(q)]
  38. P=int(n**0.5)
  39. query.sort(key=lambda x:(x[0]//P,x[1]))
  40. curL,curR=0,-1
  41. ans=[0 for i in range(q)]
  42. for Q in query:
  43. left,right,ind=Q
  44. left-=1
  45. right-=1
  46.  
  47. while curL>left:
  48. curL-=1
  49. ADD(l[curL])
  50.  
  51. while curR<right:
  52. curR+=1
  53. ADD(l[curR])
  54.  
  55. while curL<left:
  56. REMOVE(l[curL])
  57. curL+=1
  58.  
  59. while curR>right:
  60. REMOVE(l[curR])
  61. curR-=1
  62.  
  63. ans[ind]=length
  64.  
  65. print('\n'.join(map(str,ans)))
  66.  
  67.  
  68.  
  69. for _ in range(1):
  70. solve()
  71.  
Success #stdin #stdout 0.02s 9288KB
stdin
10 10
2 5 6 5 2 1 7 9 7 2
5 5
2 4
6 7
2 2
7 8
7 9
1 8
6 9
8 10
6 8
stdout
1
2
2
1
2
2
6
3
3
3