kartik8800's blog

By kartik8800, 6 months ago, In English

Hello Codeforces, In this blog I will try to write a well detailed editorial for the CSES Range Queries section. The motivation for this editorial comes from https://codeforces.com/blog/entry/70018.

Quoting icecuber "I think CSES is a nice collection of important CP problems, and would like it to have editorials. Without editorials users will get stuck on problems, and give up without learning the solution. I think this slows down learning significantly compared to solving problems with editorials. Therefore, I encourage others who want to contribute, to write editorials for other sections of CSES."

So here I am writing an editorial for the range queries section.

If you find any error or maybe have a better solution to some problem please do share.

Range Sum Queries I

Given array is A[1..N], for each query of form (L,R) we need to output A[L] + A[L+1] + ... + A[R].
Define an array Prefix such that Prefix[i] = A[1] + A[2] + .. + A[i].
Prefix[R] = A[1] + A[2] + ... + A[R] and Prefix[L-1] = A[1] + A[2] + ... + A[L-1].
Consider Prefix[R] — Prefix[L-1] = (A[1] + A[2] + ... + A[R]) — (A[1] + A[2] + ... + A[L-1]) = (A[L] + A[L+1] + ... + A[R]).

So for every query simply output Prefix[R] — Prefix[L-1].

How Do I build the prefix Array.

Time complexity : O(N) to build prefix array and O(1) per query.

Range Minimum Queries I

Two possible ways are as follows :
1. Build a Range minimum query segment tree in O(N) time and answer each query in O(logN).
2. Build a sparse table in O(NlogN) time and answer each query in O(1).

Code for approach 1

Refer https://codeforces.com/blog/entry/71101 for my segment tree template.

Approach 2 : https://cp-algorithms.com/data_structures/sparse-table.html

Range Sum Queries II

So this one is a direct use of segment tree with point updates and I shall use my segtree template to answer this problem.

Code

Both of these queries can be performed in logN time.
Overall time complexity is O(N) for building segtree and QlogN for Q queries.

Range Minimum Queries II

Again a straightforward segment tree problem and I will use a similar code as I used for the previous problem. This is the same as RMQ1 except that here we also have point updates. Segment tree solution for RMQ1 and RMQ2 will be identical.

Code

Both of these queries can be performed in logN time.
Overall time complexity is O(N) for building segtree and QlogN for Q queries.

Range Xor Queries

For this problem we can maintain a segment tree where each node of the tree will store the xor-sum of the range of elements for which it is responsible.
So root of the tree stores : A[1]^A[2]^....^A[N].

To calculate the answer for a particular interior node of the tree we do :
NODE_VAL = LEFT_CHILD_VAL ^ RIGHT_CHILD_VAL
For leaf nodes :
NODE_VAL = A[x], where [x,x] is the range this leaf node is responsible for and if x > N then NODE_VAL = 0 as 0 is the identity for xor_sum.

Again with these observations we can use the same segtree template as follows :

Code

AC code : https://ideone.com/mPhqas

Range Update Queries

Nice question which can be directly solved with a segment tree with lazy propagation but that is an overkill plus my segtree library does not support lazy propagation as of now.

Let's define a few terms:
SUM[i] = overall update done on the ith element till now
Initially SUM[i] = 0 for all i as no updates have yet been performed, now we would like to track the updates happening so that our answer to a query 2 k can easily be v[k] + SUM[k] where v is the initial array.

How to efficiently maintain the SUM array? Let us build a range sum query segment tree on some array X which has all elements initialized to 0.

DEFINE : RSQ[i] = X[1] + X[2] + .. + X[i] = rangeSumQuery(1,i)

Now say we have a query which tells us to add u to all elements in the range [l,r] then if I perform a point update and make X[l] = X[l] + u think what happens to RSQ[i] for different values of i.

RSQ[i] is unaffected for all i where i < l
and RSQ[i] = RSQ[i] + u for all i >= l.

Effectively we just did a range update on the abstract array RSQ and the range update is equivalent to adding u to all elements of array RSQ from l to N.

But we wanted the range update to be only for the range [l,r], so we should now do a range update in which we subtract u from [r+1,N] and this is the same as doing a point update to X[r+1] such that:

X[r+1] = X[r+1] — u

it must be easy to see the abstract array RSQ is nothing but the required SUM array.

