CHARGES - Editorial

Can someone provide me a test case over which my code fails

ok got my flaw :sweat_smile: self , explicitly handled n=1 (for each query) case and done!!!

1 Like

u needed to do updates first

#include<bits/stdc++.h>
#define ll long long
using namespace std;

int main()
{
int t;
cin >> t;
while(t–)
{
ll n,k;
cin >> n >> k;
string s;
cin >> s;
ll d=0;

       for(ll i=0;i<n-1;i++)
       {
           if(s[i]!= s[i+1])
            d++;
           else
            d+=2;
       }
       while(k--)
       {

           ll q;
           cin >> q;
           if(q == 1 && n!=1 )
           {
               if(s[0] != s[1])
                d-=1;
               else
                d-=2;

               if(s[0]=='1')
               {
                   s[0]='0';
               }
               else
                s[0]='1';
                ///////////
                if(s[0] != s[1])
                d+=1;
               else
                d+=2;

                cout << d <<"\n";

           }

           else if(q == n && n !=1 )
           {
               if(s[n-1] != s[n-2])
                d-=1;
               else
                d-=2;

               if(s[n-1]=='1')
               {
                   s[0]='0';
               }
               else
                s[n-1]='1';
                ///////////
                if(s[n-1] != s[n-2])
                d+=1;
               else
                d+=2;
               cout << d <<"\n";

           }
           else if(n==1)
           {
               cout << 1 <<"\n";
           }
           else
           {
               //right
                if(s[q-1] != s[q])
                d-=1;
                else
                d-=2;

                //left

               if(s[q-1] != s[q-2])
                d-=1;
               else
                d-=2;

               if(s[q-1]=='1')
               {
                   s[q-1]='0';
               }
               else
                s[q-1]='1';
                ///////////
                if(s[q-1] != s[q])
                d+=1;
               else
                d+=2;



                if(s[q-1] != s[q-2])
                d+=1;
               else
                d+=2;

                  cout << d <<"\n";

           }


       }






    }
return 0;

}

can some one check my solution and tell me whats wrong here

1
1 3
0
1 1 1

this case with string length is 1 should output
0

you code result:
0
0
0

you didn’t consider the edge case like string length is 1. this case should output 0.
1
1 3
0
1 1 1

your code is runtime error

1 Like

1
1 3
0
1 1 1
This edge case should output:
0

your code is
-2
-4
-6

1
1 3
0
1 1 1

This case should output
0

you code is
0
0
0

1
1 3
0
1 1 1

should output
0

you output
1
2
3

@henrychen222
Can you please tell me why i am getting tle ! Thank you.
https://www.codechef.com/viewsolution/47270812

My Approach -

  1. find out the initial sum
  2. for every query finding the sum of (i+1,i,i-1) before and after change
  3. updating the sum

@darshancool25
Please let me know what is wrong with my solution. I have applied same approach to the problem.
Please help.
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
int main(void) {
// your code goes here
int a;
scanf("%d", &a);
for(int i=0; i<a; i++){
int b, c;
scanf("%d %d", &b, &c);
char arr[b];
scanf("%s", arr);
long long int sum=0;
if(b == 1){
printf("%d\n", 0);
}
else{
for(int k=0; k<(b-1); k++){
if(arr[k] == arr[k+1]){
sum = sum + 2;
}
else{
sum = sum + 1;
}
}
for(int j=0; j<c; j++){
int e;
scanf("%d", &e);
if(arr[e-1] == 48){
arr[e-1] = 49;
}
else{
arr[e-1] = 48;
}
if(b > 1){
if(e == 1){
if(arr[e-1] == arr[e]){
sum = sum + 1;
}
else{
sum = sum - 1;
}
}
else if(e == b){
if(arr[e-1] == arr[e-2]){
sum = sum +1;
}
else{
sum = sum - 1;
}
}
else{
if(arr[e-1] == arr[e-2]){
sum = sum + 1;
}
else{
if(arr[e-1] != arr[e-2]){
sum = sum - 1;
}
}
if(arr[e-1] == arr[e]){
sum = sum +1;
}
else{
if(arr[e-1] != arr[e]){
sum = sum - 1;
}
}
}
printf("%lld\n", sum);
}

   }
   }
}
return 0;

}

Did any start to code Segment Tree immediately after looking at the problem statement, like I did?

And then realised it has a simpler solution :slightly_frowning_face:

https://www.codechef.com/viewsolution/47175300

