Codeforces Round 1133 (Div. 1, Div. 2) is coming soon! Register here. ×

retr0coderxyz's blog

By retr0coderxyz, history, 6 years ago, In English

Hello guys,

I was trying to solve Distinct Colors: link.

I did a naive solution which obviously led to TLE.

TLE

I saw various solutions that use BIT. But I cant assimilate why BIT works here, what BIT represents here (for e.g. in case range sum query BIT keeps track of cummulative frequency.)

It would be great if someone tells some trick how to use BIT for various other purposes too.

Thanks!

  • Vote: I like it
  • +3
  • Vote: I do not like it

| Write comment?
»
6 years ago, hide # |
 
Vote: I like it +8 Vote: I do not like it

It's simple. Simply transform your tree into an array by doing a pre-order traversal. Then, notice that a subtree corresponds to a subarray. Now, you just need to solve the problem of finding the number of distinct elements in a subarray (which is actually SPOJ DQUERY).

»
6 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

LanceTheDragonTrainer gave some valuable insights, but still its not enough for me to pick up. I honestly don't understand "(Sweeping from left to right), for each color in your array, store the index of the next occurence of that color. If there is none, use infinity." works or what to do after that. It will be great if anybody could explain me in a well detailed manner out of your busy time, I would be really grateful.

Thank you!

  • »
    »
    6 years ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    Notice that each distinct color will have only one index where the value of next_element is larger than your right end.