So here is the algorithm :

 For every range update query (l,r,u):
 point_update(l,current_val + u)
 point_update(r+1,current_val — u)
For every query -> value at pos idx:
 print SUM[idx] + V[idx]

AC code : https://ideone.com/vBZpYx
Time complexity per query is logN.

Forest Queries

For every query of the form (x1,y1,x2,y2) we need to answer the number of trees inside the rectangle described by the top left cell of rectangle (x1,y1) and the bottom right cell of rectangle (x2,y2).

Define : DP[i][j] as the number of trees in the rectangle described by (1,1) and (i,j).

Can we use DP matrix to evaluate answers for every query?

Spoiler

Ok, but how?

Spoiler

How to build DP matrix?
Let tree[i][j] = 1, if there is a tree at cell (i,j) else tree[i][j] = 0.

 DP[0][0] = DP[-1][0] = DP[0][-1] = 0
for i from 1 to N:
   for j from 1 to N:
    DP[i][j] = DP[i-1][j] + DP[i][j-1] — DP[i-1][j-1] + tree[i][j]

Time complexity for build O(N*N) and time complexity per query is O(1).
AC code : https://ideone.com/5dGTfY

Hotel Queries

Observation : For each group, we need to find the 1st hotel with enough vacancies and book rooms for the group in that hotel.

Brute force : Start checking every hotel from left to right for the number of rooms it has. As soon as you find a hotel with enough rooms use required number of rooms in this hotel. Repeatedly do this till all groups have been assigned a hotel.

How to optimize?

How do I know if there is any hotel in the first x hotels which can be assigned to the current group?

Spoiler

Algorithm :

 For each group gi with size si:
  Find the 1st hotel x such that vacancy(x) >= si.
   Do point update : vacancy(x) = vacancy(x) — si.
   Print x.
  If no valid x exists print 0.

Time complexity is O(Mlog^2N), logN steps for the binarySearch and each step of the binary search uses the Range max query segment tree which works in logN time.

List Removals

Brute force is quite simple if you simply simulate what is mentioned in the problem.
Let us try to optimize. So whenever we are asked to delete some xth element of the list we need to first locate the xth element, print it and then delete it.

How can we make the above processes faster?
Let us keep a boolean array PRESENT of size N and PRESENT[i] = 1 if the ith element of the list has not yet been deleted, 0 otherwise.

Now let us say we have the query : delete the xth element of the list, then this means we are going to delete the element at the jth index of the initial list such that :

  1. PRESENT[j] = 1.
  2. sum of PRESENT[i] for all i from 1 to j = x.

Why above conditions are necessary and sufficient to locate the correct element?
If are we deleting the element it has to be present in the list currently and so PRESENT[j] should be 1.
If this element at index j(of the initial list) is the xth element of the list(current state of the list) then there are exactly x elements present in the list in range [1,j](of the initial list) and remaining j — x elements got deleted in some previous queries.

How do I find this j?

Spoiler

Ok, elaborate.

Spoiler

How do I find number of elements not yet deleted in the range [1,j]?

Hint : problem comes from range query section

Once you have found the correct j, you need to print it and also mark PRESENT[j] = 0 and make the required point update in the segment tree.

Time complexity analysis is similar to previous problem.

AC code : https://ideone.com/anpuXy

Salary Queries

Okay so, this seems a bit hard. Maybe if the max possible salary of the employees was limited to some smaller amount(instead of a billion) we might be able to solve it.

So try solving the problem under the constraint that p,a,b <= 10^7.
Now the problem is much easier if I maintain the number of people with a given salary, let us define
freq[i] : number of employees with the salary i
We may now build a range sum query segment tree on this array and to answer a query we simply calculate the sum of the range [a,b].

For updating the salary of some employee from x to y, we do the point updates freq[x] -= 1 and freq[x] += 1 because now 1 less employee has salary x and 1 more employee has the salary y.

But the problem is not solved, since we needed to do this for max possible salary = 1billion, but now we know how to do it for 10^7.

observation

So lets group the salaries into 10^7 buckets and each bucket represent a range of 100 different contiguous salary values. The 0th bucket represents salaries from 1 to 100 and ith bucket represents the salaries from (i)*100 + 1 to (i+1)*100.

