It is recommended to use cout << “\n”; instead of cout << endl;. endl is slower because it forces a flushing stream, which is usually unnecessary
Fast IO required.
Ohhh !!! I forget about the endl ; 
Thanks for the link, i was stuck at the same issue.
It seems like the only thing that mattered was using “\n” instead of endl. I tried using
ios_base::sync_with_stdio(false);
cin.tie(NULL);
but it didn’t work until I changed endl to “\n”. Then I tried removing
ios_base::sync_with_stdio(false);
cin.tie(NULL);
and it still worked. So basically using “\n” was the only thing needed.
Also, initially I had tried submitting in Python using sys library and “\n” and that was not working either. I’m definitely not a fan of this question.
using scanf instead of cin will remove tle
https://www.codechef.com/viewsolution/36992280
I have done exactly the same way but i am getting WA can someone pls help…
during the contest, I visited the same page and used cin.tie… fast IO but still got TLE . now , I just replaced endl by \n and it works . wish I knew before 
I did the same but got tle…why?? even used fast i/o and \n and all other things needed
#include<bits/stdc++.h>
#define ll long long
#define yes cout<<“YES”<<" “;
#define no cout<<“NO”<<endl
#define pb push_back
#define for0(i, n) for (int i = 0; i < n; i++)
#define for1(i, n) for (int i = 1; i <n; i++)
#define loop(i,a,b) for (int i = a; i < b; i++)
#define bloop(i,a,b) for (int i = a ; i>=b;i–)
#define mod 1000000007
#define f first
#define sec second
#define gcd(x,y) __gcd(x, y)
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
ll t;
cin>>t;
while(t–)
{
string s;
cin>>s;
ll x1,y1;
cin>>x1>>y1;
ll q,l=0,r=0,u=0,d=0;
//unordered_map<char,ll>m;
for(ll i=0;i<s.size();i++)
{
if(s[i]==‘R’)
r++;
if(s[i]==‘U’)
u++;
if(s[i]==‘D’)
d++;
if(s[i]==‘L’)
l++;
}
cin>>q;
while(q–)
{
ll x2,y2,x3,y3,ans=0;
cin>>x2>>y2;
x3=x2-x1;
y3=y2-y1;
bool flag=false;
if(x3>0)
{
if(x3<=r)
flag=true;
if(y3>0&&flag)
{
if(y3<=u)
{
yes;
ans+=x3+y3;
cout<<ans<<”\n";
continue;
}
}
else if(y3<=0&&flag)
{
if(abs(y3)<=d)
{
yes;
ans+=x3+abs(y3);
cout<<ans<<"\n";
continue;
}
}
if(!flag)
{
no;
continue;
}
}
else
{
if(abs(x3)<=l)
flag=true;
if(y3>0&&flag)
{
if(y3<=u)
{
yes;
ans+=abs(x3)+y3;
cout<<ans<<"\n";
continue;
}
}
else if(y3<=0&&flag)
{
if(abs(y3)<=d)
{
yes;
ans+=abs(x3)+abs(y3);
cout<<ans<<"\n";
continue;
}
}
if(!flag)
{
no;
continue;
}
}
}
}
}
even i changed all cin with scanf still got Tle…IT’s a serious issue…I guess
You have to initialise the map elements again other wise they will keep on increasing for other test cases. Hope that helps.
Yeah thats a big mistake thanks bro:+1:
For people who want the solution in C programming, refer: Competitive-Programming/Code-Chef/Help-Martha at master · strikersps/Competitive-Programming · GitHub
I added Fast IO. but i still get TLE. can anyone help me?
Solution - CodeChef: Practical coding for everyone
Edit: fixed it with ‘\n’ as someone suggested above.
Try initializing the variables in the map to 0 before performing mp[s[i]]++. That worked for me. Also, it should be “NO”, you have a typo[Maybe that’s the problem].
F
Can anyone tell why i was getting tle??
from sys import stdin;
from collections import defaultdict
t=int(input())
for i in range(t):
s = stdin.readline()
dict=defaultdict(int)
x, y = map(int, input().split())
up, down, left, right = 0, 0, 0, 0
slen=len(s)
for i in range(slen):
dict[s[i]]+=1
right=dict[“R”]
up=dict[“U”]
down=dict[“D”]
left=dict[“L”]
q = int(input())
for i in range(q):
a, b = map(int, input().split())
if 0 <= a - x <= right and 0 <= b - y <= up:
print("YES", a - x + b - y)
elif 0 <= x - a <= left and 0 <= y - b <= down:
print("YES", x - a + y - b)
elif 0 <= a - x <= right and 0 <= y - b <= down:
print("YES", a - x + y - b)
elif 0 <= x - a <= left and 0 <= b - y <= up:
print("YES", x - a + b - y)
else:
print("NO")
really… most of the people… I mean at least 50% people got it wrong just because of that “\n”.