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
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Konrad Szlesinski
#include <iostream>
#include <string>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <bitset>
#include <limits>
using namespace std;
typedef vector<bool> VB;
typedef vector<int> VI;
typedef long long LL;
typedef vector<LL> VLL;
typedef unsigned long long ULL;
typedef vector<ULL> VULL;
typedef vector<VI> VVI;
typedef pair<int,int> PII;
typedef vector<PII> VPII;
typedef set<int> SI;
typedef vector<SI> VSI;
typedef set<VI> SVI;
typedef multiset<int> mSI;
typedef map<int,int> MII;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define FORD(i,a,b) for(int i=(b)-1;i>=(a);--i)
#define REP(i,n) FOR(i,0,n)
#define REPD(i,n) FORD(i,0,n)
#define VAR(v,w) __typeof(w) v=(w)
#define FORE(it,c) for(VAR(it,(c).begin());it!=(c).end();++it)
#define FORED(it,c) for(VAR(it,(c).rbegin());it!=(c).rend();++it)
#define FORENA(it,c) for(VAR(it,(c).begin());it!=(c).end();)
#define ALL(c) (c).begin(),(c).end()
#define PF push_front
#define POF pop_front
#define PB push_back
#define POB pop_back
#define PH push_heap
#define POH pop_heap
#define INS insert
#define ER erase
#define MH make_heap
#define MP make_pair
#define FT first
#define SD second

// BRZYDKA KWADRATÓWA, BLE ;C

typedef struct Rect {
    int x1, x2, y1, y2;
} Rect;
typedef list<Rect> LR;
typedef vector<Rect> VR;

bool intersect(Rect r1, Rect r2) { return !(r1.x2 <= r2.x1 || r2.x2 <= r1.x1 || r1.y2 <= r2.y1 || r2.y2 <= r1.y1); }

bool lexical_cmp(Rect r1, Rect r2) {
    if(r1.x1 != r2.x1)
        return (r1.x1 < r2.x1);
    if(r1.x2 != r2.x2)
        return (r1.x2 < r2.x2);
    if(r1.y1 != r2.y1)
        return (r1.y1 < r2.y1);
    return (r1.y2 < r2.y2);
}

int main() {

    ios_base::sync_with_stdio(false);

    int n;
    LR R;

    cin >> n;
    REP(i, n)
    {
        Rect r;
        cin >> r.x1 >> r.x2 >> r.y1 >> r.y2;
        R.PB(r);
    }

    FORENA(it, R)
    {
        bool merge = false;
        for(VAR(it2, R.begin()); it2 != it; ++it2)
        {
            if(intersect(*it, *it2))
            {
                Rect new_r;
                new_r.x1 = min(it->x1, it2->x1);
                new_r.y1 = min(it->y1, it2->y1);
                new_r.x2 = max(it->x2, it2->x2);
                new_r.y2 = max(it->y2, it2->y2);
                R.PB(new_r);
                R.ER(it2);
                it = R.ER(it);
                merge = true;
                break;
            }
        }
        if(!merge)
            ++it;
    }

    VR result;
    FORE(it, R)
        result.PB(*it);
    sort(result.begin(), result.end(), lexical_cmp);

    cout << result.size() << endl;
    FORE(it, result)
        cout << it->x1 << " " << it->x2 << " " << it->y1 << " " << it->y2 << endl;

    return 0;
}