// Nikodem "nixon" Kramarz // zad. "Parking" PA 2014 #include <iostream> #include <algorithm> #define MAX_N 50009 #define M (1 << 17) using namespace std; struct max_car { int x, y, ind; }; int n, t, w, tree[M * 2 + 5], maximum[MAX_N], akt_wys[MAX_N]; max_car max_cars[MAX_N], cars[MAX_N]; bool cmp (max_car a, max_car b) { if(a.x != b.x) return a.x < b.x; else if(a.y != b.y) return a.y < b.y; return a.ind < b.ind; } bool unsort (max_car a, max_car b) { return a.ind < b.ind; } void insert(int x, int c) { int v = M + x; tree[v] = max(tree[v], c); while(v != 1) { v /= 2; tree[v] = max(tree[2*v], tree[2*v+1]); } } int query(int a, int b) { int va = M + a, vb = M + b, res = tree[va]; if(va != vb) res = max(res, tree[vb]); while(va / 2 != vb / 2) { if(va % 2 == 0) res = max(res, tree[va+1]); if(vb % 2 == 1) res = max(res, tree[vb-1]); va /= 2; vb /= 2; } return res; } int main() { ios_base::sync_with_stdio(0); cin >> t; while(t--) { cin >> n >> w; int a, b, c, d; for(int i=0; i<n; i++) { cin >> a >> b >> c >> d; max_cars[i].x = min(a, c); max_cars[i].y = abs(b - d); max_cars[i].ind = i; } for(int i=0; i<n; i++) { cin >> a >> b >> c >> d; cars[i].x = min(a, c); cars[i].y = abs(b - d); cars[i].ind = i; } sort(max_cars, max_cars + n, cmp); sort(cars, cars + n, cmp); int counter = 1, counter2 = 1, last = max_cars[0].x, last2 = cars[0].x; max_cars[0].x = 1; cars[0].x = 1; for(int i=1; i<n; i++) { if(max_cars[i].x != last) counter++; if(cars[i].x != last2) counter2++; last = max_cars[i].x; last2 = cars[i].x; max_cars[i].x = counter; cars[i].x = counter2; } sort(max_cars, max_cars + n, unsort); sort(cars, cars + n, unsort); for(int i=0; i<n; i++) maximum[max_cars[i].x] = max(maximum[max_cars[i].x], max_cars[i].y); for(int i=1; i<=n; i++) insert(i, maximum[i]); bool p=0; int pocz, kon, m; for(int i=0; i<n; i++) { pocz = min(max_cars[i].x, cars[i].x); kon = max(max_cars[i].x, cars[i].x); if(pocz != kon) { m = query(pocz, kon); if(w - m < cars[i].y) { p = 1; break; } } akt_wys[cars[i].ind] += cars[i].y; } for(int i=1; i<=n; i++) if(akt_wys[i] > w) { p = 1; break; } if(p) cout << "NIE\n"; else cout << "TAK\n"; for(int i=1; i<=n; i++) akt_wys[i] = maximum[i] = 0; for(int i=0; i<=2*M; i++) tree[i] = 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 | // Nikodem "nixon" Kramarz // zad. "Parking" PA 2014 #include <iostream> #include <algorithm> #define MAX_N 50009 #define M (1 << 17) using namespace std; struct max_car { int x, y, ind; }; int n, t, w, tree[M * 2 + 5], maximum[MAX_N], akt_wys[MAX_N]; max_car max_cars[MAX_N], cars[MAX_N]; bool cmp (max_car a, max_car b) { if(a.x != b.x) return a.x < b.x; else if(a.y != b.y) return a.y < b.y; return a.ind < b.ind; } bool unsort (max_car a, max_car b) { return a.ind < b.ind; } void insert(int x, int c) { int v = M + x; tree[v] = max(tree[v], c); while(v != 1) { v /= 2; tree[v] = max(tree[2*v], tree[2*v+1]); } } int query(int a, int b) { int va = M + a, vb = M + b, res = tree[va]; if(va != vb) res = max(res, tree[vb]); while(va / 2 != vb / 2) { if(va % 2 == 0) res = max(res, tree[va+1]); if(vb % 2 == 1) res = max(res, tree[vb-1]); va /= 2; vb /= 2; } return res; } int main() { ios_base::sync_with_stdio(0); cin >> t; while(t--) { cin >> n >> w; int a, b, c, d; for(int i=0; i<n; i++) { cin >> a >> b >> c >> d; max_cars[i].x = min(a, c); max_cars[i].y = abs(b - d); max_cars[i].ind = i; } for(int i=0; i<n; i++) { cin >> a >> b >> c >> d; cars[i].x = min(a, c); cars[i].y = abs(b - d); cars[i].ind = i; } sort(max_cars, max_cars + n, cmp); sort(cars, cars + n, cmp); int counter = 1, counter2 = 1, last = max_cars[0].x, last2 = cars[0].x; max_cars[0].x = 1; cars[0].x = 1; for(int i=1; i<n; i++) { if(max_cars[i].x != last) counter++; if(cars[i].x != last2) counter2++; last = max_cars[i].x; last2 = cars[i].x; max_cars[i].x = counter; cars[i].x = counter2; } sort(max_cars, max_cars + n, unsort); sort(cars, cars + n, unsort); for(int i=0; i<n; i++) maximum[max_cars[i].x] = max(maximum[max_cars[i].x], max_cars[i].y); for(int i=1; i<=n; i++) insert(i, maximum[i]); bool p=0; int pocz, kon, m; for(int i=0; i<n; i++) { pocz = min(max_cars[i].x, cars[i].x); kon = max(max_cars[i].x, cars[i].x); if(pocz != kon) { m = query(pocz, kon); if(w - m < cars[i].y) { p = 1; break; } } akt_wys[cars[i].ind] += cars[i].y; } for(int i=1; i<=n; i++) if(akt_wys[i] > w) { p = 1; break; } if(p) cout << "NIE\n"; else cout << "TAK\n"; for(int i=1; i<=n; i++) akt_wys[i] = maximum[i] = 0; for(int i=0; i<=2*M; i++) tree[i] = 0; } } |