# Hostel visits problem

Can you please tell me what is wrong with my code.

			while(k!=(i+1)){//updated this cond
a[i]=pq.top();
i++;
pq.pop();
}
cout<<pq.top()<<endl;
if(k>1){
for(int j=k-2;j>=0;j--){
}
}


Here update this 2 places and your code is good to go

You will still suffer TLE ,for that mantain your heap of size K at max

The following problem can be easily solved by using heaps.
For every query of type 1, insert elements until the size of the heap becomes ‘k’.
Then for every query of type 1 after reaching the size k for heap(max-heap) we will check if the current element is smaller than the root of the heap or not. If it is not smaller then we ignore it else we remove the root of the heap and push the new element in the heap. (What this will do is maintain a heap of size k which will contain k nearest coordinates for the dean) .
For every query of type 2 just print the root of the heap.

will check in half n hr

1 Like

Hey @rahul.gupta03111999
Make x,y long long instead of int

@Kartikkhariwal1 here the range for x and y is: -10^6 to 10^6, then why to take long long instead of int. as far my knowledge, we should take long long for the number greater than 10^9, right!
Make me correct if I am wrong.

btw could successfully submit after taking long long instead of int. Just want to know the reason for taking long long instead int.

Thanks!

Hey @swastikswarupmeher
Its because later you will do xx+yy which will overflow for int ,hence making x and y long long will make sure that xx+yy does not overflow.