#include #include #include #include #include #define cin fin using namespace std; typedef pair duint; static int XN, YN, XT, YT; class shelf_t { private: int _x, _y, _l; int _x1, _x2; // NOTE: absolute coordinates public: shelf_t(void) { } shelf_t(int x, int y, int l, int x1, int x2) { _x = x; _y = y; _l = l; _x1 = x + x1; _x2 = x + x2; } private: duint compute_cost_m_sub(int x1, int x2, int x) { if(x >= x2) { int m = min((x - x1) * 2, min(x, _l)); return make_pair(0, (_l - m + 1) / 2); } if(x >= x1 && x >= 2) { int m = min(x, _l); return make_pair(1, (_l - m + 1) / 2); } return make_pair(2, _l / 2); } public: duint compute_cost_m(int x) { duint l = compute_cost_m_sub(_x1, _x2, x); duint r = compute_cost_m_sub(XN - _x2, XN - _x1, XN - (x + XT)); return min(l, r); } duint compute_cost_s(int x) { int l = max(_x1 - _l / 2, _x2 - _l); int r = min(_x2 + _l / 2, _x1 + _l); if(l <= x && x + XT <= r) return make_pair(0, 0); return make_pair(1, 0); } int get_l(void) { return _l; } int get_x(void) { return _x; } int get_xmin(void) { return max(_x1 - _l, 0); } int get_xmax(void) { return min(_x2 + _l, XN) - XT; } int get_y(void) { return _y; } public: bool operator <(const shelf_t &other) const { return _y < other._y; } }; duint& operator +=(duint &x, const duint &y) { x.first += y.first; x.second += y.second; return x; } void actual_main(ifstream &fin) { shelf_t s[100]; int N; fin >> XN >> YN >> XT >> YT >> N; if(!fin) return; XN *= 2; XT *= 2; for(int i = 0; i < N; i++) { int x, y, l, x1, x2; fin >> y >> x >> l >> x1 >> x2; s[i] = shelf_t(x * 2, y, l * 2, x1 * 2, x2 * 2); } sort(s, s + N); duint sol(1000, 0); for(int i = N - 1; i >= 0; i--) { if(s[i].get_y() > YN - YT) continue; if(s[i].get_l() < XT) continue; for(int x = s[i].get_xmin(); x <= s[i].get_xmax(); x++) { duint c = s[i].compute_cost_s(x); for(int j = i + 1; j < N; j++) { if(s[j].get_y() >= s[i].get_y() + YT || c >= sol) break; c += s[j].compute_cost_m(x); } if(c < sol) sol = c; } } cout << sol.first << " " << sol.second << endl; } int main(void) { ifstream fin("library.txt"); int n; fin >> n; for(int i = 0; i < n; i++) actual_main(fin); return 0; }