#include #include #include #include #include #include using namespace std; const int LIM = 800008; int N; int A[LIM], B[LIM]; void solve() { vector> E; set> S; // Input, while finding tallest grounded ladder. int ng = 0, mg = -1; cin >> N; for (int i = 0; i < N; i++) { cin >> A[i] >> B[i]; if (!A[i]) { ng++; if (mg < 0 || B[i] > B[mg]) { mg = i; } } else { E.emplace_back(A[i], 0, i); E.emplace_back(B[i], 1, i); } } // None grounded? if (ng == 0) { cout << "0 0" << endl; return; } // Sort endpoints (including tallest grounded ladder). E.emplace_back(B[mg], 1, mg); sort(E.begin(), E.end()); // Line sweep. int ans1 = ng, ans2 = 0; int a = mg, b = mg; S.clear(); for (auto e : E) { int i = get<2>(e); if (!get<1>(e)) { // Bottom of ladder i. S.emplace(B[i], i); continue; } // Top of ladder i. if (B[a] > B[b]) { swap(a, b); } auto it = S.find(make_pair(B[i], i)); if (it != S.end()) { // Ladder i not used yet, so jump from ladder a to it. S.erase(it); ans1++; ans2++; a = i; } else if (i == b) { // Consider ladder i to be ladder a rather than b. swap(a, b); } else if (i != a) { // Ladder i is irrelevant. continue; } // Top of ladder a, so need to jump off of it. if (!S.empty()) { // There are other unused ladders, so jump from ladder a to the one with // lowest top. bool isB = a == b; a = S.begin()->second; S.erase(make_pair(B[a], a)); ans1++; ans2++; if (isB) { // This is also ladder b, so repeat for it. if (!S.empty()) { // Jump from ladder b to another unused ladder. b = S.begin()->second; S.erase(make_pair(B[b], b)); ans1++; ans2++; } else { // Jump from ladder b to the new ladder a. ans2++; b = a; } } } else if (a != b) { // Jump from ladder a to ladder b. ans2++; a = b; } else { // Nowhere else to go, so stop. break; } } cout << ans1 << " " << ans2 << endl; } int main() { int T; cin >> T; for (int t = 1; t <= T; t++) { cout << "Case #" << t << ": "; solve(); } return 0; }