What i should change to avoid TLE

#include<bits/stdc++.h>
#include
#include

using namespace std;
void printp(priority_queue<long long ,vector,greater>pq,int k)
{
while(k–>1)
{
pq.pop();
}
cout<<pq.top()<<endl;
}
int main()
{
priority_queue<long long ,vector,greater>pq;
int Q,k;
cin>>Q>>k;
while(Q–>0)
{
long long query;
cin>>query;
if(query==2 && pq.size()>=k)
{
printp(pq,k);
}

  else 
  {
	  long long  x,y;
	  cin>>x>>y;
	  pq.push(abs(x*x)+abs(y*y));
  }  
}
return 0;

}

@Ankit_kumar_3003,
For every query, you are popping k times. This gives a total complexity of O(Q.k.log(pq_size()).
If you observe carefully, you don’t need to store all the distances till the time of next query, some of the values would obviously be useless to store. Also if you do that, i.e at any moment maintain a heap that has K nearest hostels, you can answer the query in O(log(k)) time i.e just by printing the top element of pq.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.