贪心 + 模拟 即可

#include <bits/stdc++.h>
using namespace std;

const int N = 1e6 + 10;
int n, m, k, l, p;
struct line {
    int s;
    int t;
    int cnt;
};
map<pair<int, int>, int> hmp;
map<pair<int, int>, int> smp;
line heng[N];
line shu[N];
int toth, tots;
vector<pair<int, int>> vech;
vector<pair<int, int>> vecs;
bool cmp(line a, line b) {
    return a.cnt > b.cnt;
}
int ansh[N];
int anss[N];
int main() {
    cin >> n >> m >> k >> l >> p;
    for (int i = 1; i <= p; i++) {

        int ax, ay, bx, by;
        cin >> ax >> ay >> bx >> by;
        if (abs(ax - bx) == 1) {
            if (ax > bx) swap(ax, bx);
            if (hmp[ {ax, bx}] == 0) vech.push_back({ax, bx});
            hmp[ {ax, bx}]++;
        }
        if (abs(ay - by) == 1) {
            if (ay > by) swap(ay, by);
            if (smp[ {ay, by}] == 0) vecs.push_back({ay, by});
            smp[ {ay, by}]++;
        }
    }
    for (int i = 0; i < (int)vech.size(); i++) {
        int ax = vech[i].first;
        int bx = vech[i].second;
        toth++;
        heng[toth].s = ax;
        heng[toth].t = bx;
        heng[toth].cnt = hmp[ {ax, bx}];
    }
    for (int i = 0; i < (int)vecs.size(); i++) {
        int ay = vecs[i].first;
        int by = vecs[i].second;
        tots++;
        shu[tots].s = ay;
        shu[tots].t = by;
        shu[tots].cnt = smp[ {ay, by}];
    }
    sort(heng + 1, heng + 1 + toth, cmp);
    sort(shu + 1, shu + 1 + tots, cmp);
    for (int i = 1; i <= min(k, toth); i++) {
        ansh[i] = heng[i].s;
    }
    for (int i = 1; i <= min(l, tots); i++) {
        anss[i] = shu[i].s;
    }
    sort(ansh + 1, ansh + 1 + min(k, toth));
    sort(anss + 1, anss + 1 + min(l, tots));
    for (int i = 1; i <= min(k, toth); i++) cout << ansh[i] << ' ';
    cout << "\n";
    for (int i = 1; i <= min(l, tots); i++) cout << anss[i] << ' ';

}