Submission

Status:

[PPPPPPPPPPPPPPP]

Subtask/Task Score:

{100/100}

Score: 100

User: winwin1054

Problemset: fireball

Language: cpp

Time: 0.005 second

Submitted On: 2026-10-14 18:18:28

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

int n,m,q,one=0;
int arr[100][100];
int ans[100][100];
int wow=0;

void re(int x,int y) {
	if(x>n||x<=0||y>m||y<=0||arr[x-1][y-1]==0||arr[x-1][y-1]==2) {
		return;
	}
	if(arr[x-1][y-1]==1) {
		wow++;
		arr[x-1][y-1]=2;
	}
	re(x-1,y);
	re(x+1,y);
	re(x,y+1);
	re(x,y-1);
}

void pans(int x,int y) {
	if(x>n||x<=0||y>m||y<=0||arr[x-1][y-1]==0) {
		return;
	}
	if(arr[x-1][y-1]==2) {
		ans[x-1][y-1]=wow;
		arr[x-1][y-1]=0;
    }
	pans(x-1,y);
	pans(x+1,y);
	pans(x,y+1);
	pans(x,y-1);
}

int main() {
    cin >> n >> m >> q;
    for(int i=0;i<n;i++) {
    	for(int j=0;j<m;j++) {
    		cin >> arr[i][j];
    		if(arr[i][j]) one+=1;
    		ans[i][j] = 0;
		}
	}
	int x,y;
	for(int i=0;i<q;i++) {
		cin >> x >> y;
		if(!ans[x-1][y-1]) {
			wow = 0;
			re(x,y);
			pans(x,y);
			one -= ans[x-1][y-1];
		}
		cout << one << '\n';
	}
}