#include <iostream> #include <queue> using namespace std; const int MAXN = 1e3 * 2 + 20; struct n1{ int x,y; n1(int a, int b){ x = a; y = b; } }; struct n2{ int x,y; }; queue <n1> kol; int t[MAXN][MAXN]; n2 p[MAXN][MAXN], tab[MAXN][MAXN]; bool vis[MAXN][MAXN]; int n,m,z,w,active; void funk(int x, int y){ while(!kol.empty()) kol.pop(); int sz1 = 0, sz2 = 0; kol.push(n1(x,y)); p[x][y] = {0,0}; vis[x][y] = true; while(!kol.empty()){ int i = kol.front().x; int j = kol.front().y; if(t[i][j] == active){ for(int k = 1; k <= n; k++){ if(vis[i / n][k] == 0 && t[i / n][k] != active) { p[i/n][k].x = i; p[i/n][k].y = j; vis[i/n][k] = true; kol.push(n1(i/n,k)); } if(vis[k][i % n] == 0 && t[i % n][k] != active){ p[i][k%n].x = i; p[i][k%n].y = j; vis[i][k%n] = true; kol.push(n1(i,k%n)); } } }else { if(!tab[i][j].x && !tab[i][j].y){ sz1 = i; sz2 = j; break; } else if (!vis[tab[i][j].x][tab[i][j].y]) { vis[tab[i][j].x][tab[i][j].y] = true; p[tab[i][j].x][tab[i][j].y] = {i,j}; kol.push(n1(tab[i][j].x, tab[i][j].y)); } } kol.pop(); } if(!sz1 && !sz2) return; while(true){ if(!p[sz1][sz2].x && !p[sz1][sz2].y) break; if(t[sz1][sz2] != active){ tab[sz1][sz2] = p[sz1][sz2]; tab[p[sz1][sz2].x][p[sz1][sz2].y] = {sz1,sz2}; } int help = sz1; sz1 = p[sz1][sz2].x; sz2 = p[help][sz2].y; } w++; } int main() { ios_base :: sync_with_stdio(0); cin >> n >> m >> z; for(int i = 1; i <= n; i++){ for(int j = 1; j <= n; j++){ tab[i][j] = {-1,-1}; p[i][j] = {-1,-1}; } } for(int d = 0 ; d < m; d++){ int x1,y1,x2,y2; cin >> x1 >> y1 >> x2 >> y2; for(int i = x1 ; i <= x2; i++){ for(int j = y1 ; j <= y2; j++){ t[i][j] = 1 - t[i][j]; } } } for(int i = 0 ; i <= z; i++){ int x1,y1; if(!i){ active = 1; for(int j = 1; j <= n; j++){ for(int k = 1; k <= n; k++){ if(t[j][k]){ funk(j,k); } } } } else{ for(int j = 1; j <= n; j++){ for(int k = 1; k <= n; k++){ vis[j][k] = 0; } } cin >> x1 >> y1; t[x1][y1] = 1 - t[x1][y1]; tab[x1][y1].x = 0; tab[x1][y1].y = 0; tab[tab[x1][y1].x][tab[x1][y1].y] = {0,0}; active = t[x1][y1]; if(!tab[x1][y1].x && !tab[x1][y1].y){ w--; active = t[tab[x1][y1].x][tab[x1][y1].y]; funk(0,0); } funk(x1,y1); } cout << w << '\n'; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | #include <iostream> #include <queue> using namespace std; const int MAXN = 1e3 * 2 + 20; struct n1{ int x,y; n1(int a, int b){ x = a; y = b; } }; struct n2{ int x,y; }; queue <n1> kol; int t[MAXN][MAXN]; n2 p[MAXN][MAXN], tab[MAXN][MAXN]; bool vis[MAXN][MAXN]; int n,m,z,w,active; void funk(int x, int y){ while(!kol.empty()) kol.pop(); int sz1 = 0, sz2 = 0; kol.push(n1(x,y)); p[x][y] = {0,0}; vis[x][y] = true; while(!kol.empty()){ int i = kol.front().x; int j = kol.front().y; if(t[i][j] == active){ for(int k = 1; k <= n; k++){ if(vis[i / n][k] == 0 && t[i / n][k] != active) { p[i/n][k].x = i; p[i/n][k].y = j; vis[i/n][k] = true; kol.push(n1(i/n,k)); } if(vis[k][i % n] == 0 && t[i % n][k] != active){ p[i][k%n].x = i; p[i][k%n].y = j; vis[i][k%n] = true; kol.push(n1(i,k%n)); } } }else { if(!tab[i][j].x && !tab[i][j].y){ sz1 = i; sz2 = j; break; } else if (!vis[tab[i][j].x][tab[i][j].y]) { vis[tab[i][j].x][tab[i][j].y] = true; p[tab[i][j].x][tab[i][j].y] = {i,j}; kol.push(n1(tab[i][j].x, tab[i][j].y)); } } kol.pop(); } if(!sz1 && !sz2) return; while(true){ if(!p[sz1][sz2].x && !p[sz1][sz2].y) break; if(t[sz1][sz2] != active){ tab[sz1][sz2] = p[sz1][sz2]; tab[p[sz1][sz2].x][p[sz1][sz2].y] = {sz1,sz2}; } int help = sz1; sz1 = p[sz1][sz2].x; sz2 = p[help][sz2].y; } w++; } int main() { ios_base :: sync_with_stdio(0); cin >> n >> m >> z; for(int i = 1; i <= n; i++){ for(int j = 1; j <= n; j++){ tab[i][j] = {-1,-1}; p[i][j] = {-1,-1}; } } for(int d = 0 ; d < m; d++){ int x1,y1,x2,y2; cin >> x1 >> y1 >> x2 >> y2; for(int i = x1 ; i <= x2; i++){ for(int j = y1 ; j <= y2; j++){ t[i][j] = 1 - t[i][j]; } } } for(int i = 0 ; i <= z; i++){ int x1,y1; if(!i){ active = 1; for(int j = 1; j <= n; j++){ for(int k = 1; k <= n; k++){ if(t[j][k]){ funk(j,k); } } } } else{ for(int j = 1; j <= n; j++){ for(int k = 1; k <= n; k++){ vis[j][k] = 0; } } cin >> x1 >> y1; t[x1][y1] = 1 - t[x1][y1]; tab[x1][y1].x = 0; tab[x1][y1].y = 0; tab[tab[x1][y1].x][tab[x1][y1].y] = {0,0}; active = t[x1][y1]; if(!tab[x1][y1].x && !tab[x1][y1].y){ w--; active = t[tab[x1][y1].x][tab[x1][y1].y]; funk(0,0); } funk(x1,y1); } cout << w << '\n'; } } |