#include <stdio.h>
int main(void)
{
int t,i;
scanf(“%d”, &t);
while(t–)
{
int n,k,ind;
scanf(“%d %d”, &n,&k);
char s[n];
int q[k];
scanf(“%s”, s);

   for(i=0;i<k;i++)
   scanf("%d", &q[i]);
   
   for(i=0;i<k;i++)
   {
       int sum=0;
       ind=q[i]-1;
       if(s[ind]=='1')
       s[ind]='0';
       else
       s[ind]='1';
       for(int j=0;j<k-1;j++)
       {
           if(s[j]==s[j+1])
           sum=sum+2;
           else
           sum=sum+1;
       }
       printf("%d\n", sum);
   }
}
return 0;

}

Answer is correct but I get (RE) SIGSEGV
Please help me out here.

while(test–)

{

    long long n,k;

    cin>>n>>k;

    if(n==1)

    {

        cout<<"0"<<endl;

        continue;

    }

    char particles[n]; char c;

    vector <long long> par; // integer storage of charges

    long long i;

    for(i=0;i<n;i++)

    {

        cin>>c;

        if(c=='1')

        par.push(1);

        if(c=='0')

        par.push(0);

    }

    long long a[k]; // all the k queries

    for(i=0;i<k;i++)

    cin>>a[i];

    long long sum=0;

    for(i=0;i<n-1;i++)

    {

        if(par[i]==par[i+1])

        sum=sum+2;

        else

        sum=sum+1;

                

    }

    for(i=0;i<k;i++) // resolving the positions to indices by subtracting 1

    {

        a[i]=a[i]-1;

    }

    

    // CODE STARTS ::::::

   

    for(i=0;i<k;i++)

    {

        int index=a[i];

        if(index==0) // first particle

        {

            par[index]=1-par[index];  // reversing the charge

            if(par[index]==par[index+1])

            {

                sum=sum+1;

            }

            else

            sum=sum-1;

        }

        else if(index==n-1) //last particle

        {

            par[index]=1-par[index]; // reversing the charge

            if(par[index]==par[index-1])

            {

                sum=sum+1;

            }

            else

            sum=sum-1;

        }

        else // Middle Particle

        {

            par[index]=1-par[index];   // reversing the charge

            if(par[index]==par[index-1])

            {

                sum=sum+1;

            }

            else

            sum=sum-1;

            if(par[index]==par[index+1])

            {

                sum=sum+1;

            }

            else

            sum=sum-1;

        }

        cout<<sum<<endl;

    }

}

I have read all the posts in this thread and I have handled everything I could think of even n=1. Still I am getting WA. Please someone help.
Link to solution–> CodeChef: Practical coding for everyone

Hi @darshancool25 I have written where in I updated total distance before and after upadating But I dont know why iam getting TLE. Can u check my code once. Thank you!!.
Link:CodeChef: Practical coding for everyone

I can tell you there are three reason may cause TLE
(1) cout<<TotalDis<<endl; // This is very slow for endl, use ‘\n’
(2) use fast ios at the begining of main()
ios::sync_with_stdio(false);
cin.tie(nullptr);
(3) you read the string and then loop again to parse into array, which is unnecessary. you can parse the string directly.


fixed your code CodeChef: Practical coding for everyone 0.59sec
your original code have some issues cause WA also, sum is long long, not int. will overflow.

Also, based on your code and my AC java code CodeChef: Practical coding for everyone
I get another 0.02sec AC c++ version: CodeChef: Practical coding for everyone

go through vectors concept again.

          #include<bits/stdc++.h>

#define MOD 1000000007
#define ll long long int
#define ld long double
#define vll vector
#define sll set
#define pb push_back
#define FAST ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define TEST ll t; cin>>t; while(t–)
#define rep(i,a,b) for(ll i=a;i<b;++i)
using namespace std;

void solve()
{

//your code goes here
ll n , k;
cin >> n >> k;
string s;
cin >> s;
/*
vll q(n);
rep(i , 0 , k)
cin >> q[i];
*/

ll len = 0;
for(ll i = 0 ; i < n-1 ; i++)
{
if(s[i] == s[i+1])
len += 2;
else
len += 1;
}

rep(i , 0 , k)
{
ll q;
cin >> q; q–;

if(s[q]=='1') s[q] = '0';
else s[q] = '1';

if( q == 0)
{
	if( s[q] == s[q+1] ) len++;
	else len--;
}
else if( q == n-1 )
{
	if( s[q] == s[q-1] ) len++;
	else len--;
}
else
{
	if( s[q] == s[q+1] ) len++;
	else len--;
	
	if( s[q] == s[q-1] ) len++;
	else len--;
	
}
/*
if(q > 0){
	if( s[q] == s[q-1] ) len++;
	else
	len--;
}
*/

cout << len << "\n";

}

}

// Driver code
int main() {

FAST
TEST
{
	solve();
}

return 0;

}

can some help me out what’s wrong with this code please?

Thanks