Charges solution codechef

Charges solution codechef

There are NN subatomic particles lined up in a row. There are two types: protons and electrons. Protons have a positive charge and are represented by 11, while electrons have a negative charge and are represented by 00.

Our current understanding of physics gives us a way to predict how the particles will be spaced out, if we know their charges. Two adjacent particles will be separated by 11 unit if they have opposite charges, and 22 units if they have the same charge.

When Chef is not in the kitchen, he is doing physics experiments on subatomic particles. He is testing the hypothesis by having NN particles in a row, and he will change the charge of a particle KK times. In the ii-th update, he will change the charge of the QiQi-th particle. After each update, find the distance between the first and last particle.

Note: Each update is persistent for further updates.

Input Charges solution codechef

• The first line contains an integer TT, the number of test cases. Then the test cases follow.
• Each test case contains three lines of input.
• The first line contains two integers NNKK.
• The second line contains a string SS of length NN, where SiSi represents the initial charge on ii-th particle.
• The third line contains KK integers Q1,Q2,,QKQ1,Q2,…,QK, the positions of the changed particles.

Output Charges solution codechef

For each test case, output KK lines, where the ii-th line contains the answer after the updates Q1,,QiQ1,…,Qi have been made.

Constraints Charges solution codechef

• 1T51≤T≤5
• 1N,K1051≤N,K≤105
• SS contains only 00 and 11 characters.
• 1QiN1≤Qi≤N
• The sum of KK over all testcases is at most 21052⋅105

Subtask #1 (100 points): original constraints

Sample Input Charges solution codechef

1
3 3
010
2 1 3


Sample Output Charges solution codechef

4
3
2


Explanation Charges solution codechef

Update 1: After reversing the parity of particle 22, the new configuration is 000000. Since all the particles have a similar charge, each is separated from the previous by a distance of 22 units. So the location of particle 33 is 2+2=42+2=4 units from the first particle.

Update 2: After reversing the parity of particle 11, the new configuration is 100100. Here, the charges of particles 11 and 22 differ, so they are separated by 11 unit. The charges of particles 22 and 33 agree, so they are separated by 22 units. So, the location of particle 33 is 1+2=31+2=3 units from the first particle.

Update 3: After reversing the charge of particle 33, the new configuration is 101101. Here, particles 11 and 22 are separated by 11 unit and particles 22 and 33 are separated by 11 unit. So the location of particle 33 is 1+1=21+1=2 units from the first particle.

SOLUTION

What’s in it for you?

The idea behind these programming contests is that we want you to learn while competing. Also we believe that it is alright to refer to tutorials, books and other materials, learn a concept and then apply the same to solve a problem during a contest. But it is not alright to copy other people’s solutions or seek other people’s help to solve a problem without understanding it.

The dividing line may seem to be thin but it can be captured by the spirit of learning. If whatever you are doing is making you learn while you do so, we tend to believe that it is alright. Our sole intention lies in making our users learn new concepts while competing. However, all the participants are expected to abide to

What’s new in CodeChef?

You may now play with our new groups & LaTex feature on discuss.

• Please do not discuss strategy, suggestions or tips in the comments during a live contest. Posting questions clarifying the problem statement is ok. If you are unsure, email us at [email protected] .
• Discussing CodeChef’s problems or any aspect of problem, on any other platform on web, on identification, could lead to disabling of respective account and banning from the community.
• You can also send in your queries in an email to [email protected], during the contest.
• You will receive one point for solving a problem (passing all test cases – no partial credit), regardless of the level of difficulty of that problem.
• Users are ranked according to the most problems solved. Ties will be broken by the total time for each user in ascending order of time.
• The total time is the sum of the time consumed for each problem solved. The time consumed for a solved problem is the time elapsed from the beginning of the contest to the submittal of the first accepted run plus 10 penalty minutes for every previously rejected run for that problem. There is no time consumed for a problem that is not solved.
• You shall not possess more than one account on CodeChef. If you have, then do let us know, so that we can deactivate the insignificant one. If you do not report it and we come to know about it, we may deactivate both the accounts permanently.
• If anyone is using code from some other source in his submission, he should provide proper attribution. Failing this, it may be considered plagiarism and the submission will be subject to disqualification.
• The number of submissions that one can make during the contest on a single problem will be limited to 500.
• Residents of the following countries and territories are not eligible to win cash prizes/laddus/goodies due to legal restrictions: Albania, The Bahamas, Barbados, Botswana, Cambodia, Crimea region of Russia, Cuba, Ghana, Iceland, Iran, Jamaica, Mauritius, Mongolia, Myanmar, Nicaragua, North Korea, Pakistan, Panama, Sudan, Syria, Uganda, Yemen, Zimbabwe.

Note: You can now “Code, Compile, and Run” your codes on our Online IDE.

However, if you are using any other online development environment, make sure that other contestants don’t have access to your code. As a contestant, you are responsible for making sure others don’t access the code that you submit. If you use Ideone, make sure to mark your submission “private” (not secret)”.