#include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for(int i = (a); i <= (b); ++i) #define REP(i, n) for(int i = 0; i < (n); ++i) #define ff first #define ss second using ll = long long; using pii = pair<int, int>; template<class T> int size(T && a) { return (int)(a.size()); } ostream& operator << (ostream &os, string str) { for(char c : str) os << c; return os; } template <class A, class B> ostream& operator << (ostream &os, const pair<A, B> &p) { return os << '(' << p.ff << "," << p.ss << ')'; } template <class T> auto operator << (ostream &os, T &&x) -> decltype(x.begin(), os) { os << '{'; for(auto it = x.begin(); it != x.end(); ++it) os << *it << (it == prev(x.end()) ? "" : " "); return os << '}'; } template <class T> ostream& operator << (ostream &os, vector<vector<T>> vec) { for(auto x : vec) os << "\n " << x; return os; } void dump() {} template <class T, class... Args> void dump(T &&x, Args... args) { cerr << x << "; "; dump(args...); } #ifdef DEBUG # define debug(x...) cerr << "[" #x "]: ", dump(x), cerr << '\n' #else # define debug(...) 0 #endif //-------------------------------------------------- int n, m, q; vector<vector<bool>> board; vector<vector<int>> pref; void flip(int x1, int y1, int x2, int y2) { FOR(i, x1, x2) { pref[i][y1]++; pref[i][y2+1]--; } } void preprocessing() { REP(i, m) { int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; --x1, --y1, --x2, --y2; flip(x1, y1, x2, y2); } debug(pref); REP(i, n) REP(j, n) { if(j > 0) pref[i][j] += pref[i][j-1]; if(pref[i][j] % 2 == 1) board[i][j] = board[i][j] ^ 1; } } vector<vector<int>> graph; vector<int> vis, match; int t = 0; bool TurboMatching(int v) { vis[v] = t; for(int u : graph[v]) if(match[u] == -1) { match[u] = v; return true; } for(int u : graph[v]) if(match[u] != -1) if(vis[match[u]] != t and TurboMatching(match[u])){ match[u] = v; return true; } return false; } int solve() { graph = vector<vector<int>>(n*n); vis = vector<int>(n*n); match = vector<int>(n*n, -1); REP(i, n) REP(j, n) { FOR(i1, i+1, n-1) { if(board[i][j] != board[i1][j]) { int a = i*n+j, b = i1*n+j; graph[a].emplace_back(b); graph[b].emplace_back(a); } } FOR(j1, j+1, n-1) { if(board[i][j] != board[i][j1]) { int a = i*n+j, b = i*n+j1; graph[a].emplace_back(b); graph[b].emplace_back(a); } } } int ret = 0; REP(i, n) REP(j, n) { if(board[i][j]) ret += TurboMatching(i*n+j); ++t; } return ret; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m >> q; board = vector<vector<bool>>(n, vector<bool>(n)); pref = vector<vector<int>> (n, vector<int>(n+1)); preprocessing(); debug(board); cout << solve() << '\n'; while(q--) { int x, y; cin >> x >> y; --x, --y; board[x][y] = board[x][y] ^ 1; debug(board); cout << solve() << '\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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | #include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for(int i = (a); i <= (b); ++i) #define REP(i, n) for(int i = 0; i < (n); ++i) #define ff first #define ss second using ll = long long; using pii = pair<int, int>; template<class T> int size(T && a) { return (int)(a.size()); } ostream& operator << (ostream &os, string str) { for(char c : str) os << c; return os; } template <class A, class B> ostream& operator << (ostream &os, const pair<A, B> &p) { return os << '(' << p.ff << "," << p.ss << ')'; } template <class T> auto operator << (ostream &os, T &&x) -> decltype(x.begin(), os) { os << '{'; for(auto it = x.begin(); it != x.end(); ++it) os << *it << (it == prev(x.end()) ? "" : " "); return os << '}'; } template <class T> ostream& operator << (ostream &os, vector<vector<T>> vec) { for(auto x : vec) os << "\n " << x; return os; } void dump() {} template <class T, class... Args> void dump(T &&x, Args... args) { cerr << x << "; "; dump(args...); } #ifdef DEBUG # define debug(x...) cerr << "[" #x "]: ", dump(x), cerr << '\n' #else # define debug(...) 0 #endif //-------------------------------------------------- int n, m, q; vector<vector<bool>> board; vector<vector<int>> pref; void flip(int x1, int y1, int x2, int y2) { FOR(i, x1, x2) { pref[i][y1]++; pref[i][y2+1]--; } } void preprocessing() { REP(i, m) { int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; --x1, --y1, --x2, --y2; flip(x1, y1, x2, y2); } debug(pref); REP(i, n) REP(j, n) { if(j > 0) pref[i][j] += pref[i][j-1]; if(pref[i][j] % 2 == 1) board[i][j] = board[i][j] ^ 1; } } vector<vector<int>> graph; vector<int> vis, match; int t = 0; bool TurboMatching(int v) { vis[v] = t; for(int u : graph[v]) if(match[u] == -1) { match[u] = v; return true; } for(int u : graph[v]) if(match[u] != -1) if(vis[match[u]] != t and TurboMatching(match[u])){ match[u] = v; return true; } return false; } int solve() { graph = vector<vector<int>>(n*n); vis = vector<int>(n*n); match = vector<int>(n*n, -1); REP(i, n) REP(j, n) { FOR(i1, i+1, n-1) { if(board[i][j] != board[i1][j]) { int a = i*n+j, b = i1*n+j; graph[a].emplace_back(b); graph[b].emplace_back(a); } } FOR(j1, j+1, n-1) { if(board[i][j] != board[i][j1]) { int a = i*n+j, b = i*n+j1; graph[a].emplace_back(b); graph[b].emplace_back(a); } } } int ret = 0; REP(i, n) REP(j, n) { if(board[i][j]) ret += TurboMatching(i*n+j); ++t; } return ret; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m >> q; board = vector<vector<bool>>(n, vector<bool>(n)); pref = vector<vector<int>> (n, vector<int>(n+1)); preprocessing(); debug(board); cout << solve() << '\n'; while(q--) { int x, y; cin >> x >> y; --x, --y; board[x][y] = board[x][y] ^ 1; debug(board); cout << solve() << '\n'; } } |