#include <cstdio> #include <iostream> #include <set> #include <algorithm> #include <iomanip> #include <cassert> #define REP(i, n) for(int i = 0; i < n; i++) #define FWD(i, a, b) for(int i = a; i < b; i++) #define ALL(u) (u).begin(), (u).end() using namespace std; typedef pair<int,int> PII; typedef long long LL; typedef uint uint; const int INF = 1000000000; class Sol { public: int n; int w; vector<int> height; void extract_right(vector<pair<int,PII>>& in, vector <PII>& out) { for (auto a : in) { out.push_back(a.second); } } vector<PII> read_order() { vector<PII> res; vector<pair<int,PII>> order; REP(i, n) { int x1, y1, x2, y2; scanf("%d %d %d %d", &x1, &y1, &x2, &y2); if (x1 > x2) swap(x1, x2); if (y1 > y2) swap(y1, y2); order.push_back(make_pair(x2, make_pair(i, (y2 - y1)))); } sort(ALL(order)); extract_right(order, res); /* printf("order:\n"); for(int i = 0; i < res.size(); i++) { printf("(%d %d) ", res[i].first, res[i].second); } printf("\n"); */ return res; } vector<int> gen_lookup(vector<PII>& ord) { vector<int> lookup(ord.size()); for (int i = 0; i < (int)ord.size(); i++) { lookup[ord[i].first] = i; } return lookup; } void reorder(vector<int>& lookup, vector<PII>& ord) { for (int i = 0; i < (int)ord.size(); i++) { ord[i].first = lookup[ord[i].first]; } } bool merge_sort(vector<PII>& in) { /* printf("in:\n"); for(int i = 0; i < in.size(); i++) { printf("(%d %d) ", in[i].first, in[i].second); } printf("\n"); */ if (in.size() == 1) { return true; } vector<PII> low(((int)in.size()+1)/2), high(((int)in.size()/2)); REP(i, ((int)in.size()+1)/2) { low[i] = in[i]; } REP(i, ((int)in.size())/2) { high[i] = in[i + low.size()]; } if (not merge_sort(low)) return false; if (not merge_sort(high)) return false; vector<int> suf_max(((int)in.size()+1)/2 + 1); suf_max.back() = 0; for(int i = (int)low.size() - 1; i >= 0; i--) { suf_max[i] = max(suf_max[i+1], low[i].second); } int low_pos = 0, high_pos = 0; /* printf("merging:\n"); for(int i = 0; i < low.size(); i++) { printf("(%d %d) ", low[i].first, low[i].second); } printf("\n"); for(int i = 0; i < high.size(); i++) { printf("(%d %d) ", high[i].first, high[i].second); } printf("\n"); */ for(int i = 0; i < (int)in.size(); i++) { if (high_pos == (int)high.size() || (low_pos < (int)low.size() and low[low_pos].first < high[high_pos].first)) { in[i] = low[low_pos]; low_pos++; } else { if (w < suf_max[low_pos] + high[high_pos].second) { return false; } in[i] = high[high_pos]; high_pos++; } } /* printf("out:\n"); for(int i = 0; i < in.size(); i++) { printf("(%d %d) ", in[i].first, in[i].second); } printf("\n"); */ return true; } void sol() { scanf("%d %d", &n, &w); height.resize(n); vector<PII> orig = read_order(); vector<PII> dst = read_order(); vector<int> lookup = gen_lookup(dst); reorder(lookup, orig); if (merge_sort(orig)) { printf("TAK\n"); assert(is_sorted(ALL(orig))); } else printf("NIE\n"); } }; int main() { int t; scanf("%d", &t); REP(i, t) { Sol s; s.sol(); } 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 146 147 148 149 150 151 | #include <cstdio> #include <iostream> #include <set> #include <algorithm> #include <iomanip> #include <cassert> #define REP(i, n) for(int i = 0; i < n; i++) #define FWD(i, a, b) for(int i = a; i < b; i++) #define ALL(u) (u).begin(), (u).end() using namespace std; typedef pair<int,int> PII; typedef long long LL; typedef uint uint; const int INF = 1000000000; class Sol { public: int n; int w; vector<int> height; void extract_right(vector<pair<int,PII>>& in, vector <PII>& out) { for (auto a : in) { out.push_back(a.second); } } vector<PII> read_order() { vector<PII> res; vector<pair<int,PII>> order; REP(i, n) { int x1, y1, x2, y2; scanf("%d %d %d %d", &x1, &y1, &x2, &y2); if (x1 > x2) swap(x1, x2); if (y1 > y2) swap(y1, y2); order.push_back(make_pair(x2, make_pair(i, (y2 - y1)))); } sort(ALL(order)); extract_right(order, res); /* printf("order:\n"); for(int i = 0; i < res.size(); i++) { printf("(%d %d) ", res[i].first, res[i].second); } printf("\n"); */ return res; } vector<int> gen_lookup(vector<PII>& ord) { vector<int> lookup(ord.size()); for (int i = 0; i < (int)ord.size(); i++) { lookup[ord[i].first] = i; } return lookup; } void reorder(vector<int>& lookup, vector<PII>& ord) { for (int i = 0; i < (int)ord.size(); i++) { ord[i].first = lookup[ord[i].first]; } } bool merge_sort(vector<PII>& in) { /* printf("in:\n"); for(int i = 0; i < in.size(); i++) { printf("(%d %d) ", in[i].first, in[i].second); } printf("\n"); */ if (in.size() == 1) { return true; } vector<PII> low(((int)in.size()+1)/2), high(((int)in.size()/2)); REP(i, ((int)in.size()+1)/2) { low[i] = in[i]; } REP(i, ((int)in.size())/2) { high[i] = in[i + low.size()]; } if (not merge_sort(low)) return false; if (not merge_sort(high)) return false; vector<int> suf_max(((int)in.size()+1)/2 + 1); suf_max.back() = 0; for(int i = (int)low.size() - 1; i >= 0; i--) { suf_max[i] = max(suf_max[i+1], low[i].second); } int low_pos = 0, high_pos = 0; /* printf("merging:\n"); for(int i = 0; i < low.size(); i++) { printf("(%d %d) ", low[i].first, low[i].second); } printf("\n"); for(int i = 0; i < high.size(); i++) { printf("(%d %d) ", high[i].first, high[i].second); } printf("\n"); */ for(int i = 0; i < (int)in.size(); i++) { if (high_pos == (int)high.size() || (low_pos < (int)low.size() and low[low_pos].first < high[high_pos].first)) { in[i] = low[low_pos]; low_pos++; } else { if (w < suf_max[low_pos] + high[high_pos].second) { return false; } in[i] = high[high_pos]; high_pos++; } } /* printf("out:\n"); for(int i = 0; i < in.size(); i++) { printf("(%d %d) ", in[i].first, in[i].second); } printf("\n"); */ return true; } void sol() { scanf("%d %d", &n, &w); height.resize(n); vector<PII> orig = read_order(); vector<PII> dst = read_order(); vector<int> lookup = gen_lookup(dst); reorder(lookup, orig); if (merge_sort(orig)) { printf("TAK\n"); assert(is_sorted(ALL(orig))); } else printf("NIE\n"); } }; int main() { int t; scanf("%d", &t); REP(i, t) { Sol s; s.sol(); } return 0; } |