import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.List; public class par { static int n, w; static Car[] tmp = new Car[50000]; public static void main(String[] args) { int t = R.i(); Car[] srcCars = new Car[50000]; Car[] dstCars = new Car[50000]; while (t-- > 0) { n = R.i(); w = R.i(); loadAndArrange(srcCars); loadAndArrange(dstCars); boolean ok = true; int i = 0; while (ok && i < n) { ok = srcCars[i].nr == dstCars[i].nr; i++; } W.tn(ok); } W.flush(); } static void loadAndArrange(Car[] cars) { // load int x1, x2, y1, y2; Car c; for (int i = 0; i < n; i++) { x1 = R.i(); y1 = R.i(); x2 = R.i(); y2 = R.i(); c = new Car(); c.nr = i + 1; c.x = Math.min(x1, x2); c.h = Math.abs(y2 - y1); cars[i] = c; } // sort by x Arrays.sort(cars, 0, n, new Comparator<Car>() { @Override public int compare(Car o1, Car o2) { return o1.x - o2.x; } }); // arrange arrange(cars, 0, n - 1); } // returns biggest from the range static Car arrange(Car[] cars, int start, int end) { if (start == end) { return cars[start]; } int middle = (start + end) / 2; Car leftBiggest = arrange(cars, start, middle); Car rightBiggest = arrange(cars, middle + 1, end); // merge sorted linked lists Car lc = leftBiggest; Car rc = rightBiggest; Car biggest = leftBiggest.h > rightBiggest.h ? leftBiggest : rightBiggest; Car last = null; while (lc != null || rc != null) { if (lc == null || (rc != null && rc.h >= lc.h)) { rc.tbigger = last; if (last != null) { last.tsmaller = rc; } last = rc; rc = rc.smaller; } else { lc.tbigger = last; if (last != null) { last.tsmaller = lc; } last = lc; lc = lc.smaller; } } // merge arrangements int l = start; int r = middle + 1; int i = 0; Car c; while (l <= middle && r <= end) { if (cars[l].h < cars[r].h || (cars[l].h == cars[r].h && cars[l].nr < cars[r].nr) || cars[r].h + leftBiggest.h > w) { c = cars[l++]; if (c.smaller != null) { c.smaller.bigger = c.bigger; } if (c.bigger != null) { c.bigger.smaller = c.smaller; } if (c == leftBiggest) { leftBiggest = c.smaller; } tmp[i++] = c; } else { tmp[i++] = cars[r++]; } } while (l <= middle) { tmp[i++] = cars[l++]; } while (r <= end) { tmp[i++] = cars[r++]; } // update sorted linked list while (i-- > 0) { c = tmp[i]; c.bigger = c.tbigger; c.smaller = c.tsmaller; cars[start + i] = c; } return biggest; } static class Car { int nr; int x; int h; Car bigger, smaller, tbigger, tsmaller; } static class R { static InputStream is=new BufferedInputStream(System.in); static int b='\n'; static void next(){try{b=is.read();}catch(Exception e){}} static void skip(){while(b=='\n'||b==' '||b=='\r'||b=='\t')next();} static char c(){skip();char c=(char)b;next();return c;} static int i(){skip();int i=0;while(b>='0'&&b<='9'){i=10*i+(b-'0');next();}return i;} static long l(){skip();long i=0;while(b>='0'&&b<='9'){i=10*i+(b-'0');next();}return i;} } static class W { static OutputStream os=new BufferedOutputStream(System.out,4096); static void b(int b){try{os.write(b);}catch(Exception e){}} static void c(char c){b(c);} static void cs(char c){c(c);space();} static void cn(char c){c(c);newLine();} static void i(int i){if(i>9)i(i/10);b('0'+(i%10));} static void is(int i){i(i);space();} static void in(int i){i(i);newLine();} static void l(long l){if(l>9)l(l/10);b('0'+(int)(l%10));} static void ls(int l){l(l);space();} static void ln(int l){l(l);newLine();} static void s(String s){try{os.write(s.getBytes());}catch(Exception e){}} static void ss(String s){s(s);space();} static void sn(String s){s(s);newLine();} static void tn(boolean b){sn(b?"TAK":"NIE");} static void space(){b(' ');} static void newLine(){b('\n');} static void flush(){try{os.flush();}catch(Exception e){}}; } static class T { static List<String> s=new ArrayList<String>(); static List<Long> t=new ArrayList<Long>(); static void t(String n){s.add(n);t.add(System.currentTimeMillis());} static void err(){t.add(System.currentTimeMillis());System.err.println();for(int i=0;i<s.size();i++){System.err.println(s.get(i)+": "+(t.get(i+1)-t.get(i)));}System.err.println("TOTAL: "+(t.get(t.size() - 1)-t.get(0)));System.err.flush();} } }
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 | import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.List; public class par { static int n, w; static Car[] tmp = new Car[50000]; public static void main(String[] args) { int t = R.i(); Car[] srcCars = new Car[50000]; Car[] dstCars = new Car[50000]; while (t-- > 0) { n = R.i(); w = R.i(); loadAndArrange(srcCars); loadAndArrange(dstCars); boolean ok = true; int i = 0; while (ok && i < n) { ok = srcCars[i].nr == dstCars[i].nr; i++; } W.tn(ok); } W.flush(); } static void loadAndArrange(Car[] cars) { // load int x1, x2, y1, y2; Car c; for (int i = 0; i < n; i++) { x1 = R.i(); y1 = R.i(); x2 = R.i(); y2 = R.i(); c = new Car(); c.nr = i + 1; c.x = Math.min(x1, x2); c.h = Math.abs(y2 - y1); cars[i] = c; } // sort by x Arrays.sort(cars, 0, n, new Comparator<Car>() { @Override public int compare(Car o1, Car o2) { return o1.x - o2.x; } }); // arrange arrange(cars, 0, n - 1); } // returns biggest from the range static Car arrange(Car[] cars, int start, int end) { if (start == end) { return cars[start]; } int middle = (start + end) / 2; Car leftBiggest = arrange(cars, start, middle); Car rightBiggest = arrange(cars, middle + 1, end); // merge sorted linked lists Car lc = leftBiggest; Car rc = rightBiggest; Car biggest = leftBiggest.h > rightBiggest.h ? leftBiggest : rightBiggest; Car last = null; while (lc != null || rc != null) { if (lc == null || (rc != null && rc.h >= lc.h)) { rc.tbigger = last; if (last != null) { last.tsmaller = rc; } last = rc; rc = rc.smaller; } else { lc.tbigger = last; if (last != null) { last.tsmaller = lc; } last = lc; lc = lc.smaller; } } // merge arrangements int l = start; int r = middle + 1; int i = 0; Car c; while (l <= middle && r <= end) { if (cars[l].h < cars[r].h || (cars[l].h == cars[r].h && cars[l].nr < cars[r].nr) || cars[r].h + leftBiggest.h > w) { c = cars[l++]; if (c.smaller != null) { c.smaller.bigger = c.bigger; } if (c.bigger != null) { c.bigger.smaller = c.smaller; } if (c == leftBiggest) { leftBiggest = c.smaller; } tmp[i++] = c; } else { tmp[i++] = cars[r++]; } } while (l <= middle) { tmp[i++] = cars[l++]; } while (r <= end) { tmp[i++] = cars[r++]; } // update sorted linked list while (i-- > 0) { c = tmp[i]; c.bigger = c.tbigger; c.smaller = c.tsmaller; cars[start + i] = c; } return biggest; } static class Car { int nr; int x; int h; Car bigger, smaller, tbigger, tsmaller; } static class R { static InputStream is=new BufferedInputStream(System.in); static int b='\n'; static void next(){try{b=is.read();}catch(Exception e){}} static void skip(){while(b=='\n'||b==' '||b=='\r'||b=='\t')next();} static char c(){skip();char c=(char)b;next();return c;} static int i(){skip();int i=0;while(b>='0'&&b<='9'){i=10*i+(b-'0');next();}return i;} static long l(){skip();long i=0;while(b>='0'&&b<='9'){i=10*i+(b-'0');next();}return i;} } static class W { static OutputStream os=new BufferedOutputStream(System.out,4096); static void b(int b){try{os.write(b);}catch(Exception e){}} static void c(char c){b(c);} static void cs(char c){c(c);space();} static void cn(char c){c(c);newLine();} static void i(int i){if(i>9)i(i/10);b('0'+(i%10));} static void is(int i){i(i);space();} static void in(int i){i(i);newLine();} static void l(long l){if(l>9)l(l/10);b('0'+(int)(l%10));} static void ls(int l){l(l);space();} static void ln(int l){l(l);newLine();} static void s(String s){try{os.write(s.getBytes());}catch(Exception e){}} static void ss(String s){s(s);space();} static void sn(String s){s(s);newLine();} static void tn(boolean b){sn(b?"TAK":"NIE");} static void space(){b(' ');} static void newLine(){b('\n');} static void flush(){try{os.flush();}catch(Exception e){}}; } static class T { static List<String> s=new ArrayList<String>(); static List<Long> t=new ArrayList<Long>(); static void t(String n){s.add(n);t.add(System.currentTimeMillis());} static void err(){t.add(System.currentTimeMillis());System.err.println();for(int i=0;i<s.size();i++){System.err.println(s.get(i)+": "+(t.get(i+1)-t.get(i)));}System.err.println("TOTAL: "+(t.get(t.size() - 1)-t.get(0)));System.err.flush();} } } |