I invested wasted more than 2hrs on this
Yeah same I tried it for over an hour in the contest. I felt the difficulty gap between the first and second(this) question was quite high.
I spent around 2 hours for this problems and got so many WA’s still couldn’t find solution.
But I am learning and will definitely solve all problems someday
can someone help me understand why I am getting TLE, even subtask 1 feels like it is taking too long to run
bro me too struggle hard for C I was getting Runtime error in 2 test case in python
because of one statement sys.setrecursionlimit(106)****
i forgot to include it in my code
I did exactly what ma’am said, but still got TLE. I have coded in C++, could anyone please help me find what’s wrong with my solution? Thanks
Is it working?
you messed up sub seq with sub array
I also did same mistake 
/**************************************************************************
- Id
- File:
- Purpose:
- Author: Sanchit Gupta (CS19B071)
- Created:
- Last modified:
- Bugs:
- Change Log:
- While(!(succeed==try());
**************************************************************************/
#include
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t–)
{
long int n;
long int q;
long int i=0;
cin>>n>>q;
long int arr[n];
map<long int,long int> mp;
long int count=0;
/while(i!=n)
{
cin>>arr[i];
i++;
}/
i=0;
cin>>arr[i];
long int prev=0,next;
i=1;
while(i!=n)
{
cin>>arr[i];
if(arr[i]==arr[i-1])
{
count++;
}
else
{
mp[prev]=i;
prev=i;
count=0;
}
i++;
}
mp[prev]=n;
/auto it3=mp.begin();
while(it3!=mp.end())
{
cout<first<<" "<second<<endl;
it3++;
}/
i=0;
long int x,y;
while(i!=q)
{
cin>>x>>y;
x–;
auto it = mp.lower_bound(x);
if(y==arr[x])
{
cout<<mp.size()<<endl;
i++;
continue;
}
else if(x>0 && x<n-1 && y==arr[x-1] && y==arr[x+1])
{
arr[x]=y;
if(it->first==x)
{
auto it1=it;
auto it2=it;
it–;
it1++;
long int a=it->first,b=it1->second;
mp.erase(it2);
mp.erase(it1);
mp[a]=b;
}
}
else if(x<n-1 && y==arr[x+1])
{
arr[x]=y;
if(it->first==x)
{
auto it1=it;
it1++;
long int a=it->first,b=it1->second;
mp.erase(it);
mp.erase(it1);
mp[a]=b;
}
else
{
auto it1=it,it2=it;
it2--;
long int a=it2->first,b=it->second;
mp.erase(it);
mp.erase(it2);
mp[a]=x;
mp[x]=b;
}
}
else if(x>0 && y==arr[x-1])
{
//cout<<"yeah";
arr[x]=y;
auto it1=it;
it1--;
long int a=it1->first,b=it->second;
mp.erase(it);
mp.erase(it1);
mp[a]=x+1;
if(x+1!=b)
{
mp[x+1]=b;
}
}
else
{
arr[x]=y;
//cout<<"yes";
if(it==mp.end())
{
//cout<<"yes";
auto it1=it;
it1--;
long int a=it1->first,b=it1->second;
mp.erase(it1);
mp[a]=x;
mp[x]=x+1;
if(b!=x+1)
{
mp[x+1]=b;
}
}
else if(it->first==x)
{
if(it->second==x+1)
{
}
else
{
//cout<<"yeah";
long int s = it->second;
mp.erase(it);
mp[x]=x+1;
mp[x+1]=s;
}
}
else
{
long int a=it->first,b=it->second;
mp.erase(it);
mp[a]=x;
mp[x]=x+1;
mp[x+1]=b;
}
}
/*auto it3=mp.begin();
while(it3!=mp.end())
{
cout<<it3->first<<" "<<it3->second<<endl;
it3++;
}*/
cout<<mp.size()<<endl;
i++;
}
}
}
What is the problem with this code ?
Why does it give TLE
This question can also be done using Segment Trees. But The time take will be O(QLog(N)+NLog(N))
Yes
I think I overkilled this question by using Segment Trees
. Although this approach is better than the Segment Tree one…
That’s not the link to your code. It’s just a link to the Codechef IDE. Paste a link of your submitted solution or something like that.
I was getting TLE with this code in 2nd subtask of Div2 C problem please tell what is wrong.
Why I am getting TLE for second task even if my logic is same.
code link: Solution
#include <iostream>
using namespace std;
int main() {
int t;
cin>>t;
while(t-- >0)
{
int n,q;
cin>>n;
cin>>q;
int a[n];
int x[q];
int y[q];
for(int i =0;i <n;i++)
{
cin>>a[i];
}
for(int i =0;i<q;i++)
{
cin>>x[i];
cin>>y[i];
}
int max=0;
int prev=-1;
for(int j=0;j<n;j++)
{
if(a[j]!=prev)
{
prev=a[j];
max++;
}
}
for(int i=0;i<q;i++)
{
if(a[x[i]-1]==y[i])
{
cout<<max<<endl;
continue;
}
else
{
if(x[i]-1!=0)
{
if(a[x[i]-1]==a[x[i]-2])
max++;
if(y[i]==a[x[i]-2])
max--;
}
if(x[i]-1!=n-1)
{
if(a[x[i]-1]==a[x[i]])
max++;
if(y[i]==a[x[i]])
max--;
}
a[x[i]-1]=y[i];
cout<<max<<endl;
}
}
}
return 0;
}
Even Tried with java fastIO
class chefweds {
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) {
FastReader sc=new FastReader();
int t=sc.nextInt();
while(t-- >0)
{
int n=sc.nextInt();
int q=sc.nextInt();
int a[]=new int[n];
int x[]=new int[q];
int y[]=new int[q];
for(int i =0;i <n;i++)
{
int cv;
cv=sc.nextInt();
a[i]=cv;
}
for(int i =0;i<q;i++)
{
x[i]=sc.nextInt();
y[i]=sc.nextInt();
}
deploytemp obj=new deploytemp();
obj.solve(n,q,a,x,y);
}
}
}
class deploytemp
{
public void solve(int n,int q,int a[],int x[],int y[])
{
int max=0;
int prev=-1;
for(int j=0;j<n;j++)
{
if(a[j]!=prev)
{
prev=a[j];
max++;
}
}
for(int i=0;i<q;i++)
{
if(a[x[i]-1]==y[i])
{
System.out.println(max);
continue;
}
if(x[i]-1!=0)
{
if(a[x[i]-1]==a[x[i]-2])
max++;
if(y[i]==a[x[i]-2])
max--;
}
if(x[i]-1!=n-1)
{
if(a[x[i]-1]==a[x[i]])
max++;
if(y[i]==a[x[i]])
max--;
}
a[x[i]-1]=y[i];
System.out.println(max);
}
}
}
Try Fast Input/Output
I have heard that endl takes a lot of time as it flushes the output. Try using “\n” instead.
can anyone help me in my code i am not understanding why my list in second loop is not updating when i try to change the value in list it is not updating …
link for code - CodeChef: Practical coding for everyone