|
1 | 1 |
|
2 |
| -Let us say we have an array of $1$�s and $ 0$�s. How do we find the sum of intervals of this array and support quick updates ? |
3 |
| -Segment tree. This is just a simple sum tree. Now, let us reduce the above problem to this question. |
4 |
| - |
| 2 | +Let us say we have an array of $1$’s and $ 0$’s. How do we find the sum of intervals of this array and support quick updates ? |
| 3 | +Segment tree. This is just a simple sum tree. Now, let us reduce the above problem to this question. |
5 | 4 |
|
6 | 5 |
|
7 |
| -(Firstly, observe that the number of distinct elements in the range $[L, R]$ is equal to the number of elements who�s last occurence $[1, R]$ is $>= L$. ) |
8 |
| - |
9 | 6 |
|
| 7 | +(Firstly, observe that the number of distinct elements in the range $[L, R]$ is equal to the number of elements who’s last occurence $[1, R]$ is $>= L$. ) |
10 | 8 |
|
11 |
| -Let us maintain an array of length $ N$. |
12 | 9 |
|
13 |
| -$A[i] = 1$, if position $i$ is the last occurence of some element. |
14 | 10 |
|
15 |
| -$A[i] = 0$, otherwise. |
16 |
| - |
17 |
| -Now, one by one, we will insert elements into this tree. |
| 11 | +Let us maintain an array of length $ N$. |
18 | 12 |
|
| 13 | +$A[i] = 1$, if position $i$ is the last occurence of some element. |
19 | 14 |
|
20 |
| -Let us say the current element is $v$ |
21 |
| -We make the $A[L[v]] = 0$ and $A[i] = 1$ |
| 15 | +$A[i] = 0$, otherwise. |
| 16 | + |
| 17 | +Now, one by one, we will insert elements into this tree. |
| 18 | + |
| 19 | + |
| 20 | +Let us say the current element is $v$ |
| 21 | +We make the $A[L[v]] = 0$ and $A[i] = 1$ |
22 | 22 |
|
23 | 23 | Where$ L[v] $represents the last occurence of $v $
|
24 |
| - |
| 24 | +
|
25 | 25 | Prior to this insertion.
|
26 |
| - |
| 26 | +
|
27 | 27 | Update the sum tree accordingly.
|
28 | 28 |
|
29 |
| - |
30 |
| - |
| 29 | +
|
| 30 | +
|
31 | 31 | Now, check if any query ends at $i$.
|
32 |
| - |
33 |
| -For all queries with $R = i$ |
| 32 | +
|
| 33 | +For all queries with $R = i$ |
34 | 34 |
|
35 | 35 | There is sufficient information to answer it.
|
36 | 36 |
|
37 | 37 | We know the last occurence of every element upto $R$.
|
38 | 38 | We just need to check how many of them are after $L$
|
39 | 39 |
|
40 |
| - |
41 |
| -Since, the array $A$ holds $1$ if it is the last occurence, we just find the sum $[L, R]$ |
42 | 40 |
|
43 |
| -The number of $1$�s in that range gives us the number of elements who�s last occurence $>= L$ |
44 |
| - |
| 41 | +Since, the array $A$ holds $1$ if it is the last occurence, we just find the sum $[L, R]$ |
| 42 | + |
| 43 | +The number of $1$’s in that range gives us the number of elements who’s last occurence $>= L$ |
| 44 | + |
45 | 45 |
|
46 | 46 |
|
47 | 47 | Now, how do we efficiently get all queries with $R = i $efficiently without degrading to $O(NQ)$
|
48 | 48 |
|
49 |
| - |
50 |
| - |
| 49 | +
|
| 50 | +
|
51 | 51 | Treat everything as an event.
|
52 | 52 |
|
53 |
| -There are two kinds of events - Sum events and Query events. |
54 |
| - |
55 |
| - |
56 |
| -For insertion event, we need ---> Position, value. |
| 53 | +There are two kinds of events - Insertion events and Query events. |
| 54 | + |
| 55 | + |
| 56 | +For insertion event, we need ---> Position, value. |
| 57 | + |
57 | 58 |
|
58 |
| - |
59 | 59 | For query event, we need ---> Left, right, query_no.
|
60 |
| - |
61 |
| - |
62 |
| -Sort all the events by the following criteria ---> (Position, for insertion), (Right, for queries) |
63 |
| - |
64 | 60 |
|
65 |
| -This is $O((N + Q) log(N + Q))$ |
66 |
| - |
| 61 | + |
| 62 | +Sort all the events by the following criteria ---> (Position, for insertion), (Right, for queries) |
| 63 | + |
| 64 | + |
| 65 | +This is $O((N + Q) log(N + Q))$ |
| 66 | + |
67 | 67 | Then go through the events one by one.
|
68 | 68 |
|
69 |
| - |
70 |
| -If it�s an insertion, set $A[L[v]] = 0$ and $A[i] = 1$ |
71 |
| - |
| 69 | +
|
| 70 | +If it’s an insertion, set $A[L[v]] = 0$ and $A[i] = 1$ |
| 71 | +
|
72 | 72 | with appropriate updates on the sum tree.
|
73 | 73 |
|
74 |
| - |
75 |
| - |
76 |
| -If it�s a query event, return the number of $1$�s (sum) in the range $[L, R]$ |
| 74 | +
|
| 75 | +
|
| 76 | +If it’s a query event, return the number of $1$’s (sum) in the range $[L, R]$ |
77 | 77 |
|
78 | 78 | ----------------------------------------------------------------------------
|
79 | 79 |
|
@@ -201,4 +201,4 @@ int main()
|
201 | 201 | printf("%d\n", answer[i]);
|
202 | 202 |
|
203 | 203 | return 0;
|
204 |
| -} |
| 204 | +} |
0 commit comments