Submission
Status:
[PPPPPPPPPPPPPPP]
Subtask/Task Score:
{100/100}
Score: 100
User: samsenpisardmoo
Problemset: fireball
Language: cpp
Time: 0.002 second
Submitted On: 2026-02-15 10:22:17
#include <bits/stdc++.h>
using namespace std;
int main()
{
cin.tie(NULL) -> sync_with_stdio(false);
int n,m,q, cnt=0;
cin >> n >> m >> q;
int mp[n][m], vs[n][m];
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
cin >> mp[i][j];
vs[i][j]=0;
if (mp[i][j] == 1) cnt++;
}
}
deque <int> l;
while (q--)
{
int f, k;
cin>>f>>k;
f--, k--;
if (mp[f][k]==0 || vs[f][k])
{
cout << cnt << "\n";
continue ;
}
queue<pair<int, int>> note;
note.push({f, k});
vs[f][k] = 1;
while (!note.empty())
{
int r=note.front().first;
int c=note.front().second;
note.pop();
// exploring (r, c)
cnt--;
if(r+1 < n && mp[r+1][c]==1 && !vs[r+1][c]) note.push({r+1,c}), vs[r+1][c]=1 ;
if(c+1 < m && mp[r][c+1]==1 && !vs[r][c+1]) note.push({r,c+1}), vs[r][c+1]=1 ;
if(r-1 >= 0 && mp[r-1][c]==1 && !vs[r-1][c]) note.push({r-1,c}), vs[r-1][c]=1 ;
if(c-1 >= 0 && mp[r][c-1]==1 && !vs[r][c-1]) note.push({r,c-1}), vs[r][c-1]=1 ;
}
cout << cnt << "\n";
}
}