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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include <cstdio>
#include <algorithm>
#include <vector>
#include <map>
#include <cstdlib>
#include <random>

#define REP(i,n) for(int i=0; i<(n); ++i)

#define DEBUG if(0)

std::mt19937 rnd(78623874);

enum { n_max = 100000, x_max = 1001000 };

struct Rect
{
  int x1,x2,y1,y2;
  Rect box(const Rect &r)
  {
    return Rect{std::min(x1,r.x1),std::max(x2,r.x2),
      std::min(y1,r.y1),std::max(y2,r.y2)};
  }
  bool cross(const Rect &r) const
  {
    return std::max(x1,r.x1)<=std::min(x2,r.x2) &&
      std::max(y1,r.y1)<=std::min(y2,r.y2);
  }

  bool operator==(const Rect &r) const
  { return x1==r.x1 && x2==r.x2 && y1==r.y1 && y2==r.y2; }

  bool operator<(const Rect &r) const
  {
    return x1!=r.x1 ? x1<r.x1 : x2!=r.x2 ? x2<r.x2 : 
      y1!=r.y1 ? y1<r.y1 : y2<r.y2;
  }
};

void print(const char *name, std::vector<Rect> &R)
{
  printf("===========================\n[%s]\n",name);
  REP(i,R.size()) printf("%d: [%d;%d] [%d;%d]\n",i,R[i].x1,R[i].x2,R[i].y1,R[i].y2);
}

struct In
{
  typedef unsigned int ui;
  std::vector<Rect> R;
  void read()
  {
    int n; scanf("%d",&n); R.resize(n);
    for(auto &r : R)
    {
      scanf("%d%d%d%d",&r.x1,&r.x2,&r.y1,&r.y2);
      r.x1++; r.y1++;
    }
  }

  void gen_max(){ gen(n_max,x_max-100,100); }

  void gen(ui n = 100, ui x = 10000, ui l = 100)
  {
    R.resize(n);
    for(auto &r : R) r = Rect{int(rnd()%x),int(rnd()%l),int(rnd()%x),int(rnd()%l)};
    for(auto &r : R){ r.x2 += r.x1; r.y2 += r.y1; }
  }

  void print()
  {
    printf("%d\n",R.size());
    REP(i,R.size()) printf("[%d;%d] [%d;%d]\n",R[i].x1,R[i].x2,R[i].y1,R[i].y2);
  }
};

struct Seg
{
  int a,b; // [a,b]
  bool cross(Seg s) const { return std::max(a,s.a)<=std::min(b,s.b); }
  bool contains(Seg s) const { return a<=s.a && s.b<=b; }
  int length() const { return b-a+1; }
  bool side(int c) const { return c>(a+b)/2; }
  Seg half(bool i) const
  { int m=(a+b)/2; return i ? Seg{m+1,b} : Seg{a,m}; }
};

struct Tree
{ 
  int h;
  std::vector<std::map<int,int>> T;
  Tree(int x)
  {
    h = 1; while(h<x) h<<=1;
    T.resize(2*h);
  }
  void insert(Seg x, int y, int r){ insert(1,Seg{0,h-1},x,y,r); }
  void insert(int t, Seg cx, Seg x, int y, int r) //[u,v]
  {
    if(t>=T.size() || !cx.cross(x)) return;
    if(x.contains(cx)) T[t][y] = r;
    else
    {
      insert(t<<1,cx.half(0),x,y,r); 
      insert(t<<1|1,cx.half(1),x,y,r);  
    }
  }
  
  void erase(Seg x, int y){ erase(1,Seg{0,h-1},x,y); }
  void erase(int t, Seg cx, Seg x, int y)
  {
    if(!cx.cross(x)) return;
    if(x.contains(cx)) T[t].erase(y);
    else if(cx.length()>1)
    {
      erase(t<<1,cx.half(0),x,y); 
      erase(t<<1|1,cx.half(1),x,y);  
    }
  }
  
  int query(int x, Seg y){ return query(1,Seg{0,h-1},x,y); }
  int query(int t, Seg cx, int x, Seg y)
  {
    while(t<T.size())
    {
      auto it = T[t].lower_bound(y.a);
      if(it!=T[t].end() && it->first<=y.b)
      {
        DEBUG printf("[%d;%d]x{%d} collides with {%d}x[%d;%d]\n",cx.a,cx.b,it->first,x,y.a,y.b);
        return it->second;
      }
      bool s = cx.side(x); t = t<<1|s;
      cx = cx.half(s);
    }
    return -1;
  }
};

