#include <iostream> #include <algorithm> #include <vector> #include <functional> using namespace std; struct Event{ int y, x1, x2; bool operator<(const Event& ev)const{ return y<ev.y; } }; struct Vertex{ bool val; int M; vector<int>Neis; }; void removeFromVector(vector<int>&V, int val){ for(int i=0;;i++) if(V[i]==val){ V[i]=V.back(); V.pop_back(); break; } } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int n,m,q; cin >> n >> m >> q; int nn=n*n; vector<Vertex>G(nn); //zbuduj mapę startową vector<Event>Evs; while(m--){ int x1,y1,x2,y2; cin >> y1 >> x1 >> y2 >> x2; Evs.push_back(Event{y1,x1,x2}); Evs.push_back(Event{y2+1,x1,x2}); } sort(Evs.begin(), Evs.end()); vector<bool>InvertBefore(n+2, false); for(int y=1,it=0,git=0;y<=n;y++){ while(it<(int)Evs.size() && Evs[it].y<=y){ InvertBefore[Evs[it].x1] = !InvertBefore[Evs[it].x1]; InvertBefore[Evs[it].x2+1] = !InvertBefore[Evs[it].x2+1]; it++; } bool currentBit=false; for(int x=1;x<=n;x++){ if(InvertBefore[x]) currentBit = !currentBit; G[git].val = currentBit; G[git].M = -1; git++; } } //zbuduj graf dwudzielny for(int y=0,it=0;y<n;y++) for(int x=0;x<n;x++,it++){ for(int x1=0;x1<n;x1++) if(G[it].val != G[it+x1-x].val) G[it].Neis.push_back(it+x1-x); for(int y1=0;y1<n;y1++) if(G[it].val != G[it+(y1-y)*n].val) G[it].Neis.push_back(it+(y1-y)*n); } //turbo-matching vector<bool>Visited(nn); int matchingSize=0; function<bool(int)> extend = [&](int v)->bool{ Visited[v] = true; //try to find close match for(int nei : G[v].Neis) if(G[nei].M==-1){ G[v].M = nei; G[nei].M = v; return true; } //try to find far match for(int nei : G[v].Neis) if(!Visited[G[nei].M] && extend(G[nei].M)){ G[v].M = nei; G[nei].M = v; return true; } return false; }; auto update_matching = [&](){ while(true){ bool extended=false; fill(Visited.begin(), Visited.end(), false); for(int i=0;i<nn;i++) if(G[i].val && G[i].M==-1 && extend(i)){ matchingSize++; extended=true; } if(!extended) break; } }; update_matching(); cout << matchingSize << "\n"; //przetwarzaj punktowe zmiany while(q--){ int x,y; cin >> y >> x; x--; y--; int it=y*n+x; //odłącz od sąsiadów if(G[it].M!=-1){ G[G[it].M].M=-1; G[it].M=-1; matchingSize--; } for(int nei : G[it].Neis) removeFromVector(G[nei].Neis, it); G[it].Neis.clear(); //zmień stan G[it].val = !G[it].val; //podłącz do nowych sąsiadów for(int x1=0;x1<n;x1++) if(G[it].val != G[it+x1-x].val){ G[it].Neis.push_back(it+x1-x); G[it+x1-x].Neis.push_back(it); } for(int y1=0;y1<n;y1++) if(G[it].val != G[it+(y1-y)*n].val){ G[it].Neis.push_back(it+(y1-y)*n); G[it+(y1-y)*n].Neis.push_back(it); } //przelicz matching update_matching(); cout << matchingSize << "\n"; } return 0; }
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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | #include <iostream> #include <algorithm> #include <vector> #include <functional> using namespace std; struct Event{ int y, x1, x2; bool operator<(const Event& ev)const{ return y<ev.y; } }; struct Vertex{ bool val; int M; vector<int>Neis; }; void removeFromVector(vector<int>&V, int val){ for(int i=0;;i++) if(V[i]==val){ V[i]=V.back(); V.pop_back(); break; } } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int n,m,q; cin >> n >> m >> q; int nn=n*n; vector<Vertex>G(nn); //zbuduj mapę startową vector<Event>Evs; while(m--){ int x1,y1,x2,y2; cin >> y1 >> x1 >> y2 >> x2; Evs.push_back(Event{y1,x1,x2}); Evs.push_back(Event{y2+1,x1,x2}); } sort(Evs.begin(), Evs.end()); vector<bool>InvertBefore(n+2, false); for(int y=1,it=0,git=0;y<=n;y++){ while(it<(int)Evs.size() && Evs[it].y<=y){ InvertBefore[Evs[it].x1] = !InvertBefore[Evs[it].x1]; InvertBefore[Evs[it].x2+1] = !InvertBefore[Evs[it].x2+1]; it++; } bool currentBit=false; for(int x=1;x<=n;x++){ if(InvertBefore[x]) currentBit = !currentBit; G[git].val = currentBit; G[git].M = -1; git++; } } //zbuduj graf dwudzielny for(int y=0,it=0;y<n;y++) for(int x=0;x<n;x++,it++){ for(int x1=0;x1<n;x1++) if(G[it].val != G[it+x1-x].val) G[it].Neis.push_back(it+x1-x); for(int y1=0;y1<n;y1++) if(G[it].val != G[it+(y1-y)*n].val) G[it].Neis.push_back(it+(y1-y)*n); } //turbo-matching vector<bool>Visited(nn); int matchingSize=0; function<bool(int)> extend = [&](int v)->bool{ Visited[v] = true; //try to find close match for(int nei : G[v].Neis) if(G[nei].M==-1){ G[v].M = nei; G[nei].M = v; return true; } //try to find far match for(int nei : G[v].Neis) if(!Visited[G[nei].M] && extend(G[nei].M)){ G[v].M = nei; G[nei].M = v; return true; } return false; }; auto update_matching = [&](){ while(true){ bool extended=false; fill(Visited.begin(), Visited.end(), false); for(int i=0;i<nn;i++) if(G[i].val && G[i].M==-1 && extend(i)){ matchingSize++; extended=true; } if(!extended) break; } }; update_matching(); cout << matchingSize << "\n"; //przetwarzaj punktowe zmiany while(q--){ int x,y; cin >> y >> x; x--; y--; int it=y*n+x; //odłącz od sąsiadów if(G[it].M!=-1){ G[G[it].M].M=-1; G[it].M=-1; matchingSize--; } for(int nei : G[it].Neis) removeFromVector(G[nei].Neis, it); G[it].Neis.clear(); //zmień stan G[it].val = !G[it].val; //podłącz do nowych sąsiadów for(int x1=0;x1<n;x1++) if(G[it].val != G[it+x1-x].val){ G[it].Neis.push_back(it+x1-x); G[it+x1-x].Neis.push_back(it); } for(int y1=0;y1<n;y1++) if(G[it].val != G[it+(y1-y)*n].val){ G[it].Neis.push_back(it+(y1-y)*n); G[it+(y1-y)*n].Neis.push_back(it); } //przelicz matching update_matching(); cout << matchingSize << "\n"; } return 0; } |