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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include <cstdio>
#include <set>
#include <stack>

using namespace std;

const int ABCD = 1048576;

struct Rect
{
    int x1;
    int x2;
    int y1;
    int y2;
};

struct LEX
{
    bool operator () (Rect* a, Rect* b)
    {
        return a->x1 < b->x1 ||
               a->x1 == b->x1 && (
                  a->x2 < b->x2 ||
                  a->x2 == b->x2 && (
                      a->y1 < b->y1 ||
                      a->y1 == b->y1 &&
                          a->y2 < b->y2
                          ));
    }
};

struct NodeComp
{
    bool operator () (Rect* a, Rect* b)
    {
        return a->y2 <= b->y1;
    }
};

struct ST
{
    bool add;
    Rect* rect;
};

struct Node
{
    set<Rect*, NodeComp> ns;
    int ss;
};

Rect rectsSource[200001];
int ri = 0;
set<Rect*, LEX> rects;

Node nodes[2097152];

int n;

stack<ST> st;

bool add(Rect* rect, int i, int b, int e, bool insert)
{
    if(!insert && nodes[i].ss == 0) return true;
    set<Rect*>::iterator it = nodes[i].ns.find(rect);

    if(it != nodes[i].ns.end())
    {
//printf("dublet w %i (%i,%i)\n", i,b,e);
        rectsSource[ri].x1 = min(rect->x1, (*it)->x1);
        rectsSource[ri].x2 = max(rect->x2, (*it)->x2);
        rectsSource[ri].y1 = min(rect->y1, (*it)->y1);
        rectsSource[ri].y2 = max(rect->y2, (*it)->y2);

        ST s; s.add = true; s.rect = rectsSource+ri;
        st.push(s);
        ri++;

        s.add = false; s.rect = rect;
        st.push(s);

        s.rect = *it;
        st.push(s);

        return false;
    }

    if(rect->x1 <= b && rect->x2 >= e && insert)
    {
//printf("Dodaje do %i (%i,%i)\n", i,b,e);
        nodes[i].ns.insert(rect);
        nodes[i].ss++;
        insert = false;
    }

    if( b+1 == e)
        return true;

    bool fl = true;

    int midp = (b+e)/2;
    if( rect->x1 < midp )
    {
        fl = add(rect, i*2, b, midp, insert);
        nodes[i].ss = nodes[i*2].ss + nodes[i*2+1].ss+nodes[i].ns.size();
        if( !fl )
            return false;
    }

    if( rect->x2 > midp)
    {
        fl = add(rect, i*2+1, midp, e, insert);
        nodes[i].ss = nodes[i*2].ss + nodes[i*2+1].ss+nodes[i].ns.size();
        if( !fl )
            return false;
    }

    return true;
}

void remove(Rect* rect, int i, int b, int e)
{
    if(rect->x1 <= b && rect->x2 >= e)
    {
//printf("Przed usunieciem, %i ", nodes[i].ns.size()); for(set<Rect*>::iterator it = nodes[i].ns.begin(); it != nodes[i].ns.end(); it++)    printf("(%i %i %i %i) ", (*it)->x1,(*it)->x2,(*it)->y1,(*it)->y2);

        set<Rect*>::iterator toErase = nodes[i].ns.find(rect);
        NodeComp nc;
        while( toErase != nodes[i].ns.end() && (rect->x1 != (*toErase)->x1 || rect->x2 != (*toErase)->x2 || rect->y1 != (*toErase)->y1 || rect->y2 != (*toErase)->y2) &&
               !nc(rect, *toErase) && !nc(*toErase, rect) )
                toErase++;

        if( toErase != nodes[i].ns.end() && rect->x1 == (*toErase)->x1 && rect->x2 == (*toErase)->x2 && rect->y1 == (*toErase)->y1 && rect->y2 == (*toErase)->y2)
        {
            nodes[i].ns.erase(toErase);
            nodes[i].ss--;
//printf("Po usunieciem, %i ", nodes[i].ns.size()); for(set<Rect*>::iterator it = nodes[i].ns.begin(); it != nodes[i].ns.end(); it++)    printf("(%i %i %i %i) ", (*it)->x1,(*it)->x2,(*it)->y1,(*it)->y2); printf("\n");
            return;
        }
        toErase = nodes[i].ns.find(rect);
        set<Rect*>::iterator beg = nodes[i].ns.begin();
        while( toErase != nodes[i].ns.end() && toErase != beg && (rect->x1 != (*toErase)->x1 || rect->x2 != (*toErase)->x2 || rect->y1 != (*toErase)->y1 || rect->y2 != (*toErase)->y2) &&
               !nc(rect, *toErase) && !nc(*toErase, rect) )
               {
                toErase--;
               }

        if( toErase != nodes[i].ns.end() && rect->x1 == (*toErase)->x1 && rect->x2 == (*toErase)->x2 && rect->y1 == (*toErase)->y1 && rect->y2 == (*toErase)->y2)
        {
            nodes[i].ns.erase(toErase);
            nodes[i].ss--;
//printf("Po usunieciem, %i ", nodes[i].ns.size()); for(set<Rect*>::iterator it = nodes[i].ns.begin(); it != nodes[i].ns.end(); it++)    printf("(%i %i %i %i) ", (*it)->x1,(*it)->x2,(*it)->y1,(*it)->y2); printf("\n");
            return;
        }


//printf("Po usunieciem, %i ", nodes[i].ns.size()); for(set<Rect*>::iterator it = nodes[i].ns.begin(); it != nodes[i].ns.end(); it++)    printf("(%i %i %i %i) ", (*it)->x1,(*it)->x2,(*it)->y1,(*it)->y2); printf("\n");
        return;
    }

    if( b+1 == e)
        return;

    int midp = (b+e)/2;
    if( rect->x1 < midp )
    {
        remove(rect, i*2, b, midp);
        nodes[i].ss = nodes[i*2].ss + nodes[i*2+1].ss+nodes[i].ns.size();

    }

    if( rect->x2 > midp)
    {
        remove(rect, i*2+1, midp, e);
        nodes[i].ss = nodes[i*2].ss + nodes[i*2+1].ss+nodes[i].ns.size();
    }
}

void emptyStack()
{
    while(!st.empty())
    {
        ST s = st.top();
        st.pop();
        if(s.add)
        {
//printf("adding %i %i %i %i\n", s.rect->x1,s.rect->x2,s.rect->y1,s.rect->y2);
            if( add(s.rect, 1, 0, ABCD, true) )
            {
//printf("added\n");
                rects.insert(s.rect);
            }
        }
        else
        {
//printf("removing %i %i %i %i\n", s.rect->x1,s.rect->x2,s.rect->y1,s.rect->y2);
            remove(s.rect, 1, 0, ABCD);
            rects.erase(s.rect);
        }
    }
}

int main()
{
    scanf("%i", &n);
    for(int i = 0; i < n; ++i)
    {
        scanf("%i %i %i %i", &(rectsSource[ri].x1),&(rectsSource[ri].x2),&(rectsSource[ri].y1),&(rectsSource[ri].y2));
        ST s; s.add = true; s.rect = rectsSource+ri;
        st.push(s);
        ri++;
        emptyStack();
    }

    printf("%i\n", rects.size());
    for(set<Rect*>::iterator it = rects.begin(); it != rects.end(); it++)
    {
        printf("%i %i %i %i\n", (*it)->x1,(*it)->x2,(*it)->y1,(*it)->y2);
    }

return 0;
}