You are not logged in. Please login at www.codechef.com to post your questions!

×

Sweets All Around - Need help

I was trying this problem and got an approach using segment tree lazy as:
Add 1 in range [x,y] for each n
Query to find maximum element

However, due to x,y being upto $10^9$ I am unable to implement it. I think there's a way of doing this which I am unaware of. It would be of great use if anyone can suggest me how to do this.
Here's the question link: Sweets All Around

Thanks :)

asked 02 May '18, 23:08

dishant_18's gravatar image

5★dishant_18
61419
accept rate: 12%


Hi @dishant_18 ! Actually u don't need to use lazy propagation ! It can be done using Merge Sort Tree ! What we actually need is an integer value which completely lies with the given ranges (considering each input as a range) and also that integer value should be as minimum as possible and the number of ranges satisfying this particular value should be as maximum as possible ! So,the how to pick that minimum integer ? We can say that the minimum value will be any one of the starting ranges (i.e among all the ranges pick the starting point of that particular range and count how many ranges satisfy ! ) To do this we need to sort the ranges according to their starting points ! Then iterate through all ranges pick the starting value and count how many ranges satisfy this value ! How to count ?? Build a segment tree with ending point of every range as leaf node and merge two child's to get elements in the parent node ! Then for every query of type "How many numbers are greater than the current number in the range (0,current_index) ?" , to do this we can use lower_bound on each completely overlapping segment and sum them up and return ! Out of all the queries modify the maximum value accordingly along with the minimum value as the current starting point of the range ! My AC code : https://www.hackerearth.com/submission/16653996/ If you have any doubts feel free to ask ! Happy coding :)

link

answered 03 May '18, 00:58

msd_007's gravatar image

1★msd_007
3178
accept rate: 5%

Thank you :)

(03 May '18, 19:51) dishant_185★

No Need of segment Tree. First Use coordinate compression for X and Y. Now you may observe that after doing this you will have atmost 2*10^5 distinct elements left. Now when you are compressing X and Y , just store this mapping between original number and new mapped value in a map. After you have done this process X and Y values one by one. Suppose current student's mapped range is x=mp[X] and y=mp[Y]. So adding one to a given range can be done by using this simple trick: A[x]+=1 and A[y+1]-=1. Here A is the array initialized to 0. Do this range addition for all students.

Once you have done this , just take the prefix sum from 1 to 2*10^5(in worst case). Now check all indices from 1 to 2*10^5 and find the smallest index having the maximum value. So simply this is the maximum no. of students that can be made happy and to get the minimum no. of candies just output the map value of the smallest index for which maximum value is encountered.

link

answered 03 May '18, 20:04

lakh's gravatar image

4★lakh
1395
accept rate: 23%

edited 03 May '18, 20:09

vijju123's gravatar image

5★vijju123 ♦♦
15.4k12066

I had this approach in mind but had a very raw idea tbh. Thanks for sharing.

(03 May '18, 21:22) dishant_185★
  1. Use coordinate compression on the values and build a segment tree as usual.
    Even if the range is large, the total unique values you must consider is usually not as large. For example, in this problem $X, Y \le 10^9$ but total unique values of $X$ and $Y$ is at most $2N$. So if you have $K$ distinct values you can map each of these values to an integer in $[1..K]$ (usually preserving the sorted order). Now you can build a segment tree on a range of size $K$ and solve the problem.
  2. Use a dynamic segment tree.
    This is basically a normal segment tree but you start with the root and only create nodes when you need them. Your root covers the range $[1..10^9]$. Now if you have to do an update in the left or right half, you first create the child node if it does not yet exist, and then go down into the left or right child to update it. For queries you can gather the result from the existing nodes without having to create new ones. This is slightly less efficient than option 1 because the operations will take logarithmic time with respect to large range the tree is build on, but on the plus side you can answer queries online.
link

answered 03 May '18, 04:35

meooow's gravatar image

6★meooow ♦
7.1k718
accept rate: 48%

Thank you :)

(03 May '18, 19:51) dishant_185★
toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×1,755
×176

question asked: 02 May '18, 23:08

question was seen: 237 times

last updated: 03 May '18, 21:22