In this HackerEarth Moving people problem solution The people of your city are living in a N x M grid. If a person is out of the grid after multiple instructions, then that person is not counted in further steps. Initially, there is at most one person in every cell of this grid. You can perform Q operations or queries of the following types:

  1. 1 X Y: Move every person that is currently residing in the grid, X column left of their cells and Y column above of their cells. If X is negetive, then move people abs(X) column right of their cells. Also, if Y is negative, then move people abs(Y) column down of their cells.
  2. 2: Calculate the numbner of people that are currently living in the grid.


HackerEarth Moving people problem solution


HackerEarth Moving people problem solution.

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

const int maxn = 1005;
int n, m, x, y, z, k;

string str[maxn];
int csum[maxn][maxn];

int main()
{

    cin >> n >> m >> k;
    for(int i = 1; i <= n; i++) cin >> str[i];

    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            csum[i][j] = csum[i - 1][j] + csum[i][j - 1] - csum[i - 1][j - 1];
            if(str[i][j - 1] == '1') csum[i][j]++;
        }
    }

    int lft = 1, rgh = m, up = 1, dwn = n;
    bool flg = false;

    int mxlft = 1, mnrgh = m;
    int mxup = 1, mndwn = n;

    while(k--){
        int tp;
        scanf("%d", &tp);
        if(tp == 1){
            int x, y;
            scanf("%d %d", &x, &y);

            lft += x;
            rgh += x;

            up += y;
            dwn += y;

            mxlft = max(mxlft, lft);
            mnrgh = min(mnrgh, rgh);

            mxup = max(mxup, up);
            mndwn = min(mndwn, dwn);

        }
        else{

            if(mxlft > mnrgh || mxup > mndwn) printf("0\n");
            else{
                int anss = csum[mndwn][mnrgh] - csum[mndwn][mxlft - 1] - csum[mxup - 1][mnrgh] + csum[mxup - 1][mxlft - 1];
                printf("%d\n", anss);
            }

        }
    }


    return 0;
}