The input begins with an integer \$q, 1 \leq q \leq 10^5\$, the number of queries. The next \$q\$ lines each gives a query. A query is \$\text{"a x"}, 1 \leq x \leq 10^9\$
Print the result of each query on a single line. For each a query, print the last integer in the queue that is smaller than \$x\$. If there is no such integer, print \$-1\$.
Why my code could cause time limit exceeded?
#include <iostream>
#include <list>
using namespace std;
list<int> mylist;
int searchSmall(){
list<int>::const_iterator i;
for (i = mylist.begin(); i != mylist.end(); ++i){
if(*i < mylist.front()){
return *i;
}
}
return -1;
}
int main (){
int n;
ios_base::sync_with_stdio(false);
cin >> n;
while(n--){
char e;
cin >> e;
if(e == 'a'){
int q;
cin >> q;
mylist.push_front(q);
cout << searchSmall() << endl;
}
}
return 0;
}