1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <set>
#include <list>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;

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

const int INF = 1 << 30;
const double EPS = 1e-12;

typedef pair<pii,pii> pole;

list<pole> territory;

bool przecina(pole p1, pole p2)
{
    int right=min(p1.se.fi, p2.se.fi);
    int left=max(p1.fi.fi, p2.fi.fi);
    int up=min(p1.se.se, p2.se.se);
    int down=max(p1.fi.se, p2.fi.se);
    if(right>left && up>down) return true;
    return false;
}

pole polacz(pole p1, pole p2)
{
    int right=max(p1.se.fi, p2.se.fi);
    int left=min(p1.fi.fi, p2.fi.fi);
    int up=max(p1.se.se, p2.se.se);
    int down=min(p1.fi.se, p2.fi.se);
    return mp(mp(left,down),mp(right,up));
}

void dodaj(pole t)
{
    for(auto it=territory.begin(); it!=territory.end(); it++)
    {
        if(przecina(t,*it))
        {
            pole new_t=polacz(t,*it);
            territory.erase(it);
            return dodaj(new_t);
        }
    }
    territory.push_back(t);
}

int main()
{
    ios_base::sync_with_stdio(0);
    int n;
    cin>>n;
    for(int i=0; i<n; i++)
    {
        pii t1, t2;
        cin>>t1.fi>>t2.fi>>t1.se>>t2.se;
        if(t1.fi>t2.fi) swap(t1.fi,t2.fi);
        if(t1.se>t2.se) swap(t1.se,t2.se);
        dodaj(mp(t1,t2));
    }
    vector<pole> tt;
    for(auto it=territory.begin(); it!=territory.end(); it++)
    {
        pole p=*it;
        tt.pb(mp(mp(p.fi.fi,p.se.fi),mp(p.fi.se,p.se.se)));
    }
    sort(tt.begin(), tt.end());
    cout<<tt.size()<<"\n";
    for(auto it=tt.begin(); it!=tt.end(); it++)
    {
        pole p=*it;
        cout<<p.fi.fi<<" "<<p.fi.se<<" "<<p.se.fi<<" "<<p.se.se<<"\n";
    }
}