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

Автор Duelist1234, история, 4 месяца назад, По-английски

So the problem is as following: You are given two integers n and q , and there is an array length n with all zeroes. There are q operations of 3 types: -output the sum in array a from i to j; -a[i] becomes 1; -a[i] becomes 0.

I know this can be done using segment trees but I think that is complicating it.

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

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

i think it could be done with segment trees look at this article: https://cp-algorithms.com/data_structures/segment_tree.html

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

What about BIT?

  • »
    »
    4 месяца назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    or just use set

    • »
      »
      »
      4 месяца назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      and multiset can solve this problem :

      • sum of l ~ r
      • a[i]++
      • a[i]--
      • »
        »
        »
        »
        4 месяца назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        Thanks ill look into that

      • »
        »
        »
        »
        4 месяца назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        please elaborate

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

          we can think it on another side :

          some ball in the bag and ball has a value

          query l~r -> count ball that in the bag and value is l~r a[i]=1 -> put a ball with value i in the bag a[i]=0 -> remove a ball with value i from bag

          if the bag is a set last two query is easy and how to do the first query? we can use lower_bound and upper_bound

          here we solve it

          for the first problem ball's value are different so we can use set and the second (my) problem ball's value maybe same so we use multiset

          My English is bad thank you owo

          • »
            »
            »
            »
            »
            »
            4 месяца назад, # ^ |
              Проголосовать: нравится 0 Проголосовать: не нравится

            what will we store in the multiset? indexes of elements with a[i] = 1?

            • »
              »
              »
              »
              »
              »
              »
              4 месяца назад, # ^ |
                Проголосовать: нравится 0 Проголосовать: не нравится

              a[i] means how many of ball with value i in the bag

              so if a = [0, 1, 2, 1] (one base)

              set will be {2, 3, 3, 4}

              • »
                »
                »
                »
                »
                »
                »
                »
                4 месяца назад, # ^ |
                  Проголосовать: нравится 0 Проголосовать: не нравится

                a[i] can never be 2, as we are not incrementing it, we're just setting a[i] = 1 or a[i] = 0, so a[i] has only 2 possible values 1 and 0

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

            To find the number of balls you will need distance between 2 iterators which is $$$O(n)$$$

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

i feel like you could use a bitset(N must not be 1e18 or something , then you cant)

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

As already writted, you can use Fenwick tree(binary indexed tree), it's very quick to implement, but not so easy to understand. If you want to know easy to understand solution with at least decent time complexity, you can look into the sqrt decomposition