These buckets will store aggregated number of employees that have salaries in the given range represented by the bucket.

Now for query [a,b] : all the buckets that are entirely in the range [a,b] their aggregate values should be taken and summed up and for the 2 partial buckets(or 1) not entirely included in the range [a,b] we shall do a brute force.

So build a segment tree over the buckets and calculate the sum over all completely included buckets in the range [a,b]. For remaining partially included buckets do a brute force(actually iterate over approx 100 possible values and choose to include those which are required by a particular query, refer code).

A code will make this explanation much more clear.

AC code : https://ideone.com/zg97c8

Time Complexity?

other way to do it is using a dynamic segment tree in which you only build a node of the tree when it is needed.

Distinct Values Queries

This is a direct application of the MO's Algorithm. You may read more about MO's algorithm on https://blog.anudeep2011.com/mos-algorithm/

The brute force can be done by simply iterating from index a to b and maintaining number of distinct elements seen till now and a frequency array to indicate which elements and how many occurrences of those elements is present.

Frequency[i] = count of occurences of i in the current range.

Next we try to build the required ADD and REMOVE functions which help MO's algorithm to function properly.
To ADD a new element in the current range simply check if this element is already present(frequency > 0) and if it is present just increase its frequency else if its frequency was 0 then make it 1 and also increase the number of unique elements in the range.
To REMOVE an element from the current range, decrement its frequency by 1, if its frequency reaches 0 then decrease the number of distinct elements in the current range.

After this sort the queries as described by MO's algorithm and you are done.

Twist : We cannot use frequency array as value of individual element can go upto 10^9. So what I'll simply use an unordered_map?

No, unordered_map solution will time out due to high constant factor involved.

How to continue using the frequency array?

Time complexity : O((N+Q)root(N))

AC code : https://ideone.com/RkC547

Subarray Sum Queries

Let us try to keep track of the max sum subarray in a particular range [L,R]. If we were to build a segment tree in which each node of the tree stores max sum subarray of the range that the node is responsible for then the root keeps track of max sum subarray in the range [1,N].
However for segment trees to be efficient we need to generate the answer of interior nodes of the tree using the answers/information provided by the child nodes.

Now let's try to generate the answer for some interior node P of the segment tree assuming that we already have the answers for the children of the node P.

Node P is responsible for the range [l,r], its left child is responsible for the range [l,mid] and its right child is responsible for the range [mid+1,r].

