Was trying https://www.codechef.com/problems/ROBOTG
sbumitted the following code and got TLE:
//#define DEBUG //comment when you have to disable all debug macros.
//#define LOCAL //uncomment for testing from local file
#define NDEBUG //comment when all assert statements have to be enabled.
#include <iostream>
#include <cstring>
#include <sstream>
#include <fstream>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <bitset>
#include <climits>
#include <ctime>
#include <algorithm>
#include <functional>
#include <stack>
#include <queue>
#include <list>
#include <deque>
#include <sys/time.h>
#include <iomanip>
#include <cstdarg>
#include <utility> //std::pair
#include <cassert>
#define fd(i,a) for(i=1;i<=a;i++)
#define fa(i,a,b) for(i=a;i<=b;i++)
#define fs(i,a,b,c) for(i=a;i<=b;i+=c)
#define tr(c,i) for(typeof(c.begin()) i = (c).begin(); i != (c).end(); i++)
#define present(c,x) ((c).find(x) != (c).end())
#define all(x) x.begin(), x.end()
#define clr(x,y) memset(x,y,sizeof x)
#define log2(x) (log(x)/log(2))
#define ARRAY_SIZE(arr) (1[&arr]-arr)
#define INDEX(arr,elem) (lower_bound(all(arr),elem)-arr.begin())
#define equals(a,b) (a.compareTo(b)==0) //for strings only
template<class P, class Q> inline void smin(P &a, Q b) { if (b < a) a = b; }
template<class P, class Q> inline void smax(P &a, Q b) { if (a < b) a = b; }
#define pb push_back
#define mp make_pair
#define lld long long int
#define MOD 1000000007
#define gcd __gcd
using namespace std;
#ifdef DEBUG
#define debug(args...) {dbg,args; cerr<<endl;}
#else
#define debug(args...) // Just strip off all debug tokens
#endif
struct debugger
{
template<typename T> debugger& operator , (const T& v)
{
cerr<<v<<" ";
return *this;
}
}dbg;
template<class T>
inline void inputInt(T &n )
{
n=0;
T ch=getchar_unlocked();
/*int sign=1;
while(( ch < '0' || ch > '9') && ch!='-')
ch=getchar_unlocked();
if(ch=='-')
{
sign=-1;
ch=getchar_unlocked();
}
while( ch >= '0' && ch <= '9' )
n = (n<<3)+(n<<1) + ch-'0', ch=getchar_unlocked();
n*=sign;*/
while( ch < '0' || ch > '9')
ch=getchar_unlocked();
while( ch >= '0' && ch <= '9' )
n = (n<<3)+(n<<1) + ch-'0', ch=getchar_unlocked();
}
inline void inputStr(string &str){
str="";
char ch = getchar_unlocked();
debug(ch);
int i=0;
while(ch<33){ch=getchar_unlocked();}
while(ch!='\n'){str+=ch;ch=getchar_unlocked();}
}
int main()
{
#ifdef LOCAL
freopen("input.in","r",stdin);
#endif
int T,n,m;
string s;
inputInt(T);
while(T--){
inputInt(n);
inputInt(m);
inputStr(s);
int len = s.length(), L,U,R,D, h=0, v=0;
debug("yo",s);
bool safe=true;
L=U=R=D=0;
for(int i=0;i<len;i++){
if(s[i]=='L')h--;
else if(s[i]=='R')h++;
else if(s[i]=='U')v++;
else v--;
if(h<0 && abs(h)>L)L=abs(h);
else if(h>0 && abs(h)>R)R=abs(h);
if(v<0 && abs(v)>D)D=abs(v);
else if(v>0 && abs(v)>U)U=abs(v);
debug(h,v,L,R,U,D,m,n);
if(L+R>=m || U+D>=n){
safe=false;
break;
}
}
printf("%s\n",safe?"safe":"unsafe");
}
}
when i accepted input from cin, it worked fine. Why is it getting stuck.