CHEFBM - Editorial





Author: Dmytro Berezin
Tester: Sergey Kulik
Editorialist: Lalit Kundu






There is a N * M matrix, where A*[j] is j for all i,j. There are P queries of form (i,j), where we do A*[j]++.
For each row i,
if there exists a j, such that A*[j] > A*[j+1], print -1.
else print sum[i=M to 2]{A*[j]-A*[j-1]}.
1 ≤ N,M,P ≤ 105


For each i, we also keep note of values (i,j-1) and (i,j+1), if (i,j) is given in input. Traversing over the queries, we can see which values change and print answer accordingly.


We have to process for each i independently. Let’s say for an i, we have NN queries of form (j,k) which means: increase A*[j] by k. For such a query we also store (j-1,0) and (j+1,0), because these two indexes are also effected.
If there were no changes, our answer will be m-1. Now, we traverse over all queries in increasing order of j and look at index j+1, if the value at j+1 is less than value at j, we know that answer is -1 for this row. Else, we add the (A*[j+1]-A*[j]) to the answer and subtract -1 from the answer. We can use map to perform this process efficiently. See (setter/tester/editorialist)'s solution for implementation details.


Author’s solution
Setter’s solution
Editorialist’s solution


" For such a query we also store (j-1,0) and (j+1,0), because these two indexes are also effected."

Can anyone expand on this line? Where do we store that?


for which test case does this code fails what is wrong in it


can you help me as well


I tried many times but always got WA . Both the above use case is correct for me .
If any one can give me test case that caused WA , it will be a great help.


Can someone please check if this is giving right answers on your test cases? It can calculate results even if input of a row changes after changes in other rows, for example

4 4 6

3 2

2 3

3 2

2 4

1 2



Can the approach be this:
storing count of the incremented index(the coloumn), adding to it the index values. Sort this, if its same as the initial string, ans will be m-1 otherwise -1. For eg.

4 4 6
2 2
3 2
3 2
4 3
4 4
4 3

then for the third row, count(this array will be 1-indexed) will be 0 2 0 0, adding the index values to it, it will be 1 4 3 4. Now if i’ll sort this, i’ll not get the same array and hence answer should be -1. I implemented this here: and its most obvious to give TLE. BUt is there any way I can optimize this??


Why does this ans give TLE:
It works fine on my computer.Please help.


Where shall i get the input file and output file?


Why does this ans give WA: It works fine on my computer.Please help.

its working fine for every test cases…but getting wrong answer…can any one please help


why am i getting wrong answer


WA is coming plz provide the test cases where my code fails


Can anyone tell me the complexity of solution in editorial.
I think it should be


O(n) for each row
then O(p*) queries of a row then calcuating cost O(n)

am i ryt?


I did this in pretty naive way…
stored all the queries in vector pairs…

sorted the vector pairs with first element increasing and corresponding second element decreasing…

for e.g if pairs are (1,2) (2,3) (2,1) (2,4)… the results of sorting would be (1,2) (2,4) (2,3) (2,1)…


for i from 1 to n:

        count all the occurrences of (since vectors are sorted, just increment j and keep checking
        count all occurrences of next number and if the next number is 1 less than previous number that this count shouldn't be greater than count(previous number)+1
        if its not 1 less than previous number than count shouldn't be more than 1
        count all the occurrences of (i,m).. let this be k
        count all the occurrences of (i,1).. lets this be f
        if all the conditions above are satisfied answer is (m+k)-(f+1)
        else its -1
        ans is m-1

here is my solution…

This seems quite fast still TLE.

is working fine for every test cases, but I’m getting wrong answer. Can any one please tell me which test case is failing?
I used the following approach:

  1. Store queries in struct A {int i,j;}

  2. Sort A in increasing order of rows and corresponding decreasing order of columns

    for i=1 to n {
    if A[count].i!=i
    print m-1
    if k!=m
    ans=m-k-1, next=k+1;
    ans=0, next=-1;
    for j=k to 1 {
    while(A[count].j==j && A[count].i==i)
    print ans;



I really hate to do this,but i can’t figure out the error in this:
if anyone can provide any corner test cases.Thanks.

my code is working fine on my computer.but here i am getting wrong answer

#20 is my solution to the problem, I have tried all the test cases provided in the above answers and also the sample test case and it is working fine, I’m not able to figure out why my code is getting a WA.

Please help me to figure it out.