prabal_1997's blog

By prabal_1997, history, 7 years ago, In English

question goes like this.... given an array of n nos.and queries q the queries follow like this given range l to r (l<=r) for every query we have to find sum of elements which have an odd frequency in the given range

note : 1 based indexing is followed.............................................................................................

example : 5......n

1 2 2 2 1....elements

3..queries...

1 3...

1 4......

1 5......

output: 1..... 7..... 6.....

explanation : 1. in range 1 to 3 only 1 has odd frequecy i.e 1 so ans =1 !!! 2.in range 1 to 4 1 has frequency 1 and 2 has frequecy 3 so ans = 1*1 + 2*3 = 6 !!!

I am not able to solve this......question if time limit is 1 second.

  • Vote: I like it
  • -19
  • Vote: I do not like it

| Write comment?
»
7 years ago, # |
  Vote: I like it 0 Vote: I do not like it

We sum all entries of element in range?

»
7 years ago, # |
  Vote: I like it +8 Vote: I do not like it

Using the standard Mo's algorithm with two pointers should work within 1 second time limit for N ≤ 105. To keep the frequencies and the answer, do the following...

  • Let freq[x] be the frequency of number x. Initially all frequencies are 0 and ans = 0. Parity changes with every insertion/removal, so suppose we shrink/expand current range and we need to add/remove number x. If freq[x] is currently odd, we perform ans = ans - x * freq[x] and then update freq[x], and if, on the other hand, freq[x] is currently even, we first update freq[x] and then perform ans = ans + x * freq[x].
  • »
    »
    7 years ago, # ^ |
      Vote: I like it -24 Vote: I do not like it

    i know this but i am not able to code this....

    • »
      »
      »
      7 years ago, # ^ |
        Vote: I like it +8 Vote: I do not like it

      This is how I usually code Mo's...

      Mo's C++ Code