Time limit exceeded FAST I/0 needed

i am getting time limit exceeded in CENS20G and i think my solution is correct

#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define FastIO ios_base::sync_with_stdio(false); cin.tie(NULL);
#define INF 1e9+7
#define fin cin
#define ub upper_bound
#define lb lower_bound
using namespace std;

int main() {
// your cod goes here
FastIO
int t=1;
cin>>t;
while(t–){
ll temp=0,flag=1,ans=0,maxo=1e9+7,mino=-1e9-7;

    string s; cin>>s;
    ll n= s.length();
    ll x1,y1; cin>>x1>>y1;
    
    ll a=0,b=0,c=0,d=0;
    for(ll i=0;i<n;i++){
        if(s[i]=='L') a++;
        if(s[i]=='R') b++;
        if(s[i]=='D') c++;
        if(s[i]=='U') d++;
    }
    ll q; cin>>q;
    
    while(q--){
            flag=1;
        ll x2,y2; cin>>x2>>y2;
        if(x2-x1+a<0 || x2-x1-b>0) flag=0;
        if(y2-y1+c<0 || y2-y1-d>0) flag=0;
        
        ll x=0;
        
        if(flag) cout <<"YES"<<" "<<abs(x2-x1)+abs(y2-y1)<<endl;
        else cout <<"NO"<<endl;
    }
}
return 0;

}

my question is if its necessary to have such high limits for length of string? aren’t you testing the logic?

Never use endl with fast i/o

5 Likes

https://www.geeksforgeeks.org/fast-io-for-competitive-programming/

use “\n” for fast I/O

at top define

#define endl '\n'

u will never have tle (if approach is correct) , bcz of endl.

@nikhilbalwani @doubleux

1 Like

i used an endl instead of a ‘\n’, and lost 2 hours :frowning:

2 Likes

After 3 TLEs, I went back to good old scanf() and printf(). :upside_down_face: