I looked through a lot of posts and tried every testcase suggested. Also handled the n==1 corner case and printed 0 k times. But still WA.
Please help me out. stuck on this problem since yesterday.
Submission link—>CodeChef: Practical coding for everyone
#include <bits/stdc++.h>
using namespace std;
void fastscan(long long &number)
{
bool negative = false;
register long long c;
number = 0;
c = getchar();
if (c==‘-’)
{
negative = true;
c = getchar();
}
for (; (c>47 && c<58); c=getchar())
number = number *10 + c - 48;
if (negative)
number *= -1;
}
#define push push_back
//#define cin(n) fastscan(n)
#define all(x) x.begin(),x.end()
#define watch(x) cout<<"x= "<<x<<endl;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long test;
cin>>test;
while(test–)
{
long long n,k;
cin>>n>>k;
if(n==1)
{
for(int i=0;i<k;i++)
cout<<“0”<<endl;
continue;
}
char particles[n]; char c;
vector 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;
}
}
return 0;
}