Submission

Status:

[PPPPPPPPPPPPPPP]

Subtask/Task Score:

{100/100}

Score: 100

User: Kx

Problemset: fireball

Language: cpp

Time: 0.003 second

Submitted On: 2026-03-19 15:13:59

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

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};

int n, m, q, res = 0;
vector<vector<int>> mp;

bool pmap(int x, int y) {
    return x >= 1 && x <= n && y >= 1 && y <= m;
}

void laam(int x, int y) {
    if(mp[x][y] == 0) return;

    for(int i = 0; i < 4; ++i) {
        int xx = x + dx[i], yy = y + dy[i];

        mp[x][y] = 0;
        if(pmap(xx, yy) && mp[xx][yy] == 1) {
            laam(xx, yy);
            res--;
        }
    }
}

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    cin >> n >> m >> q;

    mp.assign(n + 2, vector<int>(m + 2));
    for(int i = 1; i <= n; ++i) {
        for(int j = 1; j <= m; ++j) {
            cin >> mp[i][j];
            if(mp[i][j] == 1) res++;
        }
    }

    while(q--) {
        int x, y; cin >> x >> y;

        if(mp[x][y] == 1) {
            laam(x, y);
            res--;
        }

        cout << res << '\n';
    }

    return 0;
}