Now we need to find the sum of max sum subarray in the range [l,r].
Assume you have all necessary information about the child nodes but if you have some information about a child node you also need to generate that piece of information for the parent node as well(since this node also has a parent which will use information given by P to generate it's answer).

How to relate the answer for parent to the answers about children?
Agreed, now what info to keep for every node?

So to summarize, for every node which represents the range [l,r] we should store :
1. sum of max sum subarray in the range [l,r].
2. maximum possible sum of some prefix [l,x] (such value of x is chosen, such that l <= x <= r and sum of elements in range [l,x] is maximum possible.)
3. maximum possible sum of some suffix [x,r].

We now know how to calculate sum of max sum subarray for some node using the above mentioned information about children nodes but as discussed we should also calculate prefix and suffix info for the parent also.

How to calculate prefix and suffix for parent node

Refer the combine function in the code for more clarity.
AC code : https://ideone.com/MhVmBs

Time complexity : logN per update.

Forest Queries II

Problem is almost the same as Forest Queries but we also have point updates.
I will discuss two approaches, one of them is quite efficient while the other one struggles to run in the time limit(passes if constant factor is low).

Approach 1

Let us do build the same DP matrix which we had built for the problem Forest Queries. If somehow we are able to keep our DP matrix consistent with the updates then our answer will be the same as before.

ANSWER TO QUERY (x1,y1,x2,y2) : DP[x2][y2] — DP[x1-1][y2] — DP[x2][y1-1] + DP[x1-1][y1-1]

How to efficiently track the updates that happen on some entry DP[i][j]?

Which entries of the DP matrix change if cell (i,j) is updated?

Alright, so now I need to efficiently add or subtract 1 from all the matrix entries (x,y) where that x >= i and y >= j.

So how do we track these updates?

Time complexity O(Q*N*logN)
AC code : https://ideone.com/mcAdwL
Code for fenwick tree is taken from : https://cp-algorithms.com/data_structures/fenwick.html

Approach 2
This approach is more efficient and some might also find it easier to understand.
It uses a 2D Binary Indexed tree to achieve an overall time complexity of O(N^2 + Q*log^2(N)).

You can read more about it here : TopCoder-2DBIT

Range Updates and Sums

No surprises here. My solution will use the data structure and the technique related to that data structure which most would have already guessed after reading the problem statement. The important thing would be a thorough understanding of the concept and a neat implementation(I have tried to make it readable).

what data structure, what technique?

Alright so our segment tree needs to support the following operations :
1. increase values in range [l,r] by x.
2. set values in range [l,r] = x.
3. return sum of values in range [l,r].

Think about what information should we store per node of the tree, so that we are able to lazily update our nodes when a new update comes in and we are able to propagate the updates downward when needed.

To better understand lazy propagation, I recommend reading this : Super amazing theory in 1 comment (My implementation uses applyAggr and compose functions mentioned here).

What info to store per node?
How do I find the actual Sum using these variables?
How do i propagate these updates downward?

Time Complexity is logN per query.
If something is not explained or if something isn't clear feel free to ask but I recommend understanding lazy prop well before attempting the problem or reading the editorial.
AC code : https://ideone.com/8HQxMk

Please feel free to point out mistakes, suggest better/alternate solutions and to contribute.
I'd be glad to know if this helps :)

P.S. Will add remaining problems in a few days.

UPD : Editorial is almost complete with 2 problems left. 2nd last uses segment tree with lazy prop and I am guessing the last one uses some kind of persistent data structure, will add soon.

 
 
 
 
  • Vote: I like it
  • +181
  • Vote: I do not like it

»
5 months ago, # |
  Vote: I like it 0 Vote: I do not like it

nice one thanks.

if possible , can you please provide all the other links for cses editorials like these 2 by you and icecuber

»
5 months ago, # |
  Vote: I like it +2 Vote: I do not like it

Nice, I'll come back here when I solve those problems.

»
5 months ago, # |
  Vote: I like it 0 Vote: I do not like it

can you provide editorials for the graph section as well

  • »
    »
    5 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I'll see to it, however there are a lot of problems in the graph section. Maybe sometime in the future I'll write an editorial on some of the selected problems from that section.

»
5 months ago, # |
Rev. 9   Vote: I like it +3 Vote: I do not like it

Distinct values queries can also be done using segment tree (with sorted vector in each node) in $$$\mathcal{O}(N\log^2N)$$$.

Let's say your original array is $$$A$$$. For each index $$$i$$$, you store the smallest index $$$j$$$ such that $$$A[i] = A[j]$$$ but $$$i < j$$$. Let the array of $$$j$$$ values be $$$B$$$. Build segment tree over $$$B$$$. For each query $$$[L,R]$$$, you just need to check how many values in each of the $$$\mathcal{O}(\log N)$$$ segments have value $$$> R$$$ via binary search.

Of course, this only works if there are no updates. Also, MO's algorithm can be adapted to solve more difficult range query problems (e.g. range query for most frequent element can be done in $$$\mathcal{O}(Q\sqrt{N}$$$)).

Upd: Here is my implementation of this solution.

  • »
    »
    5 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Nice approach, I am guessing this should work.
    Do contribute the code if you get the time for that.

    • »
      »
      »
      5 months ago, # ^ |
      Rev. 9   Vote: I like it +8 Vote: I do not like it

      Sure. I will implement it when I have time.

      I think I have one more approach. This time it has time complexity of $$$\mathcal{O}(N\log N)$$$.

      1. Sort the queries by left endpoint.
      2. For each index $$$i$$$, you store the smallest index $$$j$$$ such that $$$A[i]=A[j]$$$ But $$$i<j$$$. Let the array of $$$j$$$ values be $$$B$$$.
      3. Create an array of 0s (with the same size as $$$A$$$). Call this array $$$C$$$. Add 1 to $$$C[i]$$$ if $$$i$$$ is the leftmost index of the value $$$A[i]$$$ in A. Build a segment tree/fenwick tree over $$$C$$$.
      4. To handle a query $$$[L,R]$$$, set $$$C[B[i]]$$$ to $$$1$$$ for $$$i < L$$$. Notice that we only need to do this once for each value of $$$i$$$ so we don't need to worry about the time complexity of this operation. Then report the sum of values in $$$[L,R]$$$ using your range tree.

      Once again, this approach only works if there are no updates.

      This is the implementation that I wrote which got AC on CSES.

  • »
    »
    5 months ago, # ^ |
    Rev. 2   Vote: I like it +8 Vote: I do not like it

    We can also solve Distinct Value Queries using Persistent Segment Tree (Online) in O(NlogN). Code — https://pastebin.com/WhkF5cCp

    • »
      »
      »
      5 months ago, # ^ |
      Rev. 8   Vote: I like it 0 Vote: I do not like it

      Ah yes we can. I think the approach is very similar to my first segment tree solution but we sort the values in $$$B$$$ in ascending order and set the $$$i^{th}$$$ index to $$$1$$$ in the $$$j^{th}$$$ version of the persistent segment tree for each $$$B[i]=j$$$. Thereafter, we just need to take the difference between sum of elements in $$$[L,R]$$$ in the $$$(n+1)^{th}$$$ and $$$R^{th}$$$ version of the segment tree for query $$$[L,R]$$$.

      Even though this works, persistence is unnecessary.

  • »
    »
    5 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    It can also be done using a persistent segment tree in $$$O(N log N + Q log N)$$$, here's how:

    Build a persistent segment tree like this:

    • Traverse the array from left to right.
    • For the element with index N, update its value in the ST to 1 and save its position using a bucket (you can use a map or coordinate compression, whichever you prefer).
    • For the remaining elements, if it is the first appearance of the element, update its value in the ST to a 1. Otherwise, first update the value of the position where it last appeared to 0 and then update its value in the ST to a 1.
    • Save the position of this element using a bucket.

    Example: For the array $$$[2, 3, 1, 3, 2]$$$, we will get the following STs:

    Element 5: $$$[0, 0, 0, 0, 1]$$$

    Element 4: $$$[0, 0, 0, 1, 1]$$$

    Element 3: $$$[0, 0, 1, 1, 1]$$$

    Element 2: $$$[0, 1, 1, 0, 1]$$$

    Element 1: $$$[1, 1, 1, 0, 0]$$$

    Note that since this is a persistent segment tree, you will have all different versions stored in memory.

    Now, to answer a query in the form $$$[a, b]$$$, we just have to do a "sum" query from $$$a$$$ to $$$b$$$ on the ST of element $$$a$$$. This works because, by construction, only one element for every value will have a 1, and this will be the leftmost one. If we have one or more elements in the range $$$[a, b]$$$ with a certain value, the "sum" query will count only one of them. If we don't have any elements with a certain value, the "sum" query will not count them.

    Total time complexity: $$$O(N log N + Q log N)$$$, for the construction of the persistent ST and for every query.

    Total memory complexity: $$$O(N log N)$$$, because we are using a persistent segment tree.

    Implementation
  • »
    »
    5 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Hey LanceTheDragonTrainer, can you explain this line: For each query [L, R], you just need to check how many values in each of the O(logN) segments have value > R via binary search.. Why do so? How does that will give us the correct answer for a query [L, R]?

    • »
      »
      »
      4 weeks ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Firstly, do you understand what are the values in the segment tree?

      • »
        »
        »
        »
        4 weeks ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Yes, that segment tree is also called merge sort tree and it stores sorted vector at each node. Sorted values for each segment.

        • »
          »
          »
          »
          »
          4 weeks ago, # ^ |
          Rev. 2   Vote: I like it 0 Vote: I do not like it

          Ok. What are the values in the array from which you would use to build the segment tree? Hint: You do not use the input array. You need to transform it.

          • »
            »
            »
            »
            »
            »
            4 weeks ago, # ^ |
              Vote: I like it 0 Vote: I do not like it

            Hey I read your original comment, I'm repeating again. For each query [L,R], you just need to check how many values in each of the O(logN) segments have value >R. Why counting this value will ensure the correct answer. See I'm not getting this logic.

            • »
              »
              »
              »
              »
              »
              »
              4 weeks ago, # ^ |
              Rev. 3   Vote: I like it 0 Vote: I do not like it

              Ok. I think you didn't get my intention.

              In order to answer your question, you need to understand what is the array we used to build the segment tree.

              Say, our input array is: [2, 1, 4, 3, 3, 1, 2, 2].

              Our transformed array would be: [6, 5, 8, 4, 8, 8, 7, 8] assuming zero-based indexing. To understand how I got this array, read my original comment.

              Now build segment tree over the transformed array.

              • »
                »
                »
                »
                »
                »
                »
                »
                4 weeks ago, # ^ |
                  Vote: I like it 0 Vote: I do not like it

                Thanks, finally got it. I was not getting why a query on the B array will ensure the correct result for each [L, R].
                Now I got it, all those numbers in [L, R] whose B[i] is greater than R is unique in this range.

                Pardon my stupidity. Thanks again.

»
5 months ago, # |
  Vote: I like it 0 Vote: I do not like it

loved it thanks a lot for your time

»
5 months ago, # |
  Vote: I like it +3 Vote: I do not like it

Thank you so much kartik8800 You did a Greatjob! I Bookmarked this page!!

  • »
    »
    5 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I'm glad you liked it. thanks for the appreciation :)

»
5 months ago, # |
  Vote: I like it 0 Vote: I do not like it

for List Removals life can be easier with this data structure https://codeforces.com/blog/entry/10355

  • »
    »
    5 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    have you implemented it using that? can you please share the code?

    • »
      »
      »
      5 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it
      Spoiler
»
5 months ago, # |
  Vote: I like it +8 Vote: I do not like it

Hello just a doubt that how do i build my segment tree for this question : Range Update Queries like i will be using lazy propagation for the range updates, ADDEND to increase value in the range and getValue to get the value at the kth index. Can you please help. https://ideone.com/h2Mo9u this is my segment tree template

  • »
    »
    5 months ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    You don't need lazy prop for range sum updates and point queries. Instead of storing the array directly, store the differences between consecutive elements. Then each update can be expressed as two point updates at the ends of the interval, and each query is just a prefix sum query on the new array, which is easily solvable with a BIT or segment tree without lazy prop.

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

      thank you for replying yeah i have solved this question using segment tree already similar to author one, but i wanted to see if range increment(addend in case of lazzy prop can be used) to solve this question however i am unable to do it. here is the link to my template can you please guide me on solving this using lazy prop ( i somehow want to use that addend function in my template) https://ideone.com/h2Mo9u

      • »
        »
        »
        »
        5 months ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Yeah, of course you can solve it with range updates and point queries on a segment tree using lazy prop. If you are learning how to do this for the first time, I suggest reading this starting from page 245. Go through the implementation carefully and test your code along the way. It's also ok to look at other people's implementations of segment trees to get an idea of how it should work.

  • »
    »
    5 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    This is possibly the best theory about lazy propagation that I have read till now : https://codeforces.com/blog/entry/44478?#comment-290116

    Give it a read, you might find implementing lazy propagation easier.

»
5 months ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

You can solve Salary Queries offline with binary search and bit. here's my code https://pastebin.com/VG6JZaZt .

  • »
    »
    5 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Can u explain your approach, like if you are using binary search on answer then how are u managing to check for this value whether it can be answer or not?

    • »
      »
      »
      5 months ago, # ^ |
      Rev. 3   Vote: I like it 0 Vote: I do not like it

      We can add all the salaries that appear in the initial salaries array & in the queries to a vector ve. Updates: Let x be the salary of an employee we will get the index k of x in ve and add one to k in the BIT, we'll do the same thing to subtract the old salary of this employee from the BIT.Queries: we will get the indices of l and r in ve using bs and answer the queries from bit.

      time complexity : O(nlogn+qlogn)

      • »
        »
        »
        »
        3 months ago, # ^ |
          Vote: I like it +3 Vote: I do not like it

        hi your solution works but i have no clue how. i mean why are there so many continue; ? and also why are you using visited every time? please help i really want learn from your solution

»
5 months ago, # |
  Vote: I like it +3 Vote: I do not like it

Salary Queries can be solved for arbitrarily large values in the array using coord compression to get all values in the range $$$[1..N]$$$, then just use a BIT, segment tree, or whatever your favorite data structure is to solve it. The implementation is very messy, so I wouldn't recommend coding it, but it ends up taking $$$O(n\log n+q\log n)$$$.

  • »
    »
    5 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Seems reasonable but can you explain what happens when the salary of an employee gets updated?

    Let's say I did the coordinate compression on the salaries [2,6,8] and got [1,2,3]. Now the query says change 6 to 3, and 6 is mapped to 2, what do you change the compressed_number(2) to?

    Little confused on how you will work with updates.

    • »
      »
      »
      5 months ago, # ^ |
      Rev. 2   Vote: I like it +6 Vote: I do not like it

      You have to take into account future updates when running the coord compression. For instance, append all future values to the array, compress it, and then remove them again. You would also do the same thing for both endpoints of all queries.

      So I guess the complexity is actually $$$O((n+q)\log(n+q))$$$, this is basically the same thing though given the constraints.

      • »
        »
        »
        »
        5 months ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Ah, got it!

        Makes complete sense, thanks for sharing.

      • »
        »
        »
        »
        4 months ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        That was my approach and it led me to TLE, I'm using a set and a map to compress, and a FenwickTree to perform queries later on. I haven't tried compressing with vectors + binary search, but I don't think it would make a difference, any help will be appreciated

        • »
          »
          »
          »
          »
          4 months ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          Well apparently compressing with vectors + binary search is allowed to pass meanwhile set + maps isn't, since my code is AC now after that modification

        • »
          »
          »
          »
          »
          4 months ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          I got AC while using a map for compression. Here's what I used:

          Code
»
5 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Anyone has any idea where to find editorial of String Algorithm section of CSES problemset?

»
5 months ago, # |
  Vote: I like it 0 Vote: I do not like it

please anyone give me HOTEL QUERIES problem solution.

»
5 months ago, # |
  Vote: I like it +5 Vote: I do not like it

xor queries can also be solved using $$$xor(l,r) = xor(xor(1,r), xor(1, l-1))$$$
right?

»
5 months ago, # |
  Vote: I like it 0 Vote: I do not like it
Range Sum Queries I
Range Minimum Queries I
Range Sum Queries II
Range Minimum Queries II
Range Xor Queries
Range Update Queries
Forest Queries
Hotel Queries
List Removals
Salary Queries
Subarray Sum Queries
Distinct Values Queries
Forest Queries II
Range Updates and Sums
Polynomial Queries
Range Queries and Copies
»
4 months ago, # |
Rev. 3   Vote: I like it +5 Vote: I do not like it

Just mention that "Distinct Values Queries" problem have segment tree solution

code
»
4 months ago, # |
  Vote: I like it 0 Vote: I do not like it

I tried solving the problem Salary Queries by the way that you have suggested. I used Fenwick Tree instead of a Segment Tree. I am getting TLE in it. However, when I tried running the inputs, the answers are coming out to be correct. Any suggestions on optimizing it? Here is the link to my code: https://ideone.com/dtWevB

  • »
    »
    4 months ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    on first look, I would say it is possible that your helper function is causing the TLE.

    Accessing an element of a map is O(logN) operation. so inside your helper you will access it 100 times -> 100*log(N), in my implementation I have eliminated this logN factor. and my helper takes 100 + logN.

    So might be the reason for TLE, try optimizing and let me know if it passes.

    • »
      »
      »
      4 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      So, do you mean to say that instead of creating a map of <int,int> I should create a map of <int,vector> so that I can first access the map element in logN and then traverse the vector in 100 operations?

      • »
        »
        »
        »
        4 months ago, # ^ |
          Vote: I like it +3 Vote: I do not like it

        Read the calc() method of my Implementation.
        I do one map access to get iterator corresponding to value lo. From there onwards I increment the iterator till the key being pointed by iterator is less than hi.

        Iterator increment is O(1) operation.

        • »
          »
          »
          »
          »
          7 weeks ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          I was also stuck with the same problem thought I might find something useful in the comments. I was stuck at it since yesterday 2 hours earlier I found my mistake but wasn't able to resolve it. So thanks a lot for your contribution.

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

        Doing this worked out for me. Thanks a lot for your help. This question really taught me a lot. Thanks again.

»
3 months ago, # |
Rev. 3   Vote: I like it +5 Vote: I do not like it

nvm got it

»
3 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Can somebody help me optimize my code for the problem Distinct Queries. I am using Mo's algorithm to solve it. The time complexity of which is O(n*sqrt(n)). However, I am getting TLE in it. The test cases provided by them are taking more than 20 seconds to run on my code. I have recently learned Mo's algorithm which is why I am unaware of various optimizations that can be done in it. Here is my link: TLE SOLUTION Thanks in advance.

  • »
    »
    3 months ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    Not sure if it is NrootN, what about the map you are using?

    • »
      »
      »
      3 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Sorry, I forgot to include the complexity for the map. So, the overall complexity is O(N*(Sqrt(N))*Log(N)). Can you suggest me a way to remove the map to store the frequencies of the elements. I am using map as the range of elements is 10^9. I would have used an array otherwise. But for the overall code, I am pretty sure that if there is no map then the complexity would be NrootN

      • »
        »
        »
        »
        3 months ago, # ^ |
          Vote: I like it +3 Vote: I do not like it

        try reading the blog solution, defines exactly how to get rid of the map.

        • »
          »
          »
          »
          »
          3 months ago, # ^ |
            Vote: I like it +5 Vote: I do not like it

          Thanks again. It worked by coordinate compression. The time changed from 20 seconds to 0.8 seconds as soon as I did that. Really appreciate your effort in helping others.

»
3 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Hotel queries can be done in O(mlogn).

Instead of using binary search, we will descend the Segment Tree, starting at the root vertex, and moving each time to either the left or the right child, depending on which segment contains the vacancy greater than the required rooms.

Code
»
7 weeks ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

pdbs can solve few of the problems in the section.
Btw, can anyone explain this solution by rainboy

»
6 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

First of all thanks for the editorial. Really appreciate this. Hey if it's okay could you check out this code for the Polynomial Queries problem. I am using lazy propagation and the AP sum concept, but seems to be getting RE on the second sub task.

Code
»
6 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

Thanks for sharing

»
6 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

If somebody's interested in how to solve the last problem (Range Queries and Copies), this article was very helpful: https://www.geeksforgeeks.org/persistent-segment-tree-set-1-introduction/

»
4 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

for salary queries if i maintain a node with two values min and max then also i can do same thing. I checked this it is giving right answer for short inputs but on submission it is giving rte. code is here: ```

 ll n,tsize; ll arr[1000000]; ll minsegtree[1000000]; ll maxsegtree[1000000];

ll minimum(ll a, ll b){ return (a<b)?a:b; } ll maximum(ll a, ll b){ return (a>b)?a:b; }

void update(ll ith, ll val, ll low, ll high, ll index) { if(low == high){ minsegtree[index] = maxsegtree[index]= val; return; } ll mid = (low + high)/2; if(ith <= mid){ update(ith, val, low, mid, 2*index + 1); }else{ update(ith, val, mid+1, high, 2*index+ 2); } minsegtree[index] = minimum(minsegtree[2*index + 1], minsegtree[2*index + 2]); maxsegtree[index] = maximum(maxsegtree[2*index +1], maxsegtree[2*index + 2]); }

ll countemps(ll lrange, ll hrange, ll low , ll high, ll index) { if(lrange > maxsegtree[index] || hrange < minsegtree[index]) return 0;

if(minsegtree[index] >= lrange && maxsegtree[index] <= hrange){
    if(high <= n-1) return (high - low + 1);
    else{
       return (n-low);
    }
}
ll mid = (low + high)/2;
return countemps(lrange, hrange, low , mid , 2*index + 1) + countemps(lrange, hrange, mid + 1, high, 2*index + 2);

}

int main() {

fastio;
ll q;
cin>>n>>q;
for(int i=0;i<n;++i){
    cin>>arr[i];
}
tsize=1;
while(tsize<n){
    tsize *=2;
}
ll temp = tsize;
tsize = tsize*2;
for(int i=0;i<tsize;++i){
    maxsegtree[i] = inf;
}
// is size of seg tree

for(int i=0;i<n;++i){
    update(i,arr[i], 0, temp-1, 0);
}

for(int i=0;i<q;++i){
    char ch;
    cin>>ch;
    if(ch == '?'){
       ll a,b;
       cin>>a>>b;
       cout<<countemps(a,b,0,temp-1,0)<<endl;
    }else{
       ll k,x;
       cin>>k>>x;
       update(k-1,x, 0,temp-1,0);
    }
}
return 0;

}

``` can anyone check please? for sample test case it is giving right output.