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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

public class ple {
    static AVLTreeNode root = null;
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line;
        StringBuilder sb = new StringBuilder();
        int n;
        line = br.readLine();
        n = Integer.parseInt(line);
        List<Rect> list = new ArrayList<Rect>();
        for (int i = 0; i < n; i++) {
            line = br.readLine();
            String[] tab = line.split(" ");
            Rect rect = new Rect();
            rect.x = Long.parseLong(tab[0]);
            rect.width = Long.parseLong(tab[1]) - rect.x;
            rect.y = Long.parseLong(tab[2]);
            rect.height = Long.parseLong(tab[3]) - rect.y;
            rect.number = i + 1;
            list.add(rect);

        }
        list = solve(list);
        List<String> out = new ArrayList<String>();
        for (Rect rect : list)
            if(!out.contains(rect.toString()))
            out.add(rect.toString());
        Collections.sort(out);
        sb.append(out.size());
        sb.append('\n');
        for (int i = 0; i < out.size(); i++) {
            sb.append(out.get(i));
            if (i != out.size() - 1)
                sb.append('\n');
        }
        System.out.print(sb);
    }

    public static List<Rect> solve(List<Rect> in) {
        List<Rect> out = new ArrayList<Rect>();
        int n = -1;
        long rst = 0, dst = 0;
        while (n != 0) {
            List<Coord> xCoords = new ArrayList<Coord>();
            root = null;
            Map<Rect, List<Rect>> map = new TreeMap<Rect, List<Rect>>();

            for (Rect rect : in) {
                Coord coord = new Coord((int) rect.x, true);
                coord.rect = rect;
                xCoords.add(coord);
                coord = new Coord((int) (rect.x + rect.width), false);
                coord.rect = rect;
                xCoords.add(coord);
            }
            Collections.sort(xCoords);
            for (Coord coord : xCoords) {
                if (coord.isLeft) {
                    long time = System.nanoTime();
                    List<Rect> res = rangeSearch(root, (int) coord.rect.y, (int) (coord.rect.y + coord.rect.height));
                    time = System.nanoTime() - time;
                    rst += time;
                    //System.out.println("RS " + time);
                    if(res !=null)
                    for (Rect rect :res
                            )
                        if (isFieldGTZero(coord.rect, rect)&&
                                !(rect.x == coord.rect.x && rect.y == coord.rect.y &&
                                        rect.width == coord.rect.width && rect.height == coord.rect.height)) {
                            if (map.get(coord.rect) != null) {

                                map.get(coord.rect).add(rect);
                                int idx = Collections.binarySearch(in,rect);
                                if(idx>=0)
                                in.remove(idx);
                            } else {
                                List<Rect> l = new ArrayList<Rect>();
                                l.add(rect);
                                map.put(coord.rect, l);
                                int idx = Collections.binarySearch(in,rect);
                                if(idx>=0)
                                in.remove(idx);
                                idx = Collections.binarySearch(in,coord.rect);
                                if(idx>=0)
                                in.remove(idx);
                            }
                        }
                    AVLTreeNode node = new AVLTreeNode((int) coord.rect.y, coord.rect);
                    insertAVL(root,node);
                    node = new AVLTreeNode((int)(coord.rect.y + coord.rect.height), coord.rect);
                    insertAVL(root,node);


                } else {

                    removeAVL(root, (int) coord.rect.y);
                    removeAVL(root, (int) (coord.rect.y + coord.rect.height));
                }
            }
            n = map.size();
            long time = System.nanoTime();
            Map<Integer, List<Rect>> ds = getDisjointSets(map);
            time = System.nanoTime() - time;
            dst += time;
            //System.out.println("DS " + time);
            List<Rect> in2 = new ArrayList<Rect>();
            int i = 0;
            for (List<Rect> list : ds.values()) {
                Rect r = enclose(list);

                r.number = ++i;

                in2.add(r);
            }
            for (int j=0;j<in.size();j++) {
                Rect rect = in.get(j);
                rect.number = ++i;
                in2.add(rect);

            }
            in = in2;


            if (n == 0)
                out.addAll(in);
        }
        return out;
    }

    static Map<Integer, List<Rect>> getDisjointSets(Map<Rect, List<Rect>> in) {

        Map<Integer, Integer> map = new TreeMap<Integer, Integer>();
        Map<Integer, List<Rect>> res = new TreeMap<Integer, List<Rect>>();

        for (Map.Entry<Rect, List<Rect>> entry : in.entrySet()) {
            map.put((int) entry.getKey().number, (int) entry.getKey().number);
            for (Rect rect : entry.getValue()) {
                map.put((int) rect.number, (int) rect.number);
            }
        }
        for (Map.Entry<Rect, List<Rect>> entry : in.entrySet()) {
            if (!res.containsKey(map.get((int)entry.getKey().number))) {
                List<Rect> l = new ArrayList<Rect>();
                l.add(entry.getKey());
                res.put((int) map.get((int) entry.getKey().number), l);
            }
            for (Rect rect : entry.getValue()) {
                if (map.get((int) entry.getKey().number) != map.get((int) rect.number)) {
                    map.put((int) rect.number, (int) map.get((int) entry.getKey().number));

                    res.get((int) map.get((int) entry.getKey().number)).add(rect);
                }
            }

        }
        return res;
    }

    static class Coord implements Comparable<Coord> {
        int c;
        boolean isLeft = false;
        Rect rect;

        public Coord(int c, boolean isLeft) {
            this.c = c;
            this.isLeft = isLeft;
        }

        @Override
        public int compareTo(Coord o) {
            return c > o.c ? 1 : c == o.c ? 0 : -1;
        }
    }

    static Rect enclose(List<Rect> list) {
        long topLeftX = Integer.MAX_VALUE;
        long topLeftY = Integer.MAX_VALUE;
        long bottomRightX = Integer.MIN_VALUE;
        long bottomRightY = Integer.MIN_VALUE;

        for (Rect r : list) {
            if (r.x < topLeftX)
                topLeftX = r.x;

            if (r.y < topLeftY)
                topLeftY = r.y;

            if ((r.x + r.width) > bottomRightX)
                bottomRightX = (r.x + r.width);

            if ((r.y + r.height) > bottomRightY)
                bottomRightY = (r.y + r.height);
        }
        Rect res = new Rect();
        res.x = topLeftX;
        res.y = topLeftY;
        res.width = bottomRightX - topLeftX;
        res.height = bottomRightY - topLeftY;
        return res;
    }

    static class Rect implements Comparable<Rect> {
        long x, y, width, height, number;

        @Override
        public int compareTo(Rect o) {
            return (int)(number - o.number);  //To change body of implemented methods use File | Settings | File Templates.
        }

        @Override
        public int hashCode() {
            return (int)number;
        }

        @Override
        public boolean equals(Object obj) {
            if (!(obj instanceof Rect))
                return false;
            return number == ((Rect) obj).number;    //To change body of overridden methods use File | Settings | File Templates.
        }

        @Override
        public String toString() {
            return x + " " + (x + width) + " " + y + " " + (y + height);    //To change body of overridden methods use File | Settings | File Templates.
        }

    }

    public static List<Rect> rangeSearch(AVLTreeNode node, int k1, int k2) {
        if(node == null ){
            if(root == null)
            return new ArrayList<Rect>();
            return null;
        }


        if(node.value>=k1 && node.value <= k2){
            List<Rect> l = rangeSearch(node.left,k1,k2);
            List<Rect> r = rangeSearch(node.right,k1,k2);
            if(l == null)
                l = new ArrayList<Rect>();
            Collections.sort(l);
            if(r!=null)
            for(Rect rect : r) {
                int idx= Collections.binarySearch(l,rect);
                if(idx<0)
                    l.add((~idx),rect);
            }
            int idx = Collections.binarySearch(l,node.val);
            if(idx<0)
            l.add((~idx),node.val);
            return l;
        } else if(node.value<k1){
            List<Rect> r = rangeSearch(node.right,k1,k2);

            if(node.val.y<=k1 && node.val.y + node.val.height>=k2){
                if(r == null){
                List<Rect> res = new ArrayList<Rect>();
                res.add(node.val);
                r = res;
                }
                else       {
                    int idx = Collections.binarySearch(r,node.val);
                    if(idx<0)
                    r.add(~(idx),node.val);
                }
            }
            return r;
        } else if(node.value>k2)
        {
            List<Rect> l = rangeSearch(node.left, k1,k2);
            if(node.val.y<=k1 && node.val.y + node.val.height>=k2){
                if(l == null){
                    List<Rect> res = new ArrayList<Rect>();
                    res.add(node.val);
                    l = res;
                }
                else {
                    int idx = Collections.binarySearch(l,node.val);
                    if(idx<0)
                    l.add(~idx,node.val);
                }
            }
            return l;
        }

       return null;
    }

    public static boolean isFieldGTZero(Rect r1, Rect r2) {

        if ((r1.x + r1.width == r2.x) || r2.x + r2.width == r1.x ||
                r1.y + r1.height == r2.y || r2.y + r2.height == r1.y
                )
            return false;
        return true;
    }
    static class AVLTreeNode
    {
        int value;
        AVLTreeNode left;
        AVLTreeNode right;
        AVLTreeNode parent;
        int balance;
        Rect val;

        AVLTreeNode (int val, Rect rect)
        {
            value = val;
            balance = 0;
            this.val = rect;
        }
    }
    private static void setBalance(AVLTreeNode cur) {
        cur.balance = height(cur.right)-height(cur.left);
    }
    public static void insertAVL(AVLTreeNode p, AVLTreeNode q) {
        if(p==null) {
            root=q;
        } else {
            if(q.value<p.value) {
                if(p.left==null) {
                    p.left = q;
                    q.parent = p;
                    recursiveBalance(p);
                } else {
                    insertAVL(p.left,q);
                }

            } else if(q.value>p.value) {
                if(p.right==null) {
                    p.right = q;
                    q.parent = p;
                    recursiveBalance(p);
                } else {
                    insertAVL(p.right,q);
                }
            } else {

            }
        }
    }

    public static void recursiveBalance(AVLTreeNode cur) {


        setBalance(cur);
        int balance = cur.balance;


        if(balance==-2) {

            if(height(cur.left.left)>=height(cur.left.right)) {
                cur = rotateRight(cur);
            } else {
                cur = doubleRotateLeftRight(cur);
            }
        } else if(balance==2) {
            if(height(cur.right.right)>=height(cur.right.left)) {
                cur = rotateLeft(cur);
            } else {
                cur = doubleRotateRightLeft(cur);
            }
        }

        // we did not reach the root yet
        if(cur.parent!=null) {
            recursiveBalance(cur.parent);
        } else {
           root = cur;
        }
    }




    public static void removeAVL(AVLTreeNode p,int q) {
        if(p==null) {
            // der Wert existiert nicht in diesem Baum, daher ist nichts zu tun
            return;
        } else {
            if(p.value>q)  {
                removeAVL(p.left,q);
            } else if(p.value<q) {
                removeAVL(p.right,q);
            } else if(p.value==q) {
                // we found the node in the tree.. now lets go on!
                removeFoundNode(p);
            }
        }
    }


    public static void removeFoundNode(AVLTreeNode q) {
        AVLTreeNode r;
        // at least one child of q, q will be removed directly
        if(q.left==null || q.right==null) {
            // the root is deleted
            if(q.parent==null) {
                root=null;
                q=null;
                return;
            }
            r = q;
        } else {
            // q has two children --> will be replaced by successor
            r = successor(q);
            q.value = r.value;
        }

        AVLTreeNode p;
        if(r.left!=null) {
            p = r.left;
        } else {
            p = r.right;
        }

        if(p!=null) {
            p.parent = r.parent;
        }

        if(r.parent==null) {
            root = p;
        } else {
            if(r==r.parent.left) {
                r.parent.left=p;
            } else {
                r.parent.right = p;
            }
            // balancing must be done until the root is reached.
            recursiveBalance(r.parent);
        }
        r = null;
    }


    public static  AVLTreeNode rotateLeft(AVLTreeNode n) {

        AVLTreeNode v = n.right;
        v.parent = n.parent;

        n.right = v.left;

        if(n.right!=null) {
            n.right.parent=n;
        }

        v.left = n;
        n.parent = v;

        if(v.parent!=null) {
            if(v.parent.right==n) {
                v.parent.right = v;
            } else if(v.parent.left==n) {
                v.parent.left = v;
            }
        }

        setBalance(n);
        setBalance(v);

        return v;
    }


    public static AVLTreeNode rotateRight(AVLTreeNode n) {

        AVLTreeNode v = n.left;
        v.parent = n.parent;

        n.left = v.right;

        if(n.left!=null) {
            n.left.parent=n;
        }

        v.right = n;
        n.parent = v;


        if(v.parent!=null) {
            if(v.parent.right==n) {
                v.parent.right = v;
            } else if(v.parent.left==n) {
                v.parent.left = v;
            }
        }

        setBalance(n);
        setBalance(v);

        return v;
    }

    public static AVLTreeNode doubleRotateLeftRight(AVLTreeNode u) {
        u.left = rotateLeft(u.left);
        return rotateRight(u);
    }


    public static AVLTreeNode doubleRotateRightLeft(AVLTreeNode u) {
        u.right = rotateRight(u.right);
        return rotateLeft(u);
    }

/***************************** Helper Functions ************************************/


    public static AVLTreeNode successor(AVLTreeNode q) {
        if(q.right!=null) {
            AVLTreeNode r = q.right;
            while(r.left!=null) {
                r = r.left;
            }
            return r;
        } else {
            AVLTreeNode p = q.parent;
            while(p!=null && q==p.right) {
                q = p;
                p = q.parent;
            }
            return p;
        }
    }


    private static int height(AVLTreeNode cur) {
        if(cur==null) {
            return -1;
        }
        if(cur.left==null && cur.right==null) {
            return 0;
        } else if(cur.left==null) {
            return 1+height(cur.right);
        } else if(cur.right==null) {
            return 1+height(cur.left);
        } else {
            return 1+maximum(height(cur.left),height(cur.right));
        }
    }


    private static int maximum(int a, int b) {
        if(a>=b) {
            return a;
        } else {
            return b;
        }
    }


}