#include <cstdio> #include <algorithm> using namespace std; #define MAX_N 50000 struct Car { int i,x,h; bool operator < (Car c) const { return x<c.x; } }; int D[MAX_N*5]; struct Tree { inline int parent(int i) { return (i-1)/2; } inline int left(int i) { return i*2+1; } inline int right(int i) { return i*2+2; } int start,size; Tree(int n,Car *cars) { size=1; while(size<n)size<<=1; start=size-1; size<<=1; --size; for(int a=0;a<n;++a) { D[a+start]=cars[a].h; } for(int a=n+start;a<size;++a)D[a]=0; for(int a=start-1;a>=0;--a) { calculate(a); } } void calculate(int i) { D[i]=max(D[left(i)],D[right(i)]); } void edit(int i,int v) { i+=start; D[i]=v; while(i!=0) { i=parent(i); calculate(i); } } int maxInPref(int i) { int ret=0; i+=start; if(i>=size)return D[0]; while(i!=0) { if(i==right(parent(i))) { ret=max(ret,D[left(parent(i))]); } i=parent(i); } return ret; } }; int n,h; Car in[MAX_N]; Car out[MAX_N]; int I[MAX_N]; int main() { int t; scanf("%d",&t); NEXT: while(t--) { scanf("%d%d",&n,&h); for(int a=0;a<n;++a) { int x1,y1,x2,y2; scanf("%d%d%d%d",&x1,&y1,&x2,&y2); in[a].x=min(x1,x2); in[a].h=abs(y2-y1); in[a].i=a; } for(int a=0;a<n;++a) { int x1,y1,x2,y2; scanf("%d%d%d%d",&x1,&y1,&x2,&y2); out[a].x=min(x1,x2); out[a].h=abs(y2-y1); out[a].i=a; } sort(in,in+n); for(int a=0;a<n;++a)I[in[a].i]=a; sort(out,out+n); Tree tree(n,in); for(int a=0;a<n;++a) { int i=I[out[a].i]; if(in[i].h+tree.maxInPref(i)<=h) { tree.edit(i,0); }else { printf("NIE\n"); goto NEXT; } } printf("TAK\n"); } 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 | #include <cstdio> #include <algorithm> using namespace std; #define MAX_N 50000 struct Car { int i,x,h; bool operator < (Car c) const { return x<c.x; } }; int D[MAX_N*5]; struct Tree { inline int parent(int i) { return (i-1)/2; } inline int left(int i) { return i*2+1; } inline int right(int i) { return i*2+2; } int start,size; Tree(int n,Car *cars) { size=1; while(size<n)size<<=1; start=size-1; size<<=1; --size; for(int a=0;a<n;++a) { D[a+start]=cars[a].h; } for(int a=n+start;a<size;++a)D[a]=0; for(int a=start-1;a>=0;--a) { calculate(a); } } void calculate(int i) { D[i]=max(D[left(i)],D[right(i)]); } void edit(int i,int v) { i+=start; D[i]=v; while(i!=0) { i=parent(i); calculate(i); } } int maxInPref(int i) { int ret=0; i+=start; if(i>=size)return D[0]; while(i!=0) { if(i==right(parent(i))) { ret=max(ret,D[left(parent(i))]); } i=parent(i); } return ret; } }; int n,h; Car in[MAX_N]; Car out[MAX_N]; int I[MAX_N]; int main() { int t; scanf("%d",&t); NEXT: while(t--) { scanf("%d%d",&n,&h); for(int a=0;a<n;++a) { int x1,y1,x2,y2; scanf("%d%d%d%d",&x1,&y1,&x2,&y2); in[a].x=min(x1,x2); in[a].h=abs(y2-y1); in[a].i=a; } for(int a=0;a<n;++a) { int x1,y1,x2,y2; scanf("%d%d%d%d",&x1,&y1,&x2,&y2); out[a].x=min(x1,x2); out[a].h=abs(y2-y1); out[a].i=a; } sort(in,in+n); for(int a=0;a<n;++a)I[in[a].i]=a; sort(out,out+n); Tree tree(n,in); for(int a=0;a<n;++a) { int i=I[out[a].i]; if(in[i].h+tree.maxInPref(i)<=h) { tree.edit(i,0); }else { printf("NIE\n"); goto NEXT; } } printf("TAK\n"); } return 0; } |