Submission
Status:
[PPPPPPPPPPPPPPP]
Subtask/Task Score:
{100/100}
Score: 100
User: krittaphot
Problemset: fireball
Language: cpp
Time: 0.004 second
Submitted On: 2026-03-06 19:16:18
#include <bits/stdc++.h>
using namespace std;
int main(){
cin.tie(nullptr)->sync_with_stdio(false);
int n,m,q;
cin >> n >> m >> q;
int cc = 0;
vector<vector<int>> mp(n+1,vector<int>(m+1));
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin >> mp[i][j];
if(mp[i][j]==1) cc++;
}
}
for(int k=0;k<q;k++){
int a,b;
cin >> a >> b;
if (mp[a][b] == 0) {
cout << cc << "\n";
continue;
}
queue<pair<int,int>> qu;
qu.push({a,b});
mp[a][b] = 0;
cc--;
while(!qu.empty()){
int i = qu.front().first;
int j = qu.front().second;
qu.pop();
if(j+1<=m && mp[i][j+1] == 1 ){
qu.push({i,j+1});
cc--;
mp[i][j+1] = 0;
}
if(i+1<=n && mp[i+1][j] == 1){
qu.push({i+1,j});
cc--;
mp[i+1][j] = 0;
}
if(j-1>0 && mp[i][j-1] == 1 ){
qu.push({i,j-1});
cc--;
mp[i][j-1] = 0;
}
if(i-1>0 && mp[i-1][j] == 1 ){
qu.push({i-1,j});
cc--;
mp[i-1][j] = 0;
}
}
cout << cc << "\n";
}
return 0;
}