Some one can hel me in this problem?? In an array of length at most 100000 and each number is <= 1000000. I need answer many querys (at most 100000) of: in an interval [a,b] : a < b, how many different numbers are inside of the interval
Some one can hel me in this problem?? In an array of length at most 100000 and each number is <= 1000000. I need answer many querys (at most 100000) of: in an interval [a,b] : a < b, how many different numbers are inside of the interval
| # | User | Rating |
|---|---|---|
| 1 | Kevin114514 | 3722 |
| 2 | Benq | 3676 |
| 3 | jiangly | 3640 |
| 4 | turmax | 3450 |
| 5 | tourist | 3408 |
| 6 | strapple | 3360 |
| 7 | Um_nik | 3354 |
| 8 | squareOf105 | 3348 |
| 9 | maroonrk | 3196 |
| 10 | idontwannadothis…e | 3191 |
| # | User | Contrib. |
|---|---|---|
| 1 | maroonrk | 153 |
| 2 | Qingyu | 152 |
| 3 | nik_exists | 151 |
| 4 | AmShZ | 147 |
| 4 | maspy | 147 |
| 6 | Um_nik | 143 |
| 7 | Errichto | 139 |
| 8 | adamant | 136 |
| 9 | BledDest | 134 |
| 10 | AksLolCoding | 132 |
| Name |
|---|



Look Here
Spoj — Dquery My Solution: The result of a query [a, b] is number of integers whose last occurrence in [1, b] is >= a. http://pastebin.com/j9g9CHAL
Another nice idea would be the following one. Let's calculate next[i] = the next position in the array where the value is equal to a[i]. If such a position doesn't exist next[i] = array.lenght + 1. For example, if the initial array is 1,3,1,2,3, then next[1]=3, next[2]=5, next[3]=6, next[4]=6, next[5]=6. Now if we represent the points (i,next[i]) in a cartesian plane, a query [a,b] means the number of points in the rectangle [a..b] x [b+1..N+1] where N is the number of points. This can be done in O(N*log(N)) using segment trees.
Hope it helps!
this can be simplified if we go offline:
sort queries by left bound, then for each query [li, ri]: