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

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

Hi,

Can anyone help me with this problem?

You are given an array with n integers a1, a2, ..., an , a integer K and m queries.

There are two types of query:

  1. give you two integers l and r and ask you to print how many number of integers al, al + 1, ..., ar which equal to K.

  2. give you three integers l, r and v and ask you to add the value of al, al + 1, ..., ar by v.

You should print the answer of the first queries.

Thanks, Sorry for my bad English!

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

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

Do you know how to solve the second type of queries ?

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

    It can be done with segment trees (Lazy propagation). but I don't have a good idea to solve both types of queries.

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

I've proposed such a problem for a contest several years ago. After a long discussing with IGMs I strongly believe (but still have no proof) that this problem can be solved only be SQRT-decomposition in .

Basic idea: split an array into several chunks of size , for each chunk store a multiset of numbers in it and a (lazy-propagated) value which should be added to each of them.

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

I think we can do it using segment tree. Store at each Node Merge Sorted array of its children with two extra variables one for lazy and let the other be named to_subtract.

Use Lazy propagation!

At each query instead of finding K you have to find the frequency of number = K-to_subtract . This can be done in O(LOG N ) using upper bound and lower bound on the saved array as it was sorted once!

Note : during the process we never change the array! So the complexity will be O(Log N ^ 2)

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

    Your code fails on this case: N = 4, and array is {1, 1, 1, 1}.
    First query is +1 in [1, 2].
    Second is number of 1s in [1, 4].
    Your idea will print 4 instead of 2.

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

      Yeah you are right! I was thinking of same query as the update operation!

      We will have to update the array ! So the approach is totally useless !

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

As far as I know it is impossible to solve general problem in O(n * log(n)).(I remember that Haghani said that is impossible, but not I'm not sure).

But it can solved in O(n * sqrt(n)) easily.also if you have an array of K's you can done it in O(n * sqrt(n) * k).

But there is one case of problem that can be solved in O(n * log(n)) if we know that K is always smaller(or greater) than all of ai s. keep in segment node a pair: {frequency_of_minimum,minimum}.