#include <stdio.h> #include <string.h> #include <stdlib.h> #define MAX 500555 #define END (1<<29) typedef long long ll; typedef struct { int b, e; int key; int left, right; int cnt; int sum; } TNode; int cmpint(const void *av, const void *bv) { int a = *(int *)av; int b = *(int *)bv; return a-b; } class SegmentTree { int x[MAX*2]; TNode tree[MAX*4]; public: int size; void init(int _x[][2], int _n) { size = 0; // kopiuje konce for (int i=0; i<_n; i++) { x[2*i] = _x[i][0]; x[2*i+1] = _x[i][1]; } // sortuje konce qsort(x, 2*_n, sizeof(int), cmpint); // usuwa duplikaty int n = 0; for (int i=1; i<2*_n; i++) { if (x[n] != x[i]) { n++; x[n] = x[i]; } } build(0, n); } int build(int s, int t) { int v = size++; tree[v].b = x[s]; tree[v].e = x[t]; tree[v].left = -1; tree[v].right = -1; tree[v].cnt = 0; tree[v].sum = 0; if (s + 1 < t) { tree[v].key = x[(s+t)/2]; tree[v].left = build(s, (s+t)/2); tree[v].right = build((s+t)/2, t); } return v; } void insert(int b, int e, int v) { if (b == e) return; //if (v==0) printf("insert(%d, %d)\n", b, e); if (b <= tree[v].b && e >= tree[v].e) { // assign [b,e] to v tree[v].cnt ++; tree[v].sum = tree[v].e - tree[v].b; } else { if (b < tree[v].key) insert(b, e, tree[v].left); if (tree[v].key < e) insert(b, e, tree[v].right); if (tree[v].cnt == 0) tree[v].sum = tree[tree[v].left].sum + tree[tree[v].right].sum; } } void remove(int b, int e, int v) { if (b == e) return; //if (v==0) printf("remove(%d, %d)\n", b, e); if (b <= tree[v].b && e >= tree[v].e) { // remove [b,e] from v tree[v].cnt --; if (tree[v].cnt < 0) { //printf("ERROR\n"); } } else { if (b < tree[v].key) remove(b, e, tree[v].left); if (tree[v].key < e) remove(b, e, tree[v].right); } if (tree[v].cnt == 0) { if (tree[v].left >= 0) { tree[v].sum = tree[tree[v].left].sum + tree[tree[v].right].sum; } else { tree[v].sum = 0; } } } int getUnion() { return tree[0].sum; } }; int (*z)[2]; int Z; int ends[MAX*2]; SegmentTree T; int ins[MAX]; int getEndX(int pnr) { int idx = 0; if (pnr & END) { pnr ^= END; idx = 1; } return z[pnr][idx]; } int cmpend(const void *av, const void *bv) { int a = *(int *)av; int b = *(int *)bv; return getEndX(a) - getEndX(b); } void removeInterval(int px, int i) { if (z[i][0] <= px && px < z[i][1]) { T.remove(0, z[i][0], 0); T.remove(z[i][1], Z, 0); } else { T.remove(z[i][0], z[i][1], 0); } } void addInterval(int px, int i) { if (ins[i] >= 0) { removeInterval(ins[i], i); } ins[i] = px; if (z[i][0] <= px && px < z[i][1]) { T.insert(0, z[i][0], 0); T.insert(z[i][1], Z, 0); } else { T.insert(z[i][0], z[i][1], 0); } } ll solve(int xx[][2], int n, int XX) { z = xx; Z = XX; for (int i=0; i<n; i++) { ins[i] = -1; ends[2*i] = i; ends[2*i+1] = i | END; } qsort(ends, 2*n, sizeof(int), cmpend); T.init(z, n); int minUnion = Z; for (int i=0; i<n; i++) { addInterval(0, i); } int s = 0, e; int sx = 0, ex; while (s < 2*n) { for (e=s+1; e<2*n; e++) { ex = getEndX(ends[e]); if (sx != ex) break; } for (int i=s; i<e; i++) { int pe = ends[i]; if (pe & END) pe ^= END; addInterval(sx, pe); } if (minUnion > T.getUnion()) { minUnion = T.getUnion(); } //printf("minUnion=%d s=%d sx=%d\n", minUnion, s, sx); s = e; sx = ex; } //printf("wynik=%d\n\n", Z-minUnion); return Z-minUnion; } int main() { int n; int x[MAX][2]; int y[MAX][2]; int X,Y; scanf("%d %d %d", &n, &X, &Y); int j = 0; for (int i=0; i<n; i++) { int x1, y1, x2, y2; scanf("%d %d %d %d", &x[i][0], &y[i][0], &x[i][1], &y[i][1]); if (x[i][0] > x[i][1]) { int t = x[i][0]; x[i][0] = x[i][1]; x[i][1] = t; } if (y[i][0] > y[i][1]) { int t = y[i][0]; y[i][0] = y[i][1]; y[i][1] = t; } } x[n][0] = y[n][0] = 0; x[n][1] = X; y[n][1] = Y; ll s1 = solve(x, n+1, X); ll s2 = solve(y, n+1, Y); printf("%lld\n", s1*s2); return 0; }
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 | #include <stdio.h> #include <string.h> #include <stdlib.h> #define MAX 500555 #define END (1<<29) typedef long long ll; typedef struct { int b, e; int key; int left, right; int cnt; int sum; } TNode; int cmpint(const void *av, const void *bv) { int a = *(int *)av; int b = *(int *)bv; return a-b; } class SegmentTree { int x[MAX*2]; TNode tree[MAX*4]; public: int size; void init(int _x[][2], int _n) { size = 0; // kopiuje konce for (int i=0; i<_n; i++) { x[2*i] = _x[i][0]; x[2*i+1] = _x[i][1]; } // sortuje konce qsort(x, 2*_n, sizeof(int), cmpint); // usuwa duplikaty int n = 0; for (int i=1; i<2*_n; i++) { if (x[n] != x[i]) { n++; x[n] = x[i]; } } build(0, n); } int build(int s, int t) { int v = size++; tree[v].b = x[s]; tree[v].e = x[t]; tree[v].left = -1; tree[v].right = -1; tree[v].cnt = 0; tree[v].sum = 0; if (s + 1 < t) { tree[v].key = x[(s+t)/2]; tree[v].left = build(s, (s+t)/2); tree[v].right = build((s+t)/2, t); } return v; } void insert(int b, int e, int v) { if (b == e) return; //if (v==0) printf("insert(%d, %d)\n", b, e); if (b <= tree[v].b && e >= tree[v].e) { // assign [b,e] to v tree[v].cnt ++; tree[v].sum = tree[v].e - tree[v].b; } else { if (b < tree[v].key) insert(b, e, tree[v].left); if (tree[v].key < e) insert(b, e, tree[v].right); if (tree[v].cnt == 0) tree[v].sum = tree[tree[v].left].sum + tree[tree[v].right].sum; } } void remove(int b, int e, int v) { if (b == e) return; //if (v==0) printf("remove(%d, %d)\n", b, e); if (b <= tree[v].b && e >= tree[v].e) { // remove [b,e] from v tree[v].cnt --; if (tree[v].cnt < 0) { //printf("ERROR\n"); } } else { if (b < tree[v].key) remove(b, e, tree[v].left); if (tree[v].key < e) remove(b, e, tree[v].right); } if (tree[v].cnt == 0) { if (tree[v].left >= 0) { tree[v].sum = tree[tree[v].left].sum + tree[tree[v].right].sum; } else { tree[v].sum = 0; } } } int getUnion() { return tree[0].sum; } }; int (*z)[2]; int Z; int ends[MAX*2]; SegmentTree T; int ins[MAX]; int getEndX(int pnr) { int idx = 0; if (pnr & END) { pnr ^= END; idx = 1; } return z[pnr][idx]; } int cmpend(const void *av, const void *bv) { int a = *(int *)av; int b = *(int *)bv; return getEndX(a) - getEndX(b); } void removeInterval(int px, int i) { if (z[i][0] <= px && px < z[i][1]) { T.remove(0, z[i][0], 0); T.remove(z[i][1], Z, 0); } else { T.remove(z[i][0], z[i][1], 0); } } void addInterval(int px, int i) { if (ins[i] >= 0) { removeInterval(ins[i], i); } ins[i] = px; if (z[i][0] <= px && px < z[i][1]) { T.insert(0, z[i][0], 0); T.insert(z[i][1], Z, 0); } else { T.insert(z[i][0], z[i][1], 0); } } ll solve(int xx[][2], int n, int XX) { z = xx; Z = XX; for (int i=0; i<n; i++) { ins[i] = -1; ends[2*i] = i; ends[2*i+1] = i | END; } qsort(ends, 2*n, sizeof(int), cmpend); T.init(z, n); int minUnion = Z; for (int i=0; i<n; i++) { addInterval(0, i); } int s = 0, e; int sx = 0, ex; while (s < 2*n) { for (e=s+1; e<2*n; e++) { ex = getEndX(ends[e]); if (sx != ex) break; } for (int i=s; i<e; i++) { int pe = ends[i]; if (pe & END) pe ^= END; addInterval(sx, pe); } if (minUnion > T.getUnion()) { minUnion = T.getUnion(); } //printf("minUnion=%d s=%d sx=%d\n", minUnion, s, sx); s = e; sx = ex; } //printf("wynik=%d\n\n", Z-minUnion); return Z-minUnion; } int main() { int n; int x[MAX][2]; int y[MAX][2]; int X,Y; scanf("%d %d %d", &n, &X, &Y); int j = 0; for (int i=0; i<n; i++) { int x1, y1, x2, y2; scanf("%d %d %d %d", &x[i][0], &y[i][0], &x[i][1], &y[i][1]); if (x[i][0] > x[i][1]) { int t = x[i][0]; x[i][0] = x[i][1]; x[i][1] = t; } if (y[i][0] > y[i][1]) { int t = y[i][0]; y[i][0] = y[i][1]; y[i][1] = t; } } x[n][0] = y[n][0] = 0; x[n][1] = X; y[n][1] = Y; ll s1 = solve(x, n+1, X); ll s2 = solve(y, n+1, Y); printf("%lld\n", s1*s2); return 0; } |