USACO Shaping Regions(离散化)
记录一下我一直很怕做的一道离散化题。都卡了大半个月了,今天终于过了。这种离散化的题就一个恶心,不然早就啃过去了。
代码如下:
View Code
1 /* 2 ID: lyon.ly1 3 LANG: C++ 4 TASK: rect1 5 */ 6 7 #include <cstring> 8 #include <iomanip> 9 #include <cmath> 10 #include <cstdio> 11 #include <iostream> 12 #include <algorithm> 13 #include <vector> 14 #include <string> 15 #include <queue> 16 #include <ctime> 17 #include <map> 18 #include <set> 19 20 using namespace std; 21 22 #define PB push_back 23 #define FI first 24 #define SE second 25 #define MPR make_pair 26 #define REP(i, n) for (int i = 0; i < n; i++) 27 #define REP_1(i, n) for (int i = 1; i <= n; i++) 28 #define FORI(i, a, b) for (int i = a; i < b; i++) 29 #define FORD(i, a, b) for (int i = a; i > b; i--) 30 #define _clr(x) memset(x, 0, sizeof(x)) 31 #define _rst(x) memset(x, -1, sizeof(x)) 32 33 typedef long long LL; 34 typedef pair<int, int> PII; 35 typedef vector<LL> VLL; 36 typedef vector<PII> VPII; 37 typedef vector<int> VI; 38 typedef vector<double> VDBL; 39 const int N = 2505; 40 const int hashMod = 1e6 + 5; 41 const int inf = 0x55555555; 42 const double eps = 1e-8; 43 const LL linf = 0x5555555555555555ll; 44 const double finf = 1e50; 45 const double pi = acos(-1.0); 46 const int mod = 1e9 + 7; 47 48 FILE *fin = fopen("rect1.in", "r"); 49 FILE *fout = fopen("rect1.out", "w"); 50 51 struct Rect { 52 int x1, y1, x2, y2, cl; 53 Rect(int a = 0, int b = 0, int c = 0, int d = 0, int e = 0) { 54 if (a > c) swap(a, c); 55 if (b > d) swap(b, d); 56 x1 = a, y1 = b, x2 = c, y2 = d, cl = e; 57 } 58 } ; 59 60 queue<Rect> Q; 61 int clr[N]; 62 63 bool belong(int x1, int y1, int x2, int y2, Rect &x) { 64 return x.x1 <= x1 && x2 <= x.x2 && x.y1 <= y1 && y2 <= x.y2; 65 } 66 67 void insert(Rect x) { 68 int sz = Q.size(); 69 int X[5], Y[5]; 70 while (sz--) { 71 Rect tmp = Q.front(); 72 // printf("!!! %d %d %d %d %d\n", tmp.x1, tmp.y1, tmp.x2, tmp.y2, tmp.cl); 73 Q.pop(); 74 if (tmp.x2 <= x.x1 || x.x2 <= tmp.x1) { Q.push(tmp); continue;} 75 if (tmp.y2 <= x.y1 || x.y2 <= tmp.y1) { Q.push(tmp); continue;} 76 // puts("~~~~~~~~~~~~~~~"); 77 // printf("rect %d %d %d %d %d\n", tmp.x1, tmp.y1, tmp.x2, tmp.y2, tmp.cl); 78 X[0] = x.x1, X[1] = x.x2; 79 Y[0] = x.y1, Y[1] = x.y2; 80 X[2] = tmp.x1, X[3] = tmp.x2; 81 Y[2] = tmp.y1, Y[3] = tmp.y2; 82 sort(X, X + 4); 83 sort(Y, Y + 4); 84 // REP(i, 4) { 85 // printf("x %d y %d\n", X[i], Y[i]); 86 // } 87 REP(i, 3) { 88 if (X[i] == X[i + 1]) continue; 89 REP(j, 3) { 90 if (Y[j] == Y[j + 1]) continue; 91 if (belong(X[i], Y[j], X[i + 1], Y[j + 1], tmp) && !belong(X[i], Y[j], X[i + 1], Y[j + 1], x)) { 92 Q.push(Rect(X[i], Y[j], X[i + 1], Y[j + 1], tmp.cl)); 93 // printf("pb %d %d %d %d %d\n", X[i], Y[j], X[i + 1], Y[j + 1], tmp.cl); 94 } 95 } 96 } 97 // puts("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); 98 } 99 Q.push(x); 100 } 101 102 int main() { 103 int a, b, c, d, e, n; 104 // freopen("in", "r", stdin); 105 while (~fscanf(fin, "%d%d%d", &a, &b, &n)) { 106 while (Q.size()) Q.pop(); 107 _clr(clr); 108 Q.push(Rect(0, 0, a, b, 1)); 109 while (n--) { 110 fscanf(fin, "%d%d%d%d%d", &a, &b, &c, &d, &e); 111 // printf("Rect ~~~~~~~~~~~~~~~~~~ %d %d %d %d %d\n", a, b, c, d, e); 112 insert(Rect(a, b, c, d, e)); 113 } 114 while (Q.size()) { 115 Rect tmp = Q.front(); 116 // printf("%d %d %d %d %d\n", tmp.x1, tmp.y1, tmp.x2, tmp.y2, tmp.cl); 117 Q.pop(); 118 clr[tmp.cl] += (tmp.x2 - tmp.x1) * (tmp.y2 - tmp.y1); 119 } 120 REP(i, N) { 121 if (clr[i]) { 122 fprintf(fout, "%d %d\n", i, clr[i]); 123 } 124 } 125 } 126 return 0; 127 }
——written by Lyon