#include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define MAXN 65536 #define VDEBUG(...) #define DEBUG(...) //#define DEBUG(...) printf(__VA_ARGS__); fflush(stdout) //#define VDEBUG(...) printf(__VA_ARGS__); fflush(stdout) struct samochod_geo { int id, w, x; }; struct samochod { int id, w, pos1, pos2; }; bool sgeo_cmp(const samochod_geo &a, const samochod_geo &b) { return a.x < b.x; } bool sam_cmp(const samochod &a, const samochod &b) { return a.pos2 < b.pos2; } int n, w; samochod_geo sam_pos1[MAXN], sam_pos2[MAXN]; samochod sam[MAXN]; void init() { int i; int x1, x2, y1, y2; scanf("%d %d", &n, &w); DEBUG("n=%d w=%d\n", n, w); for (i = 0; i < n; i++) { scanf("%d %d %d %d", &x1, &y1, &x2, &y2); sam_pos1[i].id = i; sam_pos1[i].x = x1 < x2 ? x1 : x2; // chyba nie ma znaczenia... sam_pos1[i].w = abs(y1-y2); } for (i = 0; i < n; i++) { scanf("%d %d %d %d", &x1, &y1, &x2, &y2); sam_pos2[i].id = i; sam_pos2[i].x = x1 < x2 ? x1 : x2; // chyba nie ma znaczenia... sam_pos2[i].w = abs(y1-y2); } sort(sam_pos1, sam_pos1 + n, sgeo_cmp); sort(sam_pos2, sam_pos2 + n, sgeo_cmp); for (i = 0; i < n; i++) { sam[sam_pos1[i].id].pos1 = i; sam[sam_pos1[i].id].w = sam_pos1[i].w; sam[sam_pos1[i].id].id = sam_pos1[i].id; sam[sam_pos2[i].id].pos2 = i; sam[sam_pos2[i].id].w = sam_pos2[i].w; sam[sam_pos2[i].id].id = sam_pos2[i].id; } sort(sam, sam + n, sam_cmp); } #define PARENT(i) ((i-1)/2) #define LEFT(i) ((i) * 2 + 1) #define RIGHT(i) ((i) * 2 + 2) #define MAX(a,b) ((a > b) ? (a) : (b)) #define MIN(a,b) ((a < b) ? (a) : (b)) int rbase; int maxw[2*MAXN]; void init_rangetree() { int i; for (rbase = 1; rbase <= n; rbase *= 2); memset(maxw, 0, (2*rbase-1) * sizeof(int)); rbase--; for (i = 0; i < n; i++) { maxw[rbase + sam[i].pos1] = sam[i].w; } VDEBUG("start_range:"); for (i = 0; i < n; i++) { VDEBUG(" %d", maxw[rbase + i]); } VDEBUG("\n"); for (i = rbase - 1; i >= 0; i--) { maxw[i] = MAX(maxw[LEFT(i)], maxw[RIGHT(i)]); } } void range_set(int pos, int val) { int i; maxw[rbase + pos] = val; VDEBUG("maxw[%d] = %d\n", rbase + pos, maxw[rbase + pos]); for (i = PARENT(rbase + pos); PARENT(i) != i; i = PARENT(i)) { maxw[i] = MAX(maxw[LEFT(i)], maxw[RIGHT(i)]); VDEBUG("maxw[%d]=%d maxw[%d]=%d maxw[%d]=%d\n", i, maxw[i], LEFT(i), maxw[LEFT(i)], RIGHT(i), maxw[RIGHT(i)]); } } int _range_max(int a, int b, int i, int ra, int rb) { int maxl, maxr; if (a > rb || b < ra) return 0; if (ra >= a && rb <= b) { VDEBUG("[%d %d] -> %d\n", ra, rb, maxw[i]); return maxw[i]; } maxl = _range_max(a, b, LEFT(i), ra, (ra + rb)/2); maxr = _range_max(a, b, RIGHT(i), (ra + rb)/2 + 1, rb); VDEBUG("[%d %d] -> MAX(%d, %d) = %d\n", ra, rb, maxl, maxr, MAX(maxl, maxr)); return MAX(maxl, maxr); } int range_max(int a, int b) { return _range_max(a, b, 0, 0, rbase); } void run() { int i; init(); init_rangetree(); for (i = 0; i < n; i++) { DEBUG("id = %d pos1 = %d pos2 = %d w = %d\n", sam[i].id, sam[i].pos1, sam[i].pos2, sam[i].w); if (sam[i].pos1 != 0) { int rmax = range_max(0, sam[i].pos1 - 1); DEBUG("rmax = %d\n", rmax); if (rmax + sam[i].w > w) { printf("NIE\n"); return; } } range_set(sam[i].pos1, 0); } printf("TAK\n"); } int main() { int i, t; scanf("%d", &t); for(i = 0; i < t; i++) run(); 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 | #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define MAXN 65536 #define VDEBUG(...) #define DEBUG(...) //#define DEBUG(...) printf(__VA_ARGS__); fflush(stdout) //#define VDEBUG(...) printf(__VA_ARGS__); fflush(stdout) struct samochod_geo { int id, w, x; }; struct samochod { int id, w, pos1, pos2; }; bool sgeo_cmp(const samochod_geo &a, const samochod_geo &b) { return a.x < b.x; } bool sam_cmp(const samochod &a, const samochod &b) { return a.pos2 < b.pos2; } int n, w; samochod_geo sam_pos1[MAXN], sam_pos2[MAXN]; samochod sam[MAXN]; void init() { int i; int x1, x2, y1, y2; scanf("%d %d", &n, &w); DEBUG("n=%d w=%d\n", n, w); for (i = 0; i < n; i++) { scanf("%d %d %d %d", &x1, &y1, &x2, &y2); sam_pos1[i].id = i; sam_pos1[i].x = x1 < x2 ? x1 : x2; // chyba nie ma znaczenia... sam_pos1[i].w = abs(y1-y2); } for (i = 0; i < n; i++) { scanf("%d %d %d %d", &x1, &y1, &x2, &y2); sam_pos2[i].id = i; sam_pos2[i].x = x1 < x2 ? x1 : x2; // chyba nie ma znaczenia... sam_pos2[i].w = abs(y1-y2); } sort(sam_pos1, sam_pos1 + n, sgeo_cmp); sort(sam_pos2, sam_pos2 + n, sgeo_cmp); for (i = 0; i < n; i++) { sam[sam_pos1[i].id].pos1 = i; sam[sam_pos1[i].id].w = sam_pos1[i].w; sam[sam_pos1[i].id].id = sam_pos1[i].id; sam[sam_pos2[i].id].pos2 = i; sam[sam_pos2[i].id].w = sam_pos2[i].w; sam[sam_pos2[i].id].id = sam_pos2[i].id; } sort(sam, sam + n, sam_cmp); } #define PARENT(i) ((i-1)/2) #define LEFT(i) ((i) * 2 + 1) #define RIGHT(i) ((i) * 2 + 2) #define MAX(a,b) ((a > b) ? (a) : (b)) #define MIN(a,b) ((a < b) ? (a) : (b)) int rbase; int maxw[2*MAXN]; void init_rangetree() { int i; for (rbase = 1; rbase <= n; rbase *= 2); memset(maxw, 0, (2*rbase-1) * sizeof(int)); rbase--; for (i = 0; i < n; i++) { maxw[rbase + sam[i].pos1] = sam[i].w; } VDEBUG("start_range:"); for (i = 0; i < n; i++) { VDEBUG(" %d", maxw[rbase + i]); } VDEBUG("\n"); for (i = rbase - 1; i >= 0; i--) { maxw[i] = MAX(maxw[LEFT(i)], maxw[RIGHT(i)]); } } void range_set(int pos, int val) { int i; maxw[rbase + pos] = val; VDEBUG("maxw[%d] = %d\n", rbase + pos, maxw[rbase + pos]); for (i = PARENT(rbase + pos); PARENT(i) != i; i = PARENT(i)) { maxw[i] = MAX(maxw[LEFT(i)], maxw[RIGHT(i)]); VDEBUG("maxw[%d]=%d maxw[%d]=%d maxw[%d]=%d\n", i, maxw[i], LEFT(i), maxw[LEFT(i)], RIGHT(i), maxw[RIGHT(i)]); } } int _range_max(int a, int b, int i, int ra, int rb) { int maxl, maxr; if (a > rb || b < ra) return 0; if (ra >= a && rb <= b) { VDEBUG("[%d %d] -> %d\n", ra, rb, maxw[i]); return maxw[i]; } maxl = _range_max(a, b, LEFT(i), ra, (ra + rb)/2); maxr = _range_max(a, b, RIGHT(i), (ra + rb)/2 + 1, rb); VDEBUG("[%d %d] -> MAX(%d, %d) = %d\n", ra, rb, maxl, maxr, MAX(maxl, maxr)); return MAX(maxl, maxr); } int range_max(int a, int b) { return _range_max(a, b, 0, 0, rbase); } void run() { int i; init(); init_rangetree(); for (i = 0; i < n; i++) { DEBUG("id = %d pos1 = %d pos2 = %d w = %d\n", sam[i].id, sam[i].pos1, sam[i].pos2, sam[i].w); if (sam[i].pos1 != 0) { int rmax = range_max(0, sam[i].pos1 - 1); DEBUG("rmax = %d\n", rmax); if (rmax + sam[i].w > w) { printf("NIE\n"); return; } } range_set(sam[i].pos1, 0); } printf("TAK\n"); } int main() { int i, t; scanf("%d", &t); for(i = 0; i < t; i++) run(); return 0; } |