Блог пользователя prabal_1997

Автор prabal_1997, история, 7 лет назад, По-английски

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.

  • Проголосовать: нравится
  • -19
  • Проголосовать: не нравится

»
7 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

We sum all entries of element in range?

»
7 лет назад, # |
  Проголосовать: нравится +8 Проголосовать: не нравится

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 лет назад, # ^ |
      Проголосовать: нравится -24 Проголосовать: не нравится

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

    • »
      »
      »
      7 лет назад, # ^ |
        Проголосовать: нравится +8 Проголосовать: не нравится

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

      Mo's C++ Code