#include <iostream> #include <vector> #include <algorithm> // using https://robert1003.github.io/2020/01/27/fenwick-tree.html using namespace std; vector<vector<int>> A; vector<vector<vector<int>>> B; vector<vector<int>> HH; vector<int> HHRowSum; vector<int> HHColumnSum; int n, m, q; int lowbit(int x) { return x & (-x); } void upd(int x, int y, int v) { for (int i = x; i <= n; i += lowbit(i)) { for (int j = y; j <= n; j += lowbit(j)) { B[i][j][0] += v; B[i][j][1] += x * v; B[i][j][2] += y * v; B[i][j][3] += x * y * v; } } } int qry(int x, int y) { int ans = 0; for (int i = x; i > 0; i -= lowbit(i)) { for (int j = y; j > 0; j -= lowbit(j)) { ans += (x + 1) * (y + 1) * B[i][j][0] - (y + 1) * B[i][j][1] - (x + 1) * B[i][j][2] + B[i][j][3]; } } return ans; } void update(int x1, int y1, int x2, int y2, int v) { upd(x1, y1, v); upd(x1, y2 + 1, -v); upd(x2 + 1, y1, -v); upd(x2 + 1, y2 + 1, v); } int query(int x1, int y1, int x2, int y2) { return qry(x2, y2) - qry(x1 - 1, y2) - qry(x2, y1 - 1) + qry(x1 - 1, y1 - 1); } void printHH() { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { cout << HH[n - i + 1][j]; } cout << endl; } } void updateHH() { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { HH[i][j] = query(i, j, i, j) % 2; } } } void printSums() { int rowCount = 0; int ColumnCount = 0; for (int i = 1; i <= n; i++) { cout << HHRowSum[i] << " "; rowCount += HHRowSum[i]; } cout << rowCount << endl; for (int i = 1; i <= n; i++) { cout << HHColumnSum[i] << " "; ColumnCount += HHColumnSum[i]; } cout << ColumnCount << endl; } int main() { ios::sync_with_stdio(false); bool LOCAL = false; cin >> n >> m >> q; A.resize(n + 1, vector<int>(n + 2)); B.resize(n + 1, vector<vector<int>>(n + 2, vector<int>(4))); HH.resize(n + 1, vector<int>(n + 1)); int x1, y1, x2, y2; for (int i = 0; i < m; i++) { cin >> y1 >> x1 >> y2 >> x2; update(x1, y1, x2, y2, 1); } updateHH(); //if (LOCAL) //{ // printHH(); //} HHRowSum.resize(n + 1); HHColumnSum.resize(n + 1); long long int HHGuys = 0; long long int allGuys = (long long int)n * n; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { HHRowSum[i] += HH[i][j]; HHColumnSum[i] += HH[j][i]; HHGuys += HH[i][j]; } } int blockedColumns = 0; int blockedRows = 0; int emptyColumns = 0; int emptyRows = 0; for (int i = 1; i <= n; i++) { if (HHRowSum[i] == n) { blockedRows++; } if (HHColumnSum[i] == n) { blockedColumns++; } if (HHRowSum[i] == 0) { emptyRows++; } if (HHColumnSum[i] == 0) { emptyColumns++; } } //if (LOCAL) //{ // cout << allGuys << endl; // cout << HHGuys << endl; // cout << (long long int)blockedRows * blockedColumns << endl; // cout << blockedRows << " " << blockedColumns << endl; // cout << emptyRows << " " << emptyColumns << endl; // printSums(); //} cout << max((long long int)0, min(allGuys - HHGuys - ((long long int)emptyRows * emptyColumns), HHGuys - ((long long int)blockedRows * blockedColumns))) << endl; for (int x = 0; x < q; x++) { cin >> y1 >> x1; HH[x1][y1] ^= 1; if (HH[x1][y1] == 0) { HHRowSum[x1]--; HHColumnSum[y1]--; if (HHRowSum[x1] == n - 1) { blockedRows--; } if (HHColumnSum[y1] == n - 1) { blockedColumns--; } if (HHRowSum[x1] == 0) { emptyRows++; } if (HHColumnSum[y1] == 0) { emptyColumns++; } HHGuys--; } else { HHRowSum[x1]++; HHColumnSum[y1]++; if (HHRowSum[x1] == n) { blockedRows++; } if (HHColumnSum[y1] == n) { blockedColumns++; } if (HHRowSum[x1] == 1) { emptyRows--; } if (HHColumnSum[y1] == 1) { emptyColumns--; } HHGuys++; } //if (LOCAL) //{ // printHH(); // printSums(); // cout << blockedRows << " " << blockedColumns << endl; // cout << HHGuys << endl; //} cout << max((long long int)0, min(allGuys - HHGuys - ((long long int)emptyRows * emptyColumns), HHGuys - ((long long int)blockedRows * blockedColumns))) << endl; } }
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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 | #include <iostream> #include <vector> #include <algorithm> // using https://robert1003.github.io/2020/01/27/fenwick-tree.html using namespace std; vector<vector<int>> A; vector<vector<vector<int>>> B; vector<vector<int>> HH; vector<int> HHRowSum; vector<int> HHColumnSum; int n, m, q; int lowbit(int x) { return x & (-x); } void upd(int x, int y, int v) { for (int i = x; i <= n; i += lowbit(i)) { for (int j = y; j <= n; j += lowbit(j)) { B[i][j][0] += v; B[i][j][1] += x * v; B[i][j][2] += y * v; B[i][j][3] += x * y * v; } } } int qry(int x, int y) { int ans = 0; for (int i = x; i > 0; i -= lowbit(i)) { for (int j = y; j > 0; j -= lowbit(j)) { ans += (x + 1) * (y + 1) * B[i][j][0] - (y + 1) * B[i][j][1] - (x + 1) * B[i][j][2] + B[i][j][3]; } } return ans; } void update(int x1, int y1, int x2, int y2, int v) { upd(x1, y1, v); upd(x1, y2 + 1, -v); upd(x2 + 1, y1, -v); upd(x2 + 1, y2 + 1, v); } int query(int x1, int y1, int x2, int y2) { return qry(x2, y2) - qry(x1 - 1, y2) - qry(x2, y1 - 1) + qry(x1 - 1, y1 - 1); } void printHH() { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { cout << HH[n - i + 1][j]; } cout << endl; } } void updateHH() { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { HH[i][j] = query(i, j, i, j) % 2; } } } void printSums() { int rowCount = 0; int ColumnCount = 0; for (int i = 1; i <= n; i++) { cout << HHRowSum[i] << " "; rowCount += HHRowSum[i]; } cout << rowCount << endl; for (int i = 1; i <= n; i++) { cout << HHColumnSum[i] << " "; ColumnCount += HHColumnSum[i]; } cout << ColumnCount << endl; } int main() { ios::sync_with_stdio(false); bool LOCAL = false; cin >> n >> m >> q; A.resize(n + 1, vector<int>(n + 2)); B.resize(n + 1, vector<vector<int>>(n + 2, vector<int>(4))); HH.resize(n + 1, vector<int>(n + 1)); int x1, y1, x2, y2; for (int i = 0; i < m; i++) { cin >> y1 >> x1 >> y2 >> x2; update(x1, y1, x2, y2, 1); } updateHH(); //if (LOCAL) //{ // printHH(); //} HHRowSum.resize(n + 1); HHColumnSum.resize(n + 1); long long int HHGuys = 0; long long int allGuys = (long long int)n * n; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { HHRowSum[i] += HH[i][j]; HHColumnSum[i] += HH[j][i]; HHGuys += HH[i][j]; } } int blockedColumns = 0; int blockedRows = 0; int emptyColumns = 0; int emptyRows = 0; for (int i = 1; i <= n; i++) { if (HHRowSum[i] == n) { blockedRows++; } if (HHColumnSum[i] == n) { blockedColumns++; } if (HHRowSum[i] == 0) { emptyRows++; } if (HHColumnSum[i] == 0) { emptyColumns++; } } //if (LOCAL) //{ // cout << allGuys << endl; // cout << HHGuys << endl; // cout << (long long int)blockedRows * blockedColumns << endl; // cout << blockedRows << " " << blockedColumns << endl; // cout << emptyRows << " " << emptyColumns << endl; // printSums(); //} cout << max((long long int)0, min(allGuys - HHGuys - ((long long int)emptyRows * emptyColumns), HHGuys - ((long long int)blockedRows * blockedColumns))) << endl; for (int x = 0; x < q; x++) { cin >> y1 >> x1; HH[x1][y1] ^= 1; if (HH[x1][y1] == 0) { HHRowSum[x1]--; HHColumnSum[y1]--; if (HHRowSum[x1] == n - 1) { blockedRows--; } if (HHColumnSum[y1] == n - 1) { blockedColumns--; } if (HHRowSum[x1] == 0) { emptyRows++; } if (HHColumnSum[y1] == 0) { emptyColumns++; } HHGuys--; } else { HHRowSum[x1]++; HHColumnSum[y1]++; if (HHRowSum[x1] == n) { blockedRows++; } if (HHColumnSum[y1] == n) { blockedColumns++; } if (HHRowSum[x1] == 1) { emptyRows--; } if (HHColumnSum[y1] == 1) { emptyColumns--; } HHGuys++; } //if (LOCAL) //{ // printHH(); // printSums(); // cout << blockedRows << " " << blockedColumns << endl; // cout << HHGuys << endl; //} cout << max((long long int)0, min(allGuys - HHGuys - ((long long int)emptyRows * emptyColumns), HHGuys - ((long long int)blockedRows * blockedColumns))) << endl; } } |