#include <algorithm> #include <cstdio> #include <set> #include <vector> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define PB push_back #define ALL(c) (c).begin(),(c).end() #define SIZE(c) ((int)(c).size()) #define MP make_pair #define FT first #define SD second #define INT(x) int x; scanf("%d", &x) typedef long long LL; typedef pair<int,int> PII; int x1[300000], yy1[300000], x2[300000], y2[300000], a[300000], b[300000]; int xc[300000], yc[300000]; bool nest[300000]; int h; bool f[1 << 20]; int c[1 << 20]; void flip(int a, int b, int node = 1, int l = 0) { if (a >= b) return; int start = node - (1 << l) << h - l; int end = start + (1 << h - l); if (start == a && end == b) { f[node] ^= 1; if (l < h) c[node] = c[node << 1] + c[(node << 1) + 1]; else c[node] = 0; if (f[node]) c[node] = end - start - c[node]; return; } int m = start + end >> 1; flip(a, min(b, m), node << 1, l + 1); flip(max(a, m), b, (node << 1) + 1, l + 1); c[node] = c[node << 1] + c[(node << 1) + 1]; if (f[node]) c[node] = end - start - c[node]; } bool get(int i) { i += 1 << h; bool r = 0; while (i) { r ^= f[i]; i >>= 1; } return r; } int main() { INT(n); INT(m); INT(q); REP(mm,m) { scanf("%d%d%d%d", &x1[mm], &yy1[mm], &x2[mm], &y2[mm]); --x1[mm]; --yy1[mm]; } REP(qq,q) { scanf("%d%d", &a[qq], &b[qq]); --a[qq]; --b[qq]; } vector<pair<int, PII>> v; REP(mm,m) { v.PB(MP(x1[mm], MP(yy1[mm], y2[mm]))); v.PB(MP(x2[mm], MP(yy1[mm], y2[mm]))); } vector<pair<PII, int>> w; REP(qq,q) w.PB(MP(MP(a[qq], b[qq]), qq)); sort(ALL(v)); sort(ALL(w)); while (1 << h < n) ++h; int ii = 0, jj = 0; REP(i,n) { while (ii < SIZE(v) && v[ii].FT == i) { flip(v[ii].SD.FT, v[ii].SD.SD); ++ii; } xc[i] = c[1]; while (jj < SIZE(w) && w[jj].FT.FT == i) { nest[w[jj].SD] = get(w[jj].FT.SD); ++jj; } } REP(i,1<<h+1) f[i] = c[i] = 0; v.clear(); REP(mm,m) { v.PB(MP(yy1[mm], MP(x1[mm], x2[mm]))); v.PB(MP(y2[mm], MP(x1[mm], x2[mm]))); } sort(ALL(v)); ii = 0; REP(i,n) { while (ii < SIZE(v) && v[ii].FT == i) { flip(v[ii].SD.FT, v[ii].SD.SD); ++ii; } yc[i] = c[1]; } LL x0 = 0, xn = 0, y0 = 0, yn = 0, s = 0; REP(i,n) if (!xc[i]) ++x0; else if (xc[i] == n) ++xn; REP(i,n) if (!yc[i]) ++y0; else if (yc[i] == n) ++yn; REP(i,n) s += xc[i]; printf("%lld\n", min(s - xn * yn, LL(n) * n - s - x0 * y0)); set<PII> z; REP(qq,q) { bool found = z.find(MP(a[qq], b[qq])) != z.end(); if (found) z.erase(MP(a[qq], b[qq])); else z.insert(MP(a[qq], b[qq])); if (!found ^ nest[qq]) { ++xc[a[qq]]; ++yc[b[qq]]; ++s; if (xc[a[qq]] == n) ++xn; if (xc[a[qq]] == 1) --x0; if (yc[b[qq]] == n) ++yn; if (yc[b[qq]] == 1) --y0; } else { --xc[a[qq]]; --yc[b[qq]]; --s; if (xc[a[qq]] == 0) ++x0; if (xc[a[qq]] == n - 1) --xn; if (yc[b[qq]] == 0) ++y0; if (yc[b[qq]] == n - 1) --yn; } printf("%lld\n", min(s - xn * yn, LL(n) * n - s - x0 * y0)); } }
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 | #include <algorithm> #include <cstdio> #include <set> #include <vector> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define PB push_back #define ALL(c) (c).begin(),(c).end() #define SIZE(c) ((int)(c).size()) #define MP make_pair #define FT first #define SD second #define INT(x) int x; scanf("%d", &x) typedef long long LL; typedef pair<int,int> PII; int x1[300000], yy1[300000], x2[300000], y2[300000], a[300000], b[300000]; int xc[300000], yc[300000]; bool nest[300000]; int h; bool f[1 << 20]; int c[1 << 20]; void flip(int a, int b, int node = 1, int l = 0) { if (a >= b) return; int start = node - (1 << l) << h - l; int end = start + (1 << h - l); if (start == a && end == b) { f[node] ^= 1; if (l < h) c[node] = c[node << 1] + c[(node << 1) + 1]; else c[node] = 0; if (f[node]) c[node] = end - start - c[node]; return; } int m = start + end >> 1; flip(a, min(b, m), node << 1, l + 1); flip(max(a, m), b, (node << 1) + 1, l + 1); c[node] = c[node << 1] + c[(node << 1) + 1]; if (f[node]) c[node] = end - start - c[node]; } bool get(int i) { i += 1 << h; bool r = 0; while (i) { r ^= f[i]; i >>= 1; } return r; } int main() { INT(n); INT(m); INT(q); REP(mm,m) { scanf("%d%d%d%d", &x1[mm], &yy1[mm], &x2[mm], &y2[mm]); --x1[mm]; --yy1[mm]; } REP(qq,q) { scanf("%d%d", &a[qq], &b[qq]); --a[qq]; --b[qq]; } vector<pair<int, PII>> v; REP(mm,m) { v.PB(MP(x1[mm], MP(yy1[mm], y2[mm]))); v.PB(MP(x2[mm], MP(yy1[mm], y2[mm]))); } vector<pair<PII, int>> w; REP(qq,q) w.PB(MP(MP(a[qq], b[qq]), qq)); sort(ALL(v)); sort(ALL(w)); while (1 << h < n) ++h; int ii = 0, jj = 0; REP(i,n) { while (ii < SIZE(v) && v[ii].FT == i) { flip(v[ii].SD.FT, v[ii].SD.SD); ++ii; } xc[i] = c[1]; while (jj < SIZE(w) && w[jj].FT.FT == i) { nest[w[jj].SD] = get(w[jj].FT.SD); ++jj; } } REP(i,1<<h+1) f[i] = c[i] = 0; v.clear(); REP(mm,m) { v.PB(MP(yy1[mm], MP(x1[mm], x2[mm]))); v.PB(MP(y2[mm], MP(x1[mm], x2[mm]))); } sort(ALL(v)); ii = 0; REP(i,n) { while (ii < SIZE(v) && v[ii].FT == i) { flip(v[ii].SD.FT, v[ii].SD.SD); ++ii; } yc[i] = c[1]; } LL x0 = 0, xn = 0, y0 = 0, yn = 0, s = 0; REP(i,n) if (!xc[i]) ++x0; else if (xc[i] == n) ++xn; REP(i,n) if (!yc[i]) ++y0; else if (yc[i] == n) ++yn; REP(i,n) s += xc[i]; printf("%lld\n", min(s - xn * yn, LL(n) * n - s - x0 * y0)); set<PII> z; REP(qq,q) { bool found = z.find(MP(a[qq], b[qq])) != z.end(); if (found) z.erase(MP(a[qq], b[qq])); else z.insert(MP(a[qq], b[qq])); if (!found ^ nest[qq]) { ++xc[a[qq]]; ++yc[b[qq]]; ++s; if (xc[a[qq]] == n) ++xn; if (xc[a[qq]] == 1) --x0; if (yc[b[qq]] == n) ++yn; if (yc[b[qq]] == 1) --y0; } else { --xc[a[qq]]; --yc[b[qq]]; --s; if (xc[a[qq]] == 0) ++x0; if (xc[a[qq]] == n - 1) --xn; if (yc[b[qq]] == 0) ++y0; if (yc[b[qq]] == n - 1) --yn; } printf("%lld\n", min(s - xn * yn, LL(n) * n - s - x0 * y0)); } } |