#include <bits/stdc++.h> using namespace std; #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " // debug & operator << (debug & dd, P p) { dd << "(" << p.x << ", " << p.y << ")"; return dd; } // Turbo-matching Blazeja Magnowskiego (biblioteczka ICPC Warsaw Eagles 2015) #define MAXN 1'000'005 #define INF #define PB push_back #define MP make_pair #define ST first #define ND second #define REP(i,n) for(int i=0;i<(n);i++) #define FOR(a,b,c) for(int a=b;a<=(c);a++) #define FORD(a,b,c) for (int a=b;a>=(c);a--) #define VAR(v,n) __typeof(n) v=(n) #define ALL(c) c.begin(),c.end() #define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();i++) using namespace std; typedef long long LL; // const int MAXN = 1e6 + 5; int skoj[2][MAXN]; bool bylo[MAXN]; vector<int> v2[MAXN]; //wierzcholki z obu grup numerujemy odpowiednio od 1 do n i od 1 do m void clear(int n) { for(int i = 0; i <= n; ++i) { skoj[0][i] = skoj[1][i] = 0; bylo[i] = false; v2[i].clear(); } } void AddEdge(int a, int b) { // debug() << imie(a) imie(b); v2[b].PB(a); } bool dfs(int x) { FOREACH(it,v2[x]) if (*it != skoj[1][x] && !bylo[*it]) { bylo[*it] = true; if (!skoj[0][*it] || dfs(skoj[0][*it])) { skoj[0][*it] = x; skoj[1][x] = *it; return true; } } return false; } //tablice skoj[2] beda uzupelnione pewnym maksymalnym skojarzeniem int Turbo_Matching(int n, int m) { bool zmiana = true; while (zmiana) { zmiana = false; FOR(i,1,n) bylo[i] = false; FOR(i,1,m) if (skoj[1][i] == 0) zmiana |= dfs(i); } int cnt = 0; FOR(i, 1, m) if(skoj[1][i] != 0) { cnt++; } return cnt; } int main() { int n, m, q; scanf("%d%d%d", &n, &m, &q); vector<vector<bool>> state(n, vector<bool>(n)); auto flip = [&](int x, int y) { if(x < n && y < n) { state[x][y] = !state[x][y]; } }; while(m--) { int x1, x2, y1, y2; scanf("%d%d%d%d", &x1, &y1, &x2, &y2); x1--; x2--; y1--; y2--; flip(x1, y1); flip(x2 + 1, y1); flip(x1, y2 + 1); flip(x2 + 1, y2 + 1); // for(int i = x1; i <= x2; ++i) { // for(int j = y1; j <= y2; ++j) { // state[i][j] = !state[i][j]; // } // } } for(int row = 0; row < n; ++row) { for(int col = 1; col < n; ++col) { if(state[row][col-1]) { state[row][col] = !state[row][col]; } } } for(int row = 1; row < n; ++row) { for(int col = 0; col < n; ++col) { if(state[row-1][col]) { state[row][col] = !state[row][col]; } } } cerr << "=="; // debug() << imie(state); vector<vector<int>> id(n, vector<int>(n)); for(int nr = 0; nr <= q; ++nr) { if(nr != 0) { int x, y; scanf("%d%d", &x, &y); state[x-1][y-1] = !state[x-1][y-1]; } int nxt[2] = {0, 0}; for(int row = 0; row < n; ++row) { for(int col = 0; col < n; ++col) { id[row][col] = ++nxt[state[row][col]]; } } for(int row = 0; row < n; ++row) { for(int col = 0; col < n; ++col) { for(int r2 = row + 1; r2 < n; ++r2) { if(state[row][col] != state[r2][col]) { int a = id[row][col]; int b = id[r2][col]; if(state[row][col]) { swap(a, b); } AddEdge(a, b); } } for(int c2 = col + 1; c2 < n; ++c2) { if(state[row][col] != state[row][c2]) { int a = id[row][col]; int b = id[row][c2]; if(state[row][col]) { swap(a, b); } AddEdge(a, b); } } } } int answer = Turbo_Matching(n * n + 1, n * n + 1); printf("%d\n", answer); clear(n * n + 3); } }
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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | #include <bits/stdc++.h> using namespace std; #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " // debug & operator << (debug & dd, P p) { dd << "(" << p.x << ", " << p.y << ")"; return dd; } // Turbo-matching Blazeja Magnowskiego (biblioteczka ICPC Warsaw Eagles 2015) #define MAXN 1'000'005 #define INF #define PB push_back #define MP make_pair #define ST first #define ND second #define REP(i,n) for(int i=0;i<(n);i++) #define FOR(a,b,c) for(int a=b;a<=(c);a++) #define FORD(a,b,c) for (int a=b;a>=(c);a--) #define VAR(v,n) __typeof(n) v=(n) #define ALL(c) c.begin(),c.end() #define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();i++) using namespace std; typedef long long LL; // const int MAXN = 1e6 + 5; int skoj[2][MAXN]; bool bylo[MAXN]; vector<int> v2[MAXN]; //wierzcholki z obu grup numerujemy odpowiednio od 1 do n i od 1 do m void clear(int n) { for(int i = 0; i <= n; ++i) { skoj[0][i] = skoj[1][i] = 0; bylo[i] = false; v2[i].clear(); } } void AddEdge(int a, int b) { // debug() << imie(a) imie(b); v2[b].PB(a); } bool dfs(int x) { FOREACH(it,v2[x]) if (*it != skoj[1][x] && !bylo[*it]) { bylo[*it] = true; if (!skoj[0][*it] || dfs(skoj[0][*it])) { skoj[0][*it] = x; skoj[1][x] = *it; return true; } } return false; } //tablice skoj[2] beda uzupelnione pewnym maksymalnym skojarzeniem int Turbo_Matching(int n, int m) { bool zmiana = true; while (zmiana) { zmiana = false; FOR(i,1,n) bylo[i] = false; FOR(i,1,m) if (skoj[1][i] == 0) zmiana |= dfs(i); } int cnt = 0; FOR(i, 1, m) if(skoj[1][i] != 0) { cnt++; } return cnt; } int main() { int n, m, q; scanf("%d%d%d", &n, &m, &q); vector<vector<bool>> state(n, vector<bool>(n)); auto flip = [&](int x, int y) { if(x < n && y < n) { state[x][y] = !state[x][y]; } }; while(m--) { int x1, x2, y1, y2; scanf("%d%d%d%d", &x1, &y1, &x2, &y2); x1--; x2--; y1--; y2--; flip(x1, y1); flip(x2 + 1, y1); flip(x1, y2 + 1); flip(x2 + 1, y2 + 1); // for(int i = x1; i <= x2; ++i) { // for(int j = y1; j <= y2; ++j) { // state[i][j] = !state[i][j]; // } // } } for(int row = 0; row < n; ++row) { for(int col = 1; col < n; ++col) { if(state[row][col-1]) { state[row][col] = !state[row][col]; } } } for(int row = 1; row < n; ++row) { for(int col = 0; col < n; ++col) { if(state[row-1][col]) { state[row][col] = !state[row][col]; } } } cerr << "=="; // debug() << imie(state); vector<vector<int>> id(n, vector<int>(n)); for(int nr = 0; nr <= q; ++nr) { if(nr != 0) { int x, y; scanf("%d%d", &x, &y); state[x-1][y-1] = !state[x-1][y-1]; } int nxt[2] = {0, 0}; for(int row = 0; row < n; ++row) { for(int col = 0; col < n; ++col) { id[row][col] = ++nxt[state[row][col]]; } } for(int row = 0; row < n; ++row) { for(int col = 0; col < n; ++col) { for(int r2 = row + 1; r2 < n; ++r2) { if(state[row][col] != state[r2][col]) { int a = id[row][col]; int b = id[r2][col]; if(state[row][col]) { swap(a, b); } AddEdge(a, b); } } for(int c2 = col + 1; c2 < n; ++c2) { if(state[row][col] != state[row][c2]) { int a = id[row][col]; int b = id[row][c2]; if(state[row][col]) { swap(a, b); } AddEdge(a, b); } } } } int answer = Turbo_Matching(n * n + 1, n * n + 1); printf("%d\n", answer); clear(n * n + 3); } } |