9262022-01-28 13:57:25Valaki2Ádám és Éva együttcpp14Accepted 50/5014ms7628 KiB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pb push_back
#define mp make_pair
#define fi first
#define se second

struct segment {
    int l, r;
    segment() : l(0), r(0) {}
    segment(int l_, int r_) : l(l_), r(r_) {}
};

bool before(segment a, segment b) {
    return a.r <= b.l;
}

bool mergeable(segment a, segment b) {
    if(a.l >= b.r) return false;
    if(b.l >= a.r) return false;
    return true;
}

bool after(segment a, segment b) {
    return b.r <= a.l;
}

segment do_merge(segment a, segment b) {
    return segment(max(a.l, b.l), min(a.r, b.r));
}

int p, n, m;
vector<segment> a;
vector<segment> b;

vector<segment> ans;

void solve() {
    cin >> p;
    cin >> n;
    for(int i = 0; i < n; i++) {
        int x, y;
        cin >> x >> y;
        a.pb(segment(x, y));
    }
    cin >> m;
    for(int i = 0; i < m; i++) {
        int x, y;
        cin >> x >> y;
        b.pb(segment(x, y));
    }
    int i = 0, j = 0;
    while(i < n && j < m) {
        if(mergeable(a[i], b[j])) {
            ans.pb(do_merge(a[i], b[j]));
            if(a[i].r <= b[j].r) {
                i++;
            } else {
                j++;
            }
        } else if(before(a[i], b[j])) {
            i++;
        } else if(after(a[i], b[j])) {
            j++;
        }
    }
    cout << ans.size() << "\n";
    for(segment s : ans) {
        cout << s.l << " " << s.r << "\n";
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/02ms1844 KiB
2Accepted0/01ms1912 KiB
3Accepted2/22ms1968 KiB
4Accepted2/21ms1972 KiB
5Accepted2/21ms1972 KiB
6Accepted2/21ms1980 KiB
7Accepted2/21ms1980 KiB
8Accepted2/21ms1988 KiB
9Accepted2/21ms1988 KiB
10Accepted2/21ms1996 KiB
11Accepted2/21ms1996 KiB
12Accepted2/21ms2008 KiB
13Accepted2/21ms2008 KiB
14Accepted2/21ms2012 KiB
15Accepted2/21ms2016 KiB
16Accepted2/214ms3848 KiB
17Accepted3/314ms4484 KiB
18Accepted3/314ms4964 KiB
19Accepted4/413ms5688 KiB
20Accepted4/414ms6576 KiB
21Accepted4/414ms6992 KiB
22Accepted4/414ms7628 KiB