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
#include <iostream>
#include <utility>
#include <cstdio>
#include <algorithm>
using namespace std;

#define MAX 100001
#define MAXW 1048576
//#define MAXW 16
//#define MAXW 4
#define DEBUG(x)
#define DEBUG2(x)

struct Point {
    int a,b;
};
struct Square {
    Point x;
    Point y;
};

bool cmpl(const Square &s1, const Square &s2) {
    if(s1.x.a != s2.x.a) return s1.x.a < s2.x.a;
    if(s1.x.b != s2.x.b) return s1.x.b < s2.x.b;
    if(s1.y.a != s2.y.a) return s1.y.a < s2.y.a;
    return s1.y.b < s2.y.b;
}
bool operator!=(const Point &s1, const Point &s2) {
    return (s1.a != s2.a)  || s1.b != s2.b;
}

bool operator!=(const Square &s1, const Square &s2) {
    return (s1.x != s2.x)  || s1.y != s2.y;
}

template<class T>
std::basic_ostream<T> & operator<<(std::basic_ostream<T> &out, const Point &a) {
    out << "(" << a.a << ", " << a.b << ")";
    return out;
}

template<class T>
std::basic_ostream<T> & operator<<(std::basic_ostream<T> &out, const Square &a) {
    out << "[" << a.x << ", " << a.y << "]";
    return out;
}


Square input[MAX];
bool input_noused[MAX];
int mem;
struct Tree {
    Tree():idx(0),l(0),p(0){mem++;}
    int idx;
    Tree *l,*p;
};

Tree word;

Square curr, snew;
bool gotNewSq;

inline void maks(Square &w,const Square &s){
    w.x.a = min(w.x.a, s.x.a);
    w.x.b = max(w.x.b, s.x.b);
    w.y.a = min(w.y.a, s.y.a);
    w.y.b = max(w.y.b, s.y.b);
}

inline void mul(int &x1, int &x2, int &y1, int &y2, int Tx1, int Tx2, int Ty1, int Ty2)
{
    x1 = max(x1, Tx1); x2 = min(x2, Tx2); y1 = max(y1, Ty1); y2 = min(y2, Ty2);
}

