Problem:
Score : 600 points

Problem Statement

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?

Constraints

1\leq N,Q \leq 5 \times 10^5

1\leq c_i \leq N

1\leq l_i \leq r_i \leq N

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N Q
c_1 c_2 \cdots c_N
l_1 r_1
l_2 r_2
:
l_Q r_Q

Output

Print Q lines. The i-th line should contain the response to the i-th query.

Sample Input 1

4 3
1 2 1 3
1 3
2 4
3 3

Sample Output 1

2
3
1

The 1-st, 2-nd, and 3-rd balls from the left have the colors 1, 2, and 1 - two different colors.

The 2-st, 3-rd, and 4-th balls from the left have the colors 2, 1, and 3 - three different colors.

The 3-rd ball from the left has the color 1 - just one color.

Sample Input 2

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

Sample Output 2

1
2
2
1
2
2
6
3
3
3

Solution in Haskell (GHC 8.8.3) (judge verdict: Time Limit Exceeded)

import           Control.Monad
import           Data.List
import qualified Data.IntMap.Strict            as IntMap
import           Data.IntMap.Strict             ( IntMap )
import           Control.Monad.Trans.State.Strict
import           Data.Maybe

data Query = Update Int Int | Query Int Int Int
data Count = Count Int Int
data SegTree a = SegTree (Int, Int) (SegTree a) (SegTree a) a | Leaf Int a

getQuery :: (Int, [Int]) -> Query
getQuery (i, [l, r]) = Query i l r

sortingKey :: Query -> Int
sortingKey (Update _ prev) = prev
sortingKey (Query _ l _  ) = l

getNextQuery :: Int -> State (IntMap Int, Int) Query
getNextQuery ci = do
  (m, i) <- get
  put (IntMap.insert ci i m, i + 1)
  return $ Update i $ IntMap.findWithDefault 0 ci m

build :: (Int, Int) -> SegTree Int
build (l, r) | l + 1 == r = Leaf l 0
             | otherwise  = SegTree (l, r) (build (l, mid)) (build (mid, r)) 0
  where mid = (l + r) `div` 2

update :: Int -> SegTree Int -> SegTree Int
update _ (Leaf key value) = Leaf key (value + 1)
update key (SegTree (l, r) lt rt value)
  | key < mid = SegTree (l, r) (update key lt) rt (value + 1)
  | otherwise = SegTree (l, r) lt (update key rt) (value + 1)
  where mid = (l + r) `div` 2

count :: (Int, Int) -> SegTree Int -> Int
count a@(l, r) b@(Leaf key value) | l <= key && key < r = value
                                  | otherwise           = 0
count a@(l1, r1) b@(SegTree (l2, r2) lt rt value)
  | r1 <= l2 || r2 <= l1 = 0
  | l1 <= l2 && r2 <= r1 = value
  | otherwise            = count (l1, r1) lt + count (l1, r1) rt

getCount :: Query -> State (SegTree Int) (Maybe Count)
getCount (Update i p) = do
  st <- get
  put $ update i st
  return Nothing
getCount (Query i l r) = do
  st <- get
  return $ Just $ Count i $ count (l, r + 1) st

main :: IO ()
main = do
  [n, q]  <- map read . words <$> getLine
  c       <- map read . words <$> getLine
  queries <-
    map getQuery . zip [1 ..] . map (map read . words) <$> replicateM q getLine
  let allQueries =
        queries ++ evalState (sequence $ map getNextQuery c) (IntMap.empty, 1)
  let counts =
        catMaybes
          $ evalState (sequence $ map getCount $ sortOn sortingKey allQueries)
          $ build (1, n + 1)
  sequence_ $ map (print . (\(Count _ ans) -> ans)) $ sortOn
    (\(Count i _) -> i)
    counts