Skip to content

Commit 4d0fc3c

Browse files
Update D-Query Explanation.txt
1 parent 8aeea1d commit 4d0fc3c

File tree

1 file changed

+42
-42
lines changed

1 file changed

+42
-42
lines changed

Explanations/Explanations- 3/D-Query Explanation.txt

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,79 @@
11

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.
54

65

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-
96

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$. )
108

11-
Let us maintain an array of length $ N$.
129

13-
$A[i] = 1$, if position $i$ is the last occurence of some element.
1410

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$.
1812

13+
$A[i] = 1$, if position $i$ is the last occurence of some element.
1914

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$
2222

2323
Where$ L[v] $represents the last occurence of $v $
24-
24+
2525
Prior to this insertion.
26-
26+
2727
Update the sum tree accordingly.
2828

29-
30-
29+
30+
3131
Now, check if any query ends at $i$.
32-
33-
For all queries with $R = i$
32+
33+
For all queries with $R = i$
3434

3535
There is sufficient information to answer it.
3636

3737
We know the last occurence of every element upto $R$.
3838
We just need to check how many of them are after $L$
3939

40-
41-
Since, the array $A$ holds $1$ if it is the last occurence, we just find the sum $[L, R]$
4240

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+
4545

4646

4747
Now, how do we efficiently get all queries with $R = i $efficiently without degrading to $O(NQ)$
4848

49-
50-
49+
50+
5151
Treat everything as an event.
5252

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+
5758

58-
5959
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-
6460

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+
6767
Then go through the events one by one.
6868

69-
70-
If its an insertion, set $A[L[v]] = 0$ and $A[i] = 1$
71-
69+
70+
If its an insertion, set $A[L[v]] = 0$ and $A[i] = 1$
71+
7272
with appropriate updates on the sum tree.
7373

74-
75-
76-
If its a query event, return the number of $1$s (sum) in the range $[L, R]$
74+
75+
76+
If its a query event, return the number of $1$s (sum) in the range $[L, R]$
7777

7878
----------------------------------------------------------------------------
7979

@@ -201,4 +201,4 @@ int main()
201201
printf("%d\n", answer[i]);
202202

203203
return 0;
204-
}
204+
}

0 commit comments

Comments
 (0)