void getMaks(Tree *t, int curr_idx) {
    if(!t) return;
    if(t->idx > 0) {
        Square s = input[t->idx];
        maks(snew, s);
//        if(snew != curr)
            gotNewSq = true;
        DEBUG(cout << "getMax: " << snew << " = " << curr << " + " << input[t->idx]  << "(" << t->idx << ")" << curr_idx <<  "\n" );
        input_noused[t->idx] = true;
        input[t->idx] = snew;
        return;
    }
    getMaks(t->l, curr_idx);
    getMaks(t->p, curr_idx);
    delete(t->l);
    delete(t->p);
    t->p = t->l = 0;
    t->idx = 0;
}
Square sq(int x1, int x2, int y1, int y2)
{
    Square s; s.x.a =x1; s.x.b =x2; s.y.a =y1; s.y.b =y2;
    return s;
}
bool inters(int i, int j) {
    Square s1 = input[i], s2 = input[j];
    DEBUG(cout << " inters: " <<  s1 << " * " << s2 );

     mul(s1.x.a,s1.x.b,s1.y.a,s1.y.b, s2.x.a,s2.x.b,s2.y.a,s2.y.b);

     DEBUG(cout << " = " <<  s1 << "\n");
    if(s1.x.a < s1.x.b && s1.y.a < s1.y.b) return true;
    return false;
}
int add_c=0;
bool add(Tree *&t,int curr_idx, bool ori, int x1, int x2, int y1, int y2, int Tx1, int Tx2, int Ty1, int Ty2) {
   DEBUG(cout << curr_idx << " me: " << sq(x1,x2,y1,y2) << " tr: " << sq(Tx1,Tx2,Ty1,Ty2) );
   DEBUG2(add_c++);
   mul(x1,x2,y1,y2,Tx1,Tx2,Ty1,Ty2);
   DEBUG(cout << " mul: " <<  curr_idx << "\n");

   if(!(x1 < x2 && y1 < y2)) return t == NULL;
   if(!t) t = new Tree;
   if(t->idx == curr_idx) return true;
   if(t->idx == 0) {
       t->idx = curr_idx;
       return true;
   }
   if(t->idx > 0) {
       DEBUG(cout << "conflict: "  << t->idx  <<  "\n" );

       if(inters(t->idx, curr_idx)) {
           DEBUG(cout << "intersect: "  << t->idx  <<  "\n" );
           getMaks(t, curr_idx);
           t->idx = curr_idx;
           return true;
       } else {
           int x1,x2,y1,y2, idx = t->idx;
           Square s = input[idx];
           if(ori) { x1 = s.x.a; x2 = s.x.b; y1 = s.y.a; y2 = s.y.b; }
           else    { x1 = s.y.a; x2 = s.y.b; y1 = s.x.a; y2 = s.x.b; }
           DEBUG(cout << "Add old square: " << sq(x1,x2,y1,y2) << " ori: " << ori << "\n");
           t->idx = -1;
           add(t->p, idx, !ori, y1,y2,x1,x2,  Ty1,Ty2,Tx1, (Tx1+Tx2)/2 );
           add(t->l, idx, !ori, y1,y2,x1,x2,  Ty1,Ty2, (Tx1+Tx2)/2, Tx2);
       }
   }

   if(x1==Tx1 && x2==Tx2 && y1==Ty1 && y2==Ty2) {
       getMaks(t, curr_idx);
       t->idx = curr_idx;
       return true;
   } else {
       bool l = add(t->p,curr_idx, !ori, y1,y2,x1,x2,  Ty1,Ty2,Tx1, (Tx1+Tx2)/2);
       bool p = add(t->l,curr_idx, !ori, y1,y2,x1,x2,  Ty1,Ty2, (Tx1+Tx2)/2, Tx2);
       if(l && p) {
           t->idx = curr_idx;
           delete(t->l);
           delete(t->p);
           t->p = t->l = 0;
           return true;
       }
       return false;
   }
}

void printT(Tree *t) {
    if(!t) return;
    cout << t->idx <<"\n";
    printT(t->l);
    printT(t->p);
}

int add_c_s;

Square sl1, sl2;
int main() {
    int n;
    scanf("%d",&n);

    for(int i=1;i<=n;i++) {
        scanf("%d %d %d %d",&curr.x.a,&curr.x.b, &curr.y.a,&curr.y.b);
        input[i] = snew = curr;
        DEBUG(cout << "dodaję: "<< sq(snew.x.a, snew.x.b, snew.y.a,snew.y.b) << "\n");
        do  {
            gotNewSq = false;
            curr = snew;
            DEBUG(cout << "new add: "<< sq(snew.x.a, snew.x.b, snew.y.a,snew.y.b) << "\n");
            DEBUG2(add_c_s++);
            Tree * t= &word;
            add(t, i, true, snew.x.a, snew.x.b, snew.y.a,snew.y.b , 0,MAXW-1, 0,MAXW-1);
        } while (gotNewSq);
        input[i] = snew;
        input_noused[i] = false;
    }

    //printT(&word);
    DEBUG2(cerr << "mem: " << mem << " add_c: " << add_c << " add_c_s: " << add_c_s  << "\n");

    int count=0, count2 = 0;
    for(int i=1;i<=n;i++) {
        if(!input_noused[i]) {
           Square s = input[i];
           input[count++] = s;
        }
    }
    sort(&input[0], &input[count], cmpl);
    for(int i=0;i<count;i++) {
          if(sl1 != input[i]) {
              sl1 = input[i];
              count2 ++;
          }
    }

    printf("%d\n",count2);
    for(int i=0;i<count;i++) {
        if(sl2 != input[i]) {
            sl2 = input[i];
            printf("%d %d %d %d\n", sl2.x.a,sl2.x.b, sl2.y.a,sl2.y.b);
        }
    }
    return 0;
}