DQUERY - D-query


Given a sequence of n numbers a1, a2, ..., an and a number of d-queries. A d-query is a pair (i, j) (1 ≤ i ≤ j ≤ n). For each d-query (i, j), you have to return the number of distinct elements in the subsequence ai, ai+1, ..., aj.

Input

  • Line 1: n (1 ≤ n ≤ 30000).
  • Line 2: n numbers a1, a2, ..., an (1 ≤ ai ≤ 106).
  • Line 3: q (1 ≤ q ≤ 200000), the number of d-queries.
  • In the next q lines, each line contains 2 numbers i, j representing a d-query (1 ≤ i ≤ j ≤ n).

Output

  • For each d-query (i, j), print the number of distinct elements in the subsequence ai, ai+1, ..., aj in a single line.

Example

Input
5
1 1 2 1 3
3
1 5
2 4
3 5

Output
3
2
3 

hide comments
wsrstf: 2018-07-14 16:48:01

I guess this can be solved using slide window method? Sort all query segments and everything is much easier then.

karan_yadav: 2018-07-10 09:19:03

Was searching for literature on MO (also called Query square root decomposition), found some. I'll list the best of em here:
Literature : https://blog.anudeep2011.com/mos-algorithm/
Video : https://www.youtube.com/watch?v=hqaRYgsLpUI

Note: This video by gkcs is based on the article by Anudeep. So I'd recommend giving the article a read before watching the video.

lamia2658: 2018-07-07 08:52:33

easy MO

ankur314: 2018-06-17 09:05:23

purely MO in 0.34 sec

ankur_dhir95: 2018-06-16 18:30:41

Use fast I/O if solving with persistent seg tree (costed me a tle) , though normal I/O would work with offline segment tree solution

horizon121: 2018-06-10 09:24:24

Beware ::
Value of n is 3e5+10 . Costed me a WA

elmer_fudd: 2018-05-10 20:39:15

i try to solve using direct sqrt decomposition but RTE

my code : ***************see notes below**************

it is possible or not?

Last edit: 2018-05-10 22:58:27
mrvincy: 2018-05-04 03:08:23

max(n) is 3e5 and not 3e4

coderslegacy: 2018-04-23 00:44:12

Ac in one go use Mo's Algorithm

akshatjain02: 2018-04-14 23:22:18

Solved it using both Mo's Algorithm and Offline Segment Tree :')


Added by:Duc
Date:2008-10-26
Time limit:0.227s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO NODEJS PERL6 VB.NET
Resource:© VNOI