struct Event
{
  int i,x,s,y1,y2; //rect id, x-coord, sign, y-length
  bool operator<(const Event &e) const { return x!=e.x ? x<e.x : s!=e.s ? s>e.s : y2-y1>e.y2-e.y1; }
};

struct ST
{
  std::vector<int> A;
  ST(int x) : A(x,0) {}
  void add(int x, int v){ while(x<A.size()){ A[x] += v; x += x&-x; } }
  int get(int x)
  {
    int res = 0;
    while(x){ res += A[x]; x -= x&-x; }
    return res;
  }
};

void go(const In &I, std::vector<Rect> &res)
{
  Tree XY(x_max), YX(x_max);
  std::map<int,Rect> cur;
  REP(i,I.R.size())
  {
    DEBUG printf("begin %d\n",i);
    Rect r = I.R[i];
    while(1)
    {
      int q = -1;
      if(q==-1) q = YX.query(r.y1,Seg{r.x1,r.x2});
      if(q==-1) q = YX.query(r.y2,Seg{r.x1,r.x2});
      if(q==-1) q = XY.query(r.x1,Seg{r.y1,r.y2});
      if(q==-1) q = XY.query(r.x2,Seg{r.y1,r.y2});
      if(q==-1) break;
      DEBUG printf("erasing %d\n",q);
      Rect s = cur[q]; cur.erase(q);
      YX.erase(Seg{s.y1,s.y2},s.x1);
      YX.erase(Seg{s.y1,s.y2},s.x2);
      XY.erase(Seg{s.x1,s.x2},s.y1);
      XY.erase(Seg{s.x1,s.x2},s.y2);
      r = r.box(s);
      DEBUG printf("box = [%d;%d] [%d;%d]\n",r.x1,r.x2,r.y1,r.y2);
    }
    cur[i] = r;
    YX.insert(Seg{r.y1,r.y2},r.x1,i);
    YX.insert(Seg{r.y1,r.y2},r.x2,i);
    XY.insert(Seg{r.x1,r.x2},r.y1,i);
    XY.insert(Seg{r.x1,r.x2},r.y2,i);
    DEBUG printf("end %d\n",i);
  }
  std::vector<Event> E;
  for(auto &p : cur)
  {
    int i = p.first;
    Rect r = p.second;
    E.emplace_back(Event{i,r.x1,1,r.y1+1,r.y2+1});
    E.emplace_back(Event{i,r.x2,-1,r.y1+1,r.y2+1});
  }
  std::sort(E.begin(),E.end());

  DEBUG
  {
    std::vector<Rect> tmp; for(auto p : cur) tmp.push_back(p.second);
    print("before reduction",tmp);
  }

  ST st(x_max+10); res.clear();
  for(auto e : E)
  {
    if(e.s>0){ st.add(e.y1,1); st.add(e.y2+1,-1); }
    else
    {
      //printf("x=%d y1=%d y2=%d\n",e.x,e.y1,e.y2);
      //printf("%d\n",st.get(e.y1));
      st.add(e.y1,-1); st.add(e.y2+1,1);
      if(!st.get(e.y1)) res.push_back(cur[e.i]);
    }
  }
  std::sort(res.begin(),res.end());
}

void brute(const In &I, std::vector<Rect> &out)
{
  out.clear();
  for(auto r : I.R)
  {
    while(1)
    {
      bool ok = 1;
      REP(i,out.size()) if(r.cross(out[i]))
      {
        r = r.box(out[i]);
        std::swap(out[i],out.back()); out.pop_back();
        ok = 0; break;
      }
      if(ok) break;
    }
    out.push_back(r);
  }
  std::sort(out.begin(),out.end());
}

void test()
{
  REP(_,5)
  {
    puts("max test");
    In I; I.gen_max();
    std::vector<Rect> s;
    go(I,s);
  }
  for(int c=0;;c++)
  {
    In I; I.gen();
    std::vector<Rect> s,b;
    go(I,s); brute(I,b);
    bool ok = s.size()==b.size();
    if(ok) REP(i,s.size()) ok &= s[i]==b[i];
    if(!ok)
    {
      puts("ERROR");
      I.print();
      print("s",s); print("b",b);
      exit(1);
    }
    if(!(c%2)) printf("ok %d\n",c);
  }
}

int main()
{
  //test();
  In I; I.read();
  std::vector<Rect> res; go(I,res);
  printf("%d\n",res.size());
  for(auto r : res) printf("%d %d %d %d\n",r.x1-1,r.x2,r.y1-1,r.y2);
  return 0;
}