# | User | Rating |
---|---|---|
1 | jiangly | 3976 |
2 | tourist | 3815 |
3 | jqdai0815 | 3682 |
4 | ksun48 | 3614 |
5 | orzdevinwang | 3526 |
6 | ecnerwala | 3514 |
7 | Benq | 3482 |
8 | hos.lyric | 3382 |
9 | gamegame | 3374 |
10 | heuristica | 3357 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | -is-this-fft- | 166 |
3 | Um_nik | 161 |
3 | atcoder_official | 161 |
5 | djm03178 | 157 |
6 | Dominater069 | 156 |
7 | adamant | 154 |
8 | luogu_official | 152 |
9 | awoo | 151 |
10 | TheScrasse | 147 |
Name |
---|
Ok so first of all, lets talk about
set
andordered_set
,set
saves values in some order (usually increasing order), but you cant take the "index" of some value, for example, if we declareset<int> = {2,3,9,7}
, we know that the "index" of 9 is 3, and "index" of 7 is 2, but there is no methods that you can call to get that "index".So if you want to know the index of some ordered values, you can use the
ordered_set
(in this code is used asos_set
) that as theset
, save the values in some order, and has a method calledorder_of_key()
that does exactly what we need, give the index of some value.Now, lets talk about the ideia of the question: Lets supose that in one query the value asked is A, and A appear K times in the array, you will look to the first appearance of A, show his index, and than move A to the start. So if you query for the same A again and again, you will always look to the same first appearance of A, never looking for the others K-1 A's in the array, so doesnt matter if A appear K times, you will only need the first appearance of A.
So, the idea in this solution is to save the first appearance for all A, as in
if (!id.count(a)) id[a] = i;
, then save all the indexes of the array in an ordered_set, as inost.insert(i);
. Then, for all queries, we look for the index of A, lets call it B, and then search the index of B in theos_set
, after this, we remove B of theos_set
, and add the new index as the lowest index at the moment.At the start, the indexes look as 0 to n-1, after the first query, the lowest index will be -1, then -2, and so on.
For the sample, we have the array as
2 1 1 4 3 3 1
, index of all distinct values are:(2,0), (1,1), (4,3), (3,4)
, and theordered_set
has all the indexes{0,1,2,3,4,5,6}
. Then, we query for 3, its index is 4, the index of 4 is 4 in the os_set. So the answer is 4 + 1 = 5 (as the question is 1-indexed). Then we remove the index 4 to theos_set
and update the index of 3 as -1.(2,0), (1,1), (4,3), (3,4)
changes to(2,0), (1,1), (4,3), (3,-1)
and{0,1,2,3,4,5,6}
changes to{-1,0,1,2,3,5,6}
(erase 4, add -1).We query for 2, its index is 0, so we ask for index of 0 in the
os_set
, and the answer is 1+1 = 2. We again remove 0 of theos_set
and add -2, as the new index of 2.(2,0), (1,1), (4,3), (3,-1)
changes to(2,-2), (1,1), (4,3), (3,-1)
and{-1,0,1,2,3,5,6}
changes to{-2,-1,1,2,3,5,6}
. (erase 0, add -2).And so on.
I hope this helps you, sorry for my poor english.
got it, thank you bro :)