### Duelist1234's blog

By Duelist1234, history, 2 months ago,

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

 » 2 months ago, # |   0 i think it could be done with segment trees look at this article: https://cp-algorithms.com/data_structures/segment_tree.html
 » 2 months ago, # |   0 What about BIT?
•  » » 2 months ago, # ^ |   0 or just use set
•  » » » 2 months ago, # ^ |   0 and multiset can solve this problem : sum of l ~ r a[i]++ a[i]--
•  » » » » 2 months ago, # ^ |   0 Thanks ill look into that
•  » » » » 2 months ago, # ^ |   0 please elaborate
•  » » » » » 2 months ago, # ^ |   +3 we can think it on another side : some ball in the bag and ball has a valuequery 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 bagif the bag is a set last two query is easy and how to do the first query? we can use lower_bound and upper_boundhere we solve itfor 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 multisetMy English is bad thank you owo
•  » » » » » » 2 months ago, # ^ |   0 what will we store in the multiset? indexes of elements with a[i] = 1?
•  » » » » » » » 2 months ago, # ^ |   0 a[i] means how many of ball with value i in the bagso if a = [0, 1, 2, 1] (one base) set will be {2, 3, 3, 4}
•  » » » » » » » » 2 months ago, # ^ |   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
•  » » » » » » 2 months ago, # ^ |   +4 To find the number of balls you will need distance between 2 iterators which is $O(n)$
•  » » » » » » » 2 months ago, # ^ |   0 yes, so its O(n*q)?
•  » » » » » » » 2 months ago, # ^ |   0 you are right i forget set lower bound can't do that
•  » » » » » » » 2 months ago, # ^ |   0 but will it work if stored i such that a[i] = 1 in the set and used lowerbound to find the number of indexes in the range l to r in the set?
•  » » » » » » » 2 months ago, # ^ |   0 can't we find the difference between the iterators can't we just subtract them, wouldn't it be O(1)?
•  » » » » » » » » 2 months ago, # ^ |   0
•  » » » » » » » » » 2 months ago, # ^ |   0 We can't subtract them directly?
•  » » » » » » » 2 months ago, # ^ |   0 It is possible to subtract two iterators, but not for a std::set. We can use set from pb_ds. This is the solution to problem.
•  » » » » » » » » 2 months ago, # ^ |   0 And, certainly, we can do more complex things with Cartesian Tree.
 » 2 months ago, # |   0 i feel like you could use a bitset(N must not be 1e18 or something , then you cant)
 » 2 months ago, # |   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