#include <bits/stdc++.h> using namespace std; #define PII pair<int, int> #define VI vector<int> #define VPII vector<PII> #define LL long long #define LD long double #define f first #define s second #define MP make_pair #define PB push_back #define endl '\n' // UWAGA #define ALL(c) (c).begin(), (c).end() #define SIZ(c) (int)(c).size() #define REP(i, n) for(int i = 0; i < (int)(n); ++i) #define FOR(i, b, e) for(int i = (b); i <= (int)(e); ++i) #define FORD(i, b, e) for(int i = (b); i >= (int)(e); --i) #define random_shuffle2(a, b) shuffle(a, b, rng) #define sim template<class n sim, class s> ostream & operator << (ostream &p, pair<n, s> x) {return p << "<" << x.f << ", " << x.s << ">";} sim> auto operator << (ostream &p, n y) -> typename enable_if<!is_same<n, string>::value, decltype(y.begin(), p)>::type {int o = 0; p << "{"; for(auto c: y) {if(o++) p << ", "; p << c;} return p << "}";} void dor() {cerr << endl;} sim, class...s> void dor(n p, s...y) {cerr << p << " "; dor(y...);} sim, class s> void mini(n &p, s y) {if(p>y) p = y;} sim, class s> void maxi(n &p, s y) {if(p<y) p = y;} #ifdef DEB #define debug(...) dor(__FUNCTION__, ":", __LINE__, ": ", __VA_ARGS__) #else #define debug(...) #endif #define x skajlasdfj #define plus sakjfkdfjlfj #define I(x) #x " =", (x), " " #define A(a, i) #a "[" #i " =", i, "] =", a[i], " " mt19937_64 rng((unsigned long long)chrono::steady_clock::now().time_since_epoch().count() + 2077); const int MXN = 300 * 1000 + 5; const int BASE = 1 << 19; int jed[BASE*2]; bool do_xor[BASE*2]; void upd(int id, int a, int b, bool x) { if(x) { jed[id] = (b - a + 1) - jed[id]; do_xor[id] ^= x; } } void shift(int id, int a, int b) { upd(id*2, a, (a+b)/2, do_xor[id]); upd(id*2+1, (a+b)/2+1, b, do_xor[id]); do_xor[id] = 0; } void insert(int a, int b, bool x, int p = 0, int k = BASE-1, int id = 1) { if(b < p || k < a)return; if(a <= p && k <= b) { upd(id, p, k, x); return; } shift(id, p, k); int mid = (p+k)/2; insert(a, b, x, p, mid, id*2); insert(a, b, x, mid+1, k, id*2+1); jed[id] = jed[id*2] + jed[id*2+1]; } int query(int a, int b, int p = 0, int k = BASE-1, int id = 1) { if(b < p || k < a)return 0; if(a <= p && k <= b) return jed[id]; shift(id, p, k); int mid = (p+k)/2; return query(a, b , p, mid, id*2) + query(a, b, mid+1, k, id*2+1); } int n; int wie[MXN]; int kol[MXN]; int wie2[MXN]; int kol2[MXN]; int x[MXN]; int y[MXN]; LL all; bool prev_all = 0; list<int> plus; // w plus trzymamy aktualny ciąg kolumn // trzymamy go tak, że mamy posortowaną listę indeksów // i każdy indeks, znaczy, że trzeba dodać 1 do [i, n] żeby otrzymać kol2 aktualne LL get_res() // O(n) { if((n-kol2[1]) * 2 <= n)return all; if(wie2[1] * 2 <= n)return all; plus.clear(); FOR(i, 1, n)REP(u, kol2[i] - kol2[i-1])plus.PB(i); plus.PB(n+1); auto wsk = plus.begin(); auto wsk2 = wsk; LL sum = 0; FOR(i, 1, n) { int have_wie = wie2[i]; if(*plus.begin() == n+1){sum += have_wie;continue;} while(wsk != plus.end() && n - *wsk + 1 > have_wie)wsk++; have_wie -= (n - *wsk + 1); // done all in [wsk, m] if(plus.begin() != wsk) { wsk2 = wsk; wsk2--; int now = min(have_wie, n - *wsk2 + 1); have_wie -= now; // segment is [wsk2, wsk2+now-1] *wsk2 += now; // move plus by no of used fiels } else {wsk2 = wsk; wsk2++;} if(*wsk != n+1)plus.erase(wsk); // remove right used plus wsk = wsk2; if(wsk != plus.begin())wsk--; sum += have_wie; } if(sum == 0)prev_all = 1; return all - sum; } VPII inserts[2][MXN]; VI que[MXN]; map<PII, int> mat; int find_first_wie(int *t, int x) { int pos = 0; FORD(i, 19, 0) if(pos + (1<<i) <= n && t[pos + (1<<i)] > x)pos += 1 << i; pos++; return pos; } int find_last_wie(int *t, int x) { int pos = 0; FORD(i, 19, 0) if(pos + (1<<i) <= n && t[pos + (1<<i)] >= x)pos += 1 << i; return pos; } int find_first_kol(int *t, int x) { int pos = 0; FORD(i, 19, 0) if(pos + (1<<i) <= n && t[pos + (1<<i)] < x)pos += 1 << i; pos++; return pos; } int find_last_kol(int *t, int x) { int pos = 0; FORD(i, 19, 0) if(pos + (1<<i) <= n && t[pos + (1<<i)] <= x)pos += 1 << i; return pos; } void add(int x, int y) { wie2[find_first_wie(wie2, wie[x])]++; // uwaga bo wie2 i kol2 różne kolejności posortowania mają kol2[find_first_kol(kol2, kol[y])]--; } void rem(int x, int y) { wie2[find_last_wie(wie2, wie[x])]--; kol2[find_last_kol(kol2, kol[y])]++; } int main() { int m, q; scanf("%d%d%d", &n, &m, &q); REP(u, m) { int i1, i2, j1, j2; scanf("%d%d%d%d", &i1, &j1, &i2, &j2); inserts[0][i1].PB(MP(j1, j2)); inserts[0][i2+1].PB(MP(j1, j2)); inserts[1][j1].PB(MP(i1, i2)); inserts[1][j2+1].PB(MP(i1, i2)); } REP(i, q) { scanf("%d%d", &x[i], &y[i]); que[x[i]].PB(y[i]); } REP(u, 2) { REP(i, BASE*2){jed[i] = 0; do_xor[i] = 0;} FOR(i, 1, n) { for(auto j: inserts[u][i]) { insert(j.f, j.s, 1); } if(u == 0) { wie[i] = query(1, n); all += wie[i]; for(auto j: que[i])mat[MP(i, j)] = query(j, j); } else { kol[i] = query(1, n); } } } FOR(i, 1, n)wie2[i] = wie[i]; FOR(i, 1, n)kol2[i] = kol[i] = n-kol[i]; sort(kol2+1, kol2+n+1); sort(wie2+1, wie2+n+1); reverse(wie2 + 1, wie2+n+1); REP(i, q+1) { if(prev_all)printf("%lld\n", all); else printf("%lld\n", get_res()); if(i == q)break; mat[MP(x[i], y[i])] ^= 1; if(mat[MP(x[i], y[i])]) { prev_all = 0; add(x[i], y[i]); wie[x[i]]++; kol[y[i]]--; // kol[i] holds n - real_kol[i] all++; } else { rem(x[i], y[i]); wie[x[i]]--; kol[y[i]]++; all--; } } }
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 | #include <bits/stdc++.h> using namespace std; #define PII pair<int, int> #define VI vector<int> #define VPII vector<PII> #define LL long long #define LD long double #define f first #define s second #define MP make_pair #define PB push_back #define endl '\n' // UWAGA #define ALL(c) (c).begin(), (c).end() #define SIZ(c) (int)(c).size() #define REP(i, n) for(int i = 0; i < (int)(n); ++i) #define FOR(i, b, e) for(int i = (b); i <= (int)(e); ++i) #define FORD(i, b, e) for(int i = (b); i >= (int)(e); --i) #define random_shuffle2(a, b) shuffle(a, b, rng) #define sim template<class n sim, class s> ostream & operator << (ostream &p, pair<n, s> x) {return p << "<" << x.f << ", " << x.s << ">";} sim> auto operator << (ostream &p, n y) -> typename enable_if<!is_same<n, string>::value, decltype(y.begin(), p)>::type {int o = 0; p << "{"; for(auto c: y) {if(o++) p << ", "; p << c;} return p << "}";} void dor() {cerr << endl;} sim, class...s> void dor(n p, s...y) {cerr << p << " "; dor(y...);} sim, class s> void mini(n &p, s y) {if(p>y) p = y;} sim, class s> void maxi(n &p, s y) {if(p<y) p = y;} #ifdef DEB #define debug(...) dor(__FUNCTION__, ":", __LINE__, ": ", __VA_ARGS__) #else #define debug(...) #endif #define x skajlasdfj #define plus sakjfkdfjlfj #define I(x) #x " =", (x), " " #define A(a, i) #a "[" #i " =", i, "] =", a[i], " " mt19937_64 rng((unsigned long long)chrono::steady_clock::now().time_since_epoch().count() + 2077); const int MXN = 300 * 1000 + 5; const int BASE = 1 << 19; int jed[BASE*2]; bool do_xor[BASE*2]; void upd(int id, int a, int b, bool x) { if(x) { jed[id] = (b - a + 1) - jed[id]; do_xor[id] ^= x; } } void shift(int id, int a, int b) { upd(id*2, a, (a+b)/2, do_xor[id]); upd(id*2+1, (a+b)/2+1, b, do_xor[id]); do_xor[id] = 0; } void insert(int a, int b, bool x, int p = 0, int k = BASE-1, int id = 1) { if(b < p || k < a)return; if(a <= p && k <= b) { upd(id, p, k, x); return; } shift(id, p, k); int mid = (p+k)/2; insert(a, b, x, p, mid, id*2); insert(a, b, x, mid+1, k, id*2+1); jed[id] = jed[id*2] + jed[id*2+1]; } int query(int a, int b, int p = 0, int k = BASE-1, int id = 1) { if(b < p || k < a)return 0; if(a <= p && k <= b) return jed[id]; shift(id, p, k); int mid = (p+k)/2; return query(a, b , p, mid, id*2) + query(a, b, mid+1, k, id*2+1); } int n; int wie[MXN]; int kol[MXN]; int wie2[MXN]; int kol2[MXN]; int x[MXN]; int y[MXN]; LL all; bool prev_all = 0; list<int> plus; // w plus trzymamy aktualny ciąg kolumn // trzymamy go tak, że mamy posortowaną listę indeksów // i każdy indeks, znaczy, że trzeba dodać 1 do [i, n] żeby otrzymać kol2 aktualne LL get_res() // O(n) { if((n-kol2[1]) * 2 <= n)return all; if(wie2[1] * 2 <= n)return all; plus.clear(); FOR(i, 1, n)REP(u, kol2[i] - kol2[i-1])plus.PB(i); plus.PB(n+1); auto wsk = plus.begin(); auto wsk2 = wsk; LL sum = 0; FOR(i, 1, n) { int have_wie = wie2[i]; if(*plus.begin() == n+1){sum += have_wie;continue;} while(wsk != plus.end() && n - *wsk + 1 > have_wie)wsk++; have_wie -= (n - *wsk + 1); // done all in [wsk, m] if(plus.begin() != wsk) { wsk2 = wsk; wsk2--; int now = min(have_wie, n - *wsk2 + 1); have_wie -= now; // segment is [wsk2, wsk2+now-1] *wsk2 += now; // move plus by no of used fiels } else {wsk2 = wsk; wsk2++;} if(*wsk != n+1)plus.erase(wsk); // remove right used plus wsk = wsk2; if(wsk != plus.begin())wsk--; sum += have_wie; } if(sum == 0)prev_all = 1; return all - sum; } VPII inserts[2][MXN]; VI que[MXN]; map<PII, int> mat; int find_first_wie(int *t, int x) { int pos = 0; FORD(i, 19, 0) if(pos + (1<<i) <= n && t[pos + (1<<i)] > x)pos += 1 << i; pos++; return pos; } int find_last_wie(int *t, int x) { int pos = 0; FORD(i, 19, 0) if(pos + (1<<i) <= n && t[pos + (1<<i)] >= x)pos += 1 << i; return pos; } int find_first_kol(int *t, int x) { int pos = 0; FORD(i, 19, 0) if(pos + (1<<i) <= n && t[pos + (1<<i)] < x)pos += 1 << i; pos++; return pos; } int find_last_kol(int *t, int x) { int pos = 0; FORD(i, 19, 0) if(pos + (1<<i) <= n && t[pos + (1<<i)] <= x)pos += 1 << i; return pos; } void add(int x, int y) { wie2[find_first_wie(wie2, wie[x])]++; // uwaga bo wie2 i kol2 różne kolejności posortowania mają kol2[find_first_kol(kol2, kol[y])]--; } void rem(int x, int y) { wie2[find_last_wie(wie2, wie[x])]--; kol2[find_last_kol(kol2, kol[y])]++; } int main() { int m, q; scanf("%d%d%d", &n, &m, &q); REP(u, m) { int i1, i2, j1, j2; scanf("%d%d%d%d", &i1, &j1, &i2, &j2); inserts[0][i1].PB(MP(j1, j2)); inserts[0][i2+1].PB(MP(j1, j2)); inserts[1][j1].PB(MP(i1, i2)); inserts[1][j2+1].PB(MP(i1, i2)); } REP(i, q) { scanf("%d%d", &x[i], &y[i]); que[x[i]].PB(y[i]); } REP(u, 2) { REP(i, BASE*2){jed[i] = 0; do_xor[i] = 0;} FOR(i, 1, n) { for(auto j: inserts[u][i]) { insert(j.f, j.s, 1); } if(u == 0) { wie[i] = query(1, n); all += wie[i]; for(auto j: que[i])mat[MP(i, j)] = query(j, j); } else { kol[i] = query(1, n); } } } FOR(i, 1, n)wie2[i] = wie[i]; FOR(i, 1, n)kol2[i] = kol[i] = n-kol[i]; sort(kol2+1, kol2+n+1); sort(wie2+1, wie2+n+1); reverse(wie2 + 1, wie2+n+1); REP(i, q+1) { if(prev_all)printf("%lld\n", all); else printf("%lld\n", get_res()); if(i == q)break; mat[MP(x[i], y[i])] ^= 1; if(mat[MP(x[i], y[i])]) { prev_all = 0; add(x[i], y[i]); wie[x[i]]++; kol[y[i]]--; // kol[i] holds n - real_kol[i] all++; } else { rem(x[i], y[i]); wie[x[i]]--; kol[y[i]]++; all--; } } } |