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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <iostream>
#include <algorithm>

using namespace std;

struct prostokat
{
    long x1;
    long y1;
    long x2;
    long y2;
    long id;
    long next;
    long prev;
};

prostokat prostokaty[100001];

int porownaj(const void* p1, const void* p2)
{
    if(((prostokat*)p1)->x1<((prostokat*)p2)->x1) return -1;
    if(((prostokat*)p1)->x1==((prostokat*)p2)->x1)
    {
        if(((prostokat*)p1)->x2<((prostokat*)p2)->x2) return -1;
        if(((prostokat*)p1)->x2==((prostokat*)p2)->x2)
        {
            if(((prostokat*)p1)->y1<((prostokat*)p2)->y1) return -1;
            if(((prostokat*)p1)->y1==((prostokat*)p2)->y1)
            {
                if(((prostokat*)p1)->y2<((prostokat*)p2)->y2) return -1;
                if(((prostokat*)p1)->y2==((prostokat*)p2)->y2) return 0;
                if(((prostokat*)p1)->y2>((prostokat*)p2)->y2) return 1;
            }
            if(((prostokat*)p1)->y1>((prostokat*)p2)->y1) return 1;
        }
        if(((prostokat*)p1)->x2>((prostokat*)p2)->x2) return 1;

    }
    if(((prostokat*)p1)->x1>((prostokat*)p2)->x1) return 1;
}

bool przeciecie(prostokat& p1, prostokat& p2)
{
    if (p1.x1 < p2.x2 && p1.x2 > p2.x1 &&
        p1.y1 < p2.y2 && p1.y2 > p2.y1)
        return true;
    return false;
}

int main()
{
    long n;
    long liczba;

    cin.sync_with_stdio(false);

    cin>>n;

    for(int i=0; i<n; i++)
    {
        cin>>prostokaty[i].x1>>prostokaty[i].x2>>prostokaty[i].y1>>prostokaty[i].y2;
        prostokaty[i].id = i;
    }

    qsort(prostokaty, n, sizeof(prostokat), porownaj);

    for(int i=0; i<n; i++)
    {
        prostokaty[i].next = i+1;
        prostokaty[i].prev = i-1;
    }

    prostokaty[n].id = -1;
    prostokaty[n].next = -1;
    prostokaty[n].prev = n-1;

    liczba = n;
    bool brak_nowych = true;
    long i, k, next;
    long long  iters = 0;
    long long restarts = 0;

    do
    {
        brak_nowych = true;
        i = 0;
        while(-1 != prostokaty[i].next)
        {
            next = prostokaty[i].next;

            k=prostokaty[i].next;


            while(-1 != prostokaty[k].next)
            {
                //iters++;
                if(prostokaty[k].id != -1)
                {
                    if(prostokaty[i].x2<=prostokaty[k].x1)
                        break;

                    if(przeciecie(prostokaty[i], prostokaty[k]))
                    {
                        //cout<<"Przecinaja sie:"<<endl<<" p1 (x1="<<prostokaty[i].x1<<" y1="<<prostokaty[i].y1<<" x2="<<prostokaty[i].x2<<" y2="<<prostokaty[i].y2<<endl;
                        //cout<<" p2 (x1="<<prostokaty[k].x1<<" y1="<<prostokaty[k].y1<<" x2="<<prostokaty[k].x2<<" y2="<<prostokaty[k].y2<<endl;

                        if(prostokaty[i].x1>prostokaty[k].x1)
                            prostokaty[i].x1 = prostokaty[k].x1;

                        if(prostokaty[i].y1>prostokaty[k].y1)
                            prostokaty[i].y1 = prostokaty[k].y1;

                        if(prostokaty[i].x2<prostokaty[k].x2)
                            prostokaty[i].x2 = prostokaty[k].x2;

                        if(prostokaty[i].y2<prostokaty[k].y2)
                            prostokaty[i].y2 = prostokaty[k].y2;

                        prostokaty[k].id = -1;
                        if(-1!=prostokaty[k].prev)
                            prostokaty[prostokaty[k].prev].next = prostokaty[k].next;

                        if(-1!=prostokaty[k].next)
                            prostokaty[prostokaty[k].next].prev = prostokaty[k].prev;

                        liczba--;
                        brak_nowych = false;
                        next = i;

                    }
                }


                k=prostokaty[k].next;
            }

            i = next;
        }
     //cout<<"i="<<i<<" next[i]="<<prostokaty[i].next<<" k="<<k<<" next[k]="<<prostokaty[k].next<<endl;

    //restarts++;
    }while(!brak_nowych);

    k=0;
    for(i=0; i<n; i++)
        if(-1 != prostokaty[i].id)
            prostokaty[k++] = prostokaty[i];

    qsort(prostokaty, liczba, sizeof(prostokat), porownaj);

    //cout<<"iters:"<<iters<<" restarts:"<<restarts<<endl;
    cout<<liczba<<endl;
    for(int i=0; i<liczba; i++)
        if(prostokaty[i].id != -1)
        {
            cout<<prostokaty[i].x1<<" "<<prostokaty[i].x2<<" "<<prostokaty[i].y1<<" "<<prostokaty[i].y2<<endl;
        }

    return 0;
}