Hostel Visit problem

How can I optimize this code, it is showing TLE

#include<bits/stdc++.h>
using namespace std;

class Hostel{
public:
int x;
int y;
Hostel(int x,int y){
this->x=x;
this->y=y;
}
int distance(){
return((xx) + (yy));
}
void print(){
cout<<distance()<<endl;
}
};

class HostelCompare{
public:
bool operator () (Hostel a, Hostel b){
return (a.distance()>b.distance());
}
};

int x[1000000],y[1000000];

int main() {
priority_queue<Hostel, vector, HostelCompare> pq1,pq2;
int k,k1,q,a,i=0; cin>>q>>k; k1=k;
while(q–){
cin>>a;
if(a==1){
cin>>x[i]>>y[i];
Hostel h(x[i],y[i]); i++;
pq1.push(h);
}
if(a==2){
k–;
while(k–){
Hostel h1=pq1.top();
pq2.push(h1);
pq1.pop();
} k=k1-1;
Hostel h=pq1.top();
h.print();
while(k–){
Hostel h2=pq2.top();
pq1.push(h2);
pq2.pop();
}
k=k1;
}
}
return 0;
}

Hi Deepak, save you code on ide.codingblocks.com and share the link here.

Here is the link
https://ide.codingblocks.com/s/62239
second test case is showing TLE

Hi Deepak, you’re getting TLE because the test case at the backend is very large. So you will need to optimise your code further.

Hey Deepak, as you are not responding to this thread, I am marking your doubt as Resolved for now. Re-open it if required.

Please mark your doubts as resolved in your course’s “ Ask Doubt ” section, when your doubt is resolved.