CENS20G - Editorial

https://www.codechef.com/viewsolution/37002789
TLE??? same O(|S| +Q)

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::+1:

Try replacing endl by “\n”. This worked for me

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].

1 Like

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”.

F in the chats for this question please…

That not kind of fair!! At least they should have informed the participants about it before the contest!!

Consider the test input:

1
LUDLUDLUDLUD
1 2
2
2 3
1 -2
2 Likes

I literally wasted close to 1hr trying to figure out why the hell am i getting TLE even if i have used “\n” in place of “endl” and fastio methods…Then i submitted the same sol in c++17 instead of c++14 and it gets AC -_-.

I did same mistake man…

yeah that worked

Me neither. Literally there is no point of more than 1e7 chars input. I even submitted soln with this note. CodeChef: Practical coding for everyone

2 Likes

always use fast i/o methods. here is the code for fast i/o
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
this would take all input together and also print all the outputs together and save you from getting tle.

I figured out your mistake in the code check it out at the given link: edited code. Always try using “\n” for faster output using endl will take more time for big output.