#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
template <class T, class... U> void bug_h(const T& t, const U&... u) { cerr << t; ((cerr << " | " << u), ...); cerr << endl; }
#define bug(...) cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]: ", bug_h(__VA_ARGS__)
#else
#define cerr if (0) cerr
#define bug(...)
#endif
#define ar array
#define all(v) std::begin(v), std::end(v)
#define sz(v) int(std::size(v))
typedef long long i64;
typedef vector<int> vi;
typedef pair<int, int> pi;
int N;
struct SCC {
vector<vi> g, rg;
vi order, id;
SCC(int n) : g(n), rg(n), id(n) {}
void make_edge(int i, int j) {
g.at(i).push_back(j);
rg.at(j).push_back(i);
}
void dfs1(int i) {
id[i] = 1;
for (int j : g[i]) if (!id[j]) dfs1(j);
order.push_back(i);
}
void dfs2(int i, int c) {
id[i] = c;
for (int j : rg[i]) if (id[j] == -1) dfs2(j, c);
}
void solve() {
for (int i = 0; i < sz(id); i++) if (!id[i]) dfs1(i);
reverse(all(order)), fill(all(id), -1);
int c = 0;
for (int i : order) if (id[i] == -1) dfs2(i, c++);
}
};
const int INF = 1e9;
int K;
vector<ar<int, 4>> rect;
vector<bool> covered;
vector<pi> solutions;
ar<int, 4> get_bounds() {
int x1 = INF, x2 = 1, y1 = INF, y2 = 1;
for (int i = 0; i < N; i++) if (!covered[i]) {
auto [l, d, r, u] = rect[i];
x1 = min(x1, r);
x2 = max(x2, l);
y1 = min(y1, u);
y2 = max(y2, d);
}
return {x1, x2, y1, y2};
}
void pr() {
for (auto [x, y] : solutions) cout << x << ' ' << y << '\n';
exit(0);
}
void rec(int k) {
if (!k) {
if (count(all(covered), 1) == N) pr();
return;
}
auto [x1, x2, y1, y2] = get_bounds();
for (auto x : {x1, x2}) for (auto y : {y1, y2}) {
solutions.push_back({x, y});
vector<int> add;
for (int i = 0; i < N; i++) if (!covered[i]) {
auto [l, d, r, u] = rect[i];
if (l <= x && x <= r && d <= y && y <= u) add.push_back(i);
}
for (int i : add) covered[i] = 1;
rec(k - 1);
for (int i : add) covered[i] = 0;
solutions.pop_back();
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> N >> K;
rect.resize(N);
for (auto &[l, d, r, u] : rect) cin >> l >> d >> r >> u;
covered.resize(N);
rec(K);
auto [x1, x2, y1, y2] = get_bounds();
vector<pi> edges;
int n = 0;
vector<ar<int, 3>> A, B, C, D;
vector<int> must;
for (int i = 0; i < N; i++) {
auto [l, d, r, u] = rect[i];
if (l <= x1 && x1 <= r && d <= y1 && y2 <= u) continue;
if (l <= x2 && x2 <= r && d <= y1 && y2 <= u) continue;
if (l <= x1 && x2 <= r && d <= y1 && y1 <= u) continue;
if (l <= x1 && x2 <= r && d <= y2 && y2 <= u) continue;
int cnt = 0;
int bm = 0;
if (d <= y1 && y1 <= u) {
assert(max(x1, l) <= min(x2, r));
A.push_back({max(x1, l), min(x2, r), i * 2 + cnt});
cnt++;
bm |= 1;
}
if (d <= y2 && y2 <= u) {
assert(max(x1, l) <= min(x2, r));
B.push_back({max(x1, l), min(x2, r), i * 2 + cnt});
cnt++;
bm |= 2;
}
if (l <= x1 && x1 <= r) {
assert(max(y1, d) <= min(y2, u));
C.push_back({max(y1, d), min(y2, u), i * 2 + cnt});
cnt++;
bm |= 4;
}
if (l <= x2 && x2 <= r) {
assert(max(y1, d) <= min(y2, u));
D.push_back({max(y1, d), min(y2, u), i * 2 + cnt});
cnt++;
bm |= 8;
}
// bug(bm);
if (cnt == 1) {
bug(i);
must.push_back(i * 2);
edges.push_back({i * 2 + 1, i * 2});
} else assert(cnt == 2);
}
vector<pi> ban;
auto process = [&](const vector<ar<int, 3>> v) {
auto one = v, two = v;
sort(all(one), [&](auto x, auto y) {
return x[1] < y[1];
});
sort(all(two), [&](auto x, auto y) {
return x[0] > y[0];
});
vi f(sz(v)), g(sz(v));
for (int i = 0; i < sz(v); i++) {
if (i == 0) {
f[i] = one[i][2] ^ 1;
g[i] = two[i][2] ^ 1;
} else {
f[i] = N * 2 + (n++);
g[i] = N * 2 + (n++);
edges.push_back({f[i], one[i][2] ^ 1});
edges.push_back({f[i], f[i - 1]});
edges.push_back({g[i], two[i][2] ^ 1});
edges.push_back({g[i], g[i - 1]});
}
}
for (auto [l, r, i] : v) {
if (one[0][1] < l) {
int low = 0, hi = sz(one) - 1;
while (low < hi) {
int m = (low + hi) / 2 + 1;
one[m][1] < l ? low = m : hi = m - 1;
}
edges.push_back({i, f[low]});
}
if (two[0][0] > r) {
int low = 0, hi = sz(two) - 1;
while (low < hi) {
int m = (low + hi) / 2 + 1;
two[m][0] > r ? low = m : hi = m - 1;
}
edges.push_back({i, g[low]});
}
}
};
process(A);
process(B);
process(C);
process(D);
SCC S(N * 2 + n);
for (auto [i, j] : edges) S.make_edge(i, j);
S.solve();
for (int i = 0; i < N; i++) {
if (S.id[i * 2] == S.id[i * 2 + 1])
bug(i, S.id[i * 2], S.id[i * 2 + 1]);
assert(S.id[i * 2] != S.id[i * 2 + 1]);
}
auto go = [&](const vector<ar<int, 3>> v, int x1, int x2) {
for (auto [l, r, i] : v) if (S.id[i] > S.id[i ^ 1]) x1 = max(x1, l), x2 = min(x2, r);
bug(x1, x2);
assert(x1 <= x2);
return x1;
};
solutions.push_back({go(A, x1, x2), y1});
solutions.push_back({go(B, x1, x2), y2});
solutions.push_back({x1, go(C, y1, y2)});
solutions.push_back({x2, go(D, y1, y2)});
pr();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
564 KB |
Output is correct |
12 |
Correct |
2 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
568 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
564 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
344 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
560 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
6 ms |
348 KB |
Output is correct |
14 |
Correct |
8 ms |
1360 KB |
Output is correct |
15 |
Correct |
6 ms |
348 KB |
Output is correct |
16 |
Correct |
4 ms |
348 KB |
Output is correct |
17 |
Correct |
8 ms |
1372 KB |
Output is correct |
18 |
Correct |
4 ms |
348 KB |
Output is correct |
19 |
Correct |
6 ms |
348 KB |
Output is correct |
20 |
Correct |
9 ms |
1624 KB |
Output is correct |
21 |
Correct |
6 ms |
348 KB |
Output is correct |
22 |
Correct |
5 ms |
584 KB |
Output is correct |
23 |
Correct |
10 ms |
1836 KB |
Output is correct |
24 |
Correct |
5 ms |
344 KB |
Output is correct |
25 |
Correct |
3 ms |
344 KB |
Output is correct |
26 |
Correct |
3 ms |
344 KB |
Output is correct |
27 |
Correct |
4 ms |
464 KB |
Output is correct |
28 |
Correct |
3 ms |
348 KB |
Output is correct |
29 |
Correct |
3 ms |
344 KB |
Output is correct |
30 |
Correct |
3 ms |
348 KB |
Output is correct |
31 |
Correct |
8 ms |
1364 KB |
Output is correct |
32 |
Correct |
7 ms |
1100 KB |
Output is correct |
33 |
Correct |
8 ms |
1364 KB |
Output is correct |
34 |
Correct |
9 ms |
1368 KB |
Output is correct |
35 |
Correct |
8 ms |
1628 KB |
Output is correct |
36 |
Correct |
8 ms |
1372 KB |
Output is correct |
37 |
Correct |
9 ms |
1884 KB |
Output is correct |
38 |
Correct |
7 ms |
1884 KB |
Output is correct |
39 |
Correct |
9 ms |
1372 KB |
Output is correct |
40 |
Correct |
8 ms |
1236 KB |
Output is correct |
41 |
Correct |
8 ms |
1372 KB |
Output is correct |
42 |
Correct |
8 ms |
1596 KB |
Output is correct |
43 |
Correct |
9 ms |
1624 KB |
Output is correct |
44 |
Correct |
8 ms |
1880 KB |
Output is correct |
45 |
Correct |
4 ms |
348 KB |
Output is correct |
46 |
Correct |
8 ms |
1628 KB |
Output is correct |
47 |
Correct |
9 ms |
1628 KB |
Output is correct |
48 |
Correct |
9 ms |
1880 KB |
Output is correct |
49 |
Correct |
9 ms |
1624 KB |
Output is correct |
50 |
Correct |
8 ms |
1624 KB |
Output is correct |
51 |
Correct |
8 ms |
1860 KB |
Output is correct |
52 |
Correct |
8 ms |
1368 KB |
Output is correct |
53 |
Correct |
9 ms |
1624 KB |
Output is correct |
54 |
Correct |
8 ms |
1884 KB |
Output is correct |
55 |
Correct |
8 ms |
1364 KB |
Output is correct |
56 |
Correct |
8 ms |
1372 KB |
Output is correct |
57 |
Correct |
7 ms |
1368 KB |
Output is correct |
58 |
Correct |
7 ms |
1372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
62 ms |
8660 KB |
Output is correct |
6 |
Correct |
60 ms |
8828 KB |
Output is correct |
7 |
Correct |
62 ms |
8656 KB |
Output is correct |
8 |
Correct |
61 ms |
8832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
67 ms |
8816 KB |
Output is correct |
6 |
Correct |
80 ms |
9176 KB |
Output is correct |
7 |
Correct |
64 ms |
8932 KB |
Output is correct |
8 |
Correct |
74 ms |
8792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
564 KB |
Output is correct |
12 |
Correct |
2 ms |
344 KB |
Output is correct |
13 |
Correct |
74 ms |
8744 KB |
Output is correct |
14 |
Correct |
74 ms |
8660 KB |
Output is correct |
15 |
Correct |
65 ms |
8660 KB |
Output is correct |
16 |
Correct |
64 ms |
8660 KB |
Output is correct |
17 |
Correct |
70 ms |
8824 KB |
Output is correct |
18 |
Correct |
65 ms |
8660 KB |
Output is correct |
19 |
Correct |
83 ms |
8828 KB |
Output is correct |
20 |
Correct |
95 ms |
8572 KB |
Output is correct |
21 |
Correct |
186 ms |
8840 KB |
Output is correct |
22 |
Correct |
142 ms |
8744 KB |
Output is correct |
23 |
Correct |
108 ms |
8852 KB |
Output is correct |
24 |
Correct |
116 ms |
9056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
568 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
564 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
344 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
560 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
6 ms |
348 KB |
Output is correct |
14 |
Correct |
8 ms |
1360 KB |
Output is correct |
15 |
Correct |
6 ms |
348 KB |
Output is correct |
16 |
Correct |
4 ms |
348 KB |
Output is correct |
17 |
Correct |
8 ms |
1372 KB |
Output is correct |
18 |
Correct |
4 ms |
348 KB |
Output is correct |
19 |
Correct |
6 ms |
348 KB |
Output is correct |
20 |
Correct |
9 ms |
1624 KB |
Output is correct |
21 |
Correct |
6 ms |
348 KB |
Output is correct |
22 |
Correct |
5 ms |
584 KB |
Output is correct |
23 |
Correct |
10 ms |
1836 KB |
Output is correct |
24 |
Correct |
5 ms |
344 KB |
Output is correct |
25 |
Correct |
3 ms |
344 KB |
Output is correct |
26 |
Correct |
3 ms |
344 KB |
Output is correct |
27 |
Correct |
4 ms |
464 KB |
Output is correct |
28 |
Correct |
3 ms |
348 KB |
Output is correct |
29 |
Correct |
3 ms |
344 KB |
Output is correct |
30 |
Correct |
3 ms |
348 KB |
Output is correct |
31 |
Correct |
8 ms |
1364 KB |
Output is correct |
32 |
Correct |
7 ms |
1100 KB |
Output is correct |
33 |
Correct |
8 ms |
1364 KB |
Output is correct |
34 |
Correct |
9 ms |
1368 KB |
Output is correct |
35 |
Correct |
8 ms |
1628 KB |
Output is correct |
36 |
Correct |
8 ms |
1372 KB |
Output is correct |
37 |
Correct |
9 ms |
1884 KB |
Output is correct |
38 |
Correct |
7 ms |
1884 KB |
Output is correct |
39 |
Correct |
9 ms |
1372 KB |
Output is correct |
40 |
Correct |
8 ms |
1236 KB |
Output is correct |
41 |
Correct |
8 ms |
1372 KB |
Output is correct |
42 |
Correct |
8 ms |
1596 KB |
Output is correct |
43 |
Correct |
9 ms |
1624 KB |
Output is correct |
44 |
Correct |
8 ms |
1880 KB |
Output is correct |
45 |
Correct |
4 ms |
348 KB |
Output is correct |
46 |
Correct |
8 ms |
1628 KB |
Output is correct |
47 |
Correct |
9 ms |
1628 KB |
Output is correct |
48 |
Correct |
9 ms |
1880 KB |
Output is correct |
49 |
Correct |
9 ms |
1624 KB |
Output is correct |
50 |
Correct |
8 ms |
1624 KB |
Output is correct |
51 |
Correct |
8 ms |
1860 KB |
Output is correct |
52 |
Correct |
8 ms |
1368 KB |
Output is correct |
53 |
Correct |
9 ms |
1624 KB |
Output is correct |
54 |
Correct |
8 ms |
1884 KB |
Output is correct |
55 |
Correct |
8 ms |
1364 KB |
Output is correct |
56 |
Correct |
8 ms |
1372 KB |
Output is correct |
57 |
Correct |
7 ms |
1368 KB |
Output is correct |
58 |
Correct |
7 ms |
1372 KB |
Output is correct |
59 |
Correct |
68 ms |
8656 KB |
Output is correct |
60 |
Correct |
72 ms |
8820 KB |
Output is correct |
61 |
Correct |
66 ms |
8572 KB |
Output is correct |
62 |
Correct |
66 ms |
8572 KB |
Output is correct |
63 |
Correct |
73 ms |
8916 KB |
Output is correct |
64 |
Correct |
65 ms |
8664 KB |
Output is correct |
65 |
Correct |
67 ms |
8552 KB |
Output is correct |
66 |
Correct |
73 ms |
8816 KB |
Output is correct |
67 |
Correct |
193 ms |
8752 KB |
Output is correct |
68 |
Correct |
134 ms |
8740 KB |
Output is correct |
69 |
Correct |
97 ms |
8800 KB |
Output is correct |
70 |
Correct |
115 ms |
8604 KB |
Output is correct |
71 |
Correct |
635 ms |
8988 KB |
Output is correct |
72 |
Correct |
1044 ms |
132596 KB |
Output is correct |
73 |
Correct |
594 ms |
9012 KB |
Output is correct |
74 |
Correct |
381 ms |
8708 KB |
Output is correct |
75 |
Correct |
783 ms |
93072 KB |
Output is correct |
76 |
Correct |
399 ms |
8804 KB |
Output is correct |
77 |
Correct |
538 ms |
9036 KB |
Output is correct |
78 |
Correct |
973 ms |
137064 KB |
Output is correct |
79 |
Correct |
582 ms |
9000 KB |
Output is correct |
80 |
Correct |
493 ms |
9052 KB |
Output is correct |
81 |
Correct |
995 ms |
132988 KB |
Output is correct |
82 |
Correct |
436 ms |
8844 KB |
Output is correct |
83 |
Correct |
224 ms |
8908 KB |
Output is correct |
84 |
Correct |
222 ms |
8584 KB |
Output is correct |
85 |
Correct |
313 ms |
9032 KB |
Output is correct |
86 |
Correct |
284 ms |
8948 KB |
Output is correct |
87 |
Correct |
257 ms |
8812 KB |
Output is correct |
88 |
Correct |
275 ms |
8752 KB |
Output is correct |
89 |
Correct |
955 ms |
112156 KB |
Output is correct |
90 |
Correct |
1043 ms |
135876 KB |
Output is correct |
91 |
Correct |
883 ms |
105628 KB |
Output is correct |
92 |
Correct |
983 ms |
156524 KB |
Output is correct |
93 |
Correct |
1002 ms |
141204 KB |
Output is correct |
94 |
Correct |
994 ms |
128740 KB |
Output is correct |
95 |
Correct |
976 ms |
136304 KB |
Output is correct |
96 |
Correct |
922 ms |
132212 KB |
Output is correct |
97 |
Correct |
979 ms |
139868 KB |
Output is correct |
98 |
Correct |
1008 ms |
127548 KB |
Output is correct |
99 |
Correct |
853 ms |
112972 KB |
Output is correct |
100 |
Correct |
1008 ms |
139244 KB |
Output is correct |
101 |
Correct |
965 ms |
138856 KB |
Output is correct |
102 |
Correct |
752 ms |
83120 KB |
Output is correct |
103 |
Correct |
990 ms |
140928 KB |
Output is correct |
104 |
Correct |
829 ms |
101488 KB |
Output is correct |
105 |
Correct |
981 ms |
147176 KB |
Output is correct |
106 |
Correct |
1060 ms |
139076 KB |
Output is correct |
107 |
Correct |
979 ms |
128360 KB |
Output is correct |
108 |
Correct |
992 ms |
143948 KB |
Output is correct |
109 |
Correct |
976 ms |
138444 KB |
Output is correct |
110 |
Correct |
993 ms |
136080 KB |
Output is correct |
111 |
Correct |
1044 ms |
127632 KB |
Output is correct |
112 |
Correct |
1012 ms |
141700 KB |
Output is correct |
113 |
Correct |
823 ms |
106260 KB |
Output is correct |
114 |
Correct |
831 ms |
105972 KB |
Output is correct |
115 |
Correct |
818 ms |
106244 KB |
Output is correct |
116 |
Correct |
812 ms |
106088 KB |
Output is correct |
117 |
Correct |
1289 ms |
179968 KB |
Output is correct |
118 |
Correct |
1356 ms |
181376 KB |
Output is correct |
119 |
Correct |
1301 ms |
180056 KB |
Output is correct |
120 |
Correct |
1298 ms |
181872 KB |
Output is correct |
121 |
Correct |
1289 ms |
181512 KB |
Output is correct |
122 |
Correct |
1323 ms |
180332 KB |
Output is correct |
123 |
Correct |
1326 ms |
181192 KB |
Output is correct |
124 |
Correct |
1333 ms |
181816 KB |
Output is correct |
125 |
Correct |
1332 ms |
179900 KB |
Output is correct |
126 |
Correct |
1316 ms |
181412 KB |
Output is correct |