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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
#include <algorithm>
#include <map>
#include <vector>
#include <unordered_map>
#include <set>

#define LL long long
#define BIGMOD 1000012177LL
#define DNUM 31LL

#define K3DBG(X)

using namespace std;

class DuoSegment
{
public:
    LL a1, a2, b1, b2;

    DuoSegment() : a1(-1), a2(-1), b1(-1), b2(-1) {}

    DuoSegment(LL a1, LL a2, LL b1, LL b2) 
        : a1(a1), a2(a2), b1(b1), b2(b2)
    {
        simplify();
    }

    void simplify()
    {
        if (a1 >= a2)
        {
            a1 = a2 = -1;
        }
        if (b1 >= b2)
        {
            b1 = b2 = -1;
        }
        if (a1 == -1 && b1 != -1)
        {
            swap(a1, b1);
            swap(a2, b2);
        }
        if (b1 != -1 && a1 > b1)
        {
            swap(a1, b1);
            swap(a2, b2);
        }
        if (a2 == b1)
        {
            a2 = b2;
            b1 = -1;
            b2 = -1;
        }
    }

    bool operator==(const DuoSegment &peer) const
    {
        return a1 == peer.a1 && a2==peer.a2 && b1 == peer.b1 && b2 == peer.b2;
    }

    bool operator<(const DuoSegment &peer) const
    {
        if (a1 < peer.a1) {
            return true;
        }
        if (a1 == peer.a1 && a2 < peer.a2) {
            return true;
        }
        return false;
    }

    void print() const
    {
        printf("DuoSegment(%lld,%lld,%lld,%lld)L=%lld", a1, a2, b1, b2, length());
    }

    bool empty() const {
        return a1 == -1;
    }

    bool single() const {
        return a1 != -1 && b1 == -1;
    }

    bool isDuo() const {
        return a1 != -1 && b1 != -1;
    }

    LL length() const
    {
        return (a2 - a1) +  (b2 - b1);
    }

    void clear() 
    {
        a1 = a2 = b1 = b2 = -1;
    }

    pair<DuoSegment, DuoSegment> merge(DuoSegment s1, DuoSegment s2) const
    {        
        if (s1.empty()) return make_pair(s2, DuoSegment());
        if (s2.empty()) return make_pair(s1, DuoSegment());

        if (s1.single() && s2.single())
        {
            return make_pair(DuoSegment(s1.a1, s1.a2, s2.a1, s2.a2), DuoSegment());
        }

        if (s1.a1 < s2.a1)
        {
            return make_pair(s1, s2);
        }
        else
        {
            return make_pair(s2, s1);
        }
    }

    DuoSegment crossSingle(DuoSegment &peer)
    {
        if (empty() || peer.empty()) return DuoSegment();
        return DuoSegment(max(a1, peer.a1), min(a2, peer.a2), max(b1, peer.a1), min(b2, peer.a2));    
    }

    pair<DuoSegment, DuoSegment> cross(DuoSegment &peer)
    {
       if (peer.single())
       {
           return make_pair(crossSingle(peer), DuoSegment());
       }

        if (peer.isDuo())
        {
            DuoSegment s1 = DuoSegment(peer.a1, peer.a2, -1, -1);
            DuoSegment s2 = DuoSegment(peer.b1, peer.b2, -1, -1);

            return merge(crossSingle(s1), crossSingle(s2));
        }
        printf("error");
        return {};
    };
};

vector<DuoSegment> cross_multi_segments(vector<DuoSegment> &segments, DuoSegment &ds)
{
    vector<DuoSegment> res;

    for (int i=0; i < segments.size(); i++)
    {
        pair<DuoSegment, DuoSegment> newSegments = segments[i].cross(ds);
        if (!newSegments.first.empty())
        {
            res.push_back(newSegments.first);
        }
        if (!newSegments.second.empty())
        {
            res.push_back(newSegments.second);
        }
    }
    sort(res.begin(), res.end());

    /*printf("cross_multi_segments: ");
    for (int i=0; i < segments.size(); i++)
    {
        segments[i].print();printf(" ");
    }
    printf(", result=");
    for (int i=0; i < res.size(); i++)
    {
        res[i].print();printf(" ");
    }
    printf("\n");*/
    return res;
}

void print_duo_vec(vector<DuoSegment> &ds, bool newline)
{
    for (int i=0; i < ds.size(); i++)
    {
        ds[i].print();
    }
    if (newline) printf("\n");
}

LL G_MAX_X, G_MAX_Y;

LL sum_duo_vec(vector<DuoSegment> &ds)
{
    LL sum = 0;
    for (int i=0; i < ds.size(); i++)
    {
        sum += ds[i].length();
    }
    return sum;
}

LL slowSolve1dimension(vector<pair<LL, LL> > v, LL limit)
{
    vector<vector<DuoSegment> > solutions;
    sort(v.begin(), v.end());

    LL a = min(v[0].first, v[0].second);
    LL b = max(v[0].first, v[0].second);

    solutions.push_back({DuoSegment(a, b, -1, -1)});
    solutions.push_back({DuoSegment(0, a, b, limit)});

    /*printf("Solutions:\n");
    for (int k=0; k < solutions.size(); k++)
    {
        printf("solution %d ", k);
        print_duo_vec(solutions[k], true);
    }*/

    for (int i=1; i < v.size(); i++)
    {
        LL a = min(v[i].first, v[i].second);
        LL b = max(v[i].first, v[i].second);
        DuoSegment s1(a,b, -1, -1);
        DuoSegment s2(0, a, b, limit);

        //printf("Adding segment (%lld, %lld)\n", a,b);

        vector<vector<DuoSegment> > newSolutions;
        for (auto it=solutions.begin(); it != solutions.end(); ++it)
        {
            auto sol1 = cross_multi_segments(*it, s1);
            auto sol2 = cross_multi_segments(*it, s2);
            if (sol1.size())
            {
                newSolutions.push_back(sol1);
            }
            if (sol2.size())
            {
                newSolutions.push_back(sol2);
            }
        }
        sort(newSolutions.begin(), newSolutions.end());
        
        /*printf("Solutions:\n");
        for (int k=0; k < newSolutions.size(); k++)
        {
            printf("solution %d ", k);
            print_duo_vec(newSolutions[k], true);
        }*/
        solutions = newSolutions;
    }

    LL bestRes = 0;
    for (int i=0; i < solutions.size(); i++)
    {
        LL r =  sum_duo_vec(solutions[i]);
        //printf("%lld\n",r);
        //printf("solution %d ", i);
        //print_duo_vec(solutions[i], true);

        bestRes= max(bestRes, r);
    }
    return bestRes;
}


class CoordinatesMapper {
    public:
    unordered_map<LL, LL> mapper;

    CoordinatesMapper(vector<pair<LL, LL> > v, LL start, LL end)
    {
        vector<LL> ov;
        for (int i=0; i < v.size(); i++)
        {
            ov.push_back(v[i].first);
            ov.push_back(v[i].second);
        }
        ov.push_back(start);
        ov.push_back(end);

        sort(ov.begin(), ov.end());
        LL old = -1;
        LL mapId = 0;
        for (int i=0; i < ov.size(); i++)
        {
            if (ov[i] == old)
            {
                continue;
            }
            mapper[ov[i]] = mapId++;
            old = ov[i];
        }
        if (mapper.size() != mapId)
        {
            printf("ERROR!");
            int tmp;
            scanf("%d", &tmp);
        }
    }

    LL map(LL x)
    {
        LL res = mapper[x];
        //printf("mapping %lld -> %lld\n", x, res);
        return res;
    }

    LL length() 
    {
        return mapper.size();
    }
};


class BitTree {
    public:
    LL *mData;
    int mN;
    BitTree(int n) : mN(n) {
        mData = new LL[mN+1]; 
        for (int i=0; i<=mN; i++) 
        {
            mData[i] = 0; 
        }
    }
    
    // Updates a node in Binary Index Tree (BITree) at given index 
    // in BITree. The given value 'val' is added to BITree[i] and 
    // all of its ancestors in tree. 
    void updateBIT(int index, LL val) 
    { 
        // index in BITree[] is 1 more than the index in arr[] 
        index = index + 1; 
    
        // Traverse all ancestors and add 'val' 
        while (index <= mN) 
        { 
            // Add 'val' to current node of BI Tree 
            mData[index] += val; 
    
            // Update index to that of parent in update View 
            index += index & (-index); 
        }
    } 
    LL getElem(int index)
    {
        LL res = getSum(index);
        // printf("bt::getElem(%d)=%lld\n", index, res);
        return res;
    }
    // SERVES THE PURPOSE OF getElement() 
    // Returns sum of arr[0..index]. This function assumes 
    // that the array is preprocessed and partial sums of 
    // array elements are stored in BITree[] 
    LL getSum(int index) 
    { 
        LL sum = 0; // Iniialize result 
    
        // index in BITree[] is 1 more than the index in arr[] 
        index = index + 1; 
    
        // Traverse ancestors of BITree[index] 
        while (index>0) 
        { 
            // Add current element of BITree to sum 
            sum += mData[index]; 
    
            // Move index to parent node in getSum View 
            index -= index & (-index); 
        } 
        return sum; 
    } 
  
    // Updates such that getElement() gets an increased 
    // value when queried from l to r. 
    void update(int l, int r, LL val) 
    { 
        // Increase value at 'l' by 'val' 
        updateBIT(l, val); 
    
        // Decrease value at 'r+1' by 'val' 
        updateBIT(r+1, -val); 
    } 

    void print() {
        printf("BitTree=");
        for (int i=0; i < mN; i++) {
            printf("%lld ", getElem(i));
        }
        printf("\n");
    }
};


class DuoSegmentContainer
{
public:
    pair<DuoSegment, DuoSegment> data;

    DuoSegmentContainer(pair<DuoSegment, DuoSegment> data) : data(data)
    {}

    DuoSegmentContainer(DuoSegment ds) : data(make_pair(ds, DuoSegment()))
    {}

    DuoSegmentContainer(const vector<DuoSegment> &vec)
    {
        if (vec.size() == 1)
        {
            data = make_pair(vec[0], DuoSegment());
        }
        else if (vec.size() == 2)
        {
            data = make_pair(vec[0], vec[1]);
        }
    }

    bool isSpecial() 
    {
        if (!data.first.empty() && !data.second.empty()) return true;
        return data.first.isDuo();
    }

    LL length() {
        return data.first.length() + data.second.length();
    }

    bool operator<(const DuoSegmentContainer &peer) const
    {
        return data.first.a1 < peer.data.first.a1;
    }

    vector<DuoSegment> getVec() {
        if (!data.second.empty())
        {
            return {data.first, data.second};
        }
        return {data.first};
    }

    void print() const {
        printf("DuoSegmentContainer:");
        data.first.print(); 
        if (!data.second.empty())
        {
            data.second.print();
        }
        printf("\n");
    }
};


void print_bt_solutions(BitTree &solutionPtrs, LL cmMax)
{
    set<DuoSegmentContainer> res;
    for (int i=0; i < cmMax; i++)
    {
        LL ptr = solutionPtrs.getElem(i);
        if (ptr)
        {
            DuoSegmentContainer* dsPtr = (DuoSegmentContainer*)ptr;
            res.insert(*dsPtr);
        }
    }
    printf("BT solutions:\n");
    for (auto it=res.begin(); it!=res.end(); it++)
    {
        it->print(); printf("\n");
    }
    printf("\n");
}

void updateBitTreePtrs(CoordinatesMapper &cm, BitTree &solutionPtrs, const DuoSegmentContainer *dsc, LL value)
{
    //printf("updateBitTreePtrs %X value=%lld ", ds, value); ds->print(); printf("\n");
    //solutionPtrs.print();

    for (int i=0; i < 2; i++)
    {
        DuoSegment ds = (i == 0 ? dsc->data.first : dsc->data.second);
        if (ds.empty())
        {
            continue;
        }
        
        LL mapped_a1 = cm.map(ds.a1);
        LL mapped_a2 = cm.map(ds.a2)-1; 
        //printf("mapped (a1,a2)=(%lld, %lld)\n", mapped_a1, mapped_a2);
        solutionPtrs.update(mapped_a1, mapped_a2, value);
        
        if (ds.isDuo())
        {
            LL mapped_b1 = cm.map(ds.b1);
            LL mapped_b2 = cm.map(ds.b2)-1; 
            //printf("mapped (b1,b2)=(%lld, %lld)\n", mapped_b1, mapped_b2);
            solutionPtrs.update(mapped_b1, mapped_b2, value);
        }
    }
}

void updateSolutionsSet(CoordinatesMapper &cm, BitTree &solutionPtrs, vector<LL> &extraSegmentsPtrs, DuoSegment *ds1, DuoSegment *ds2)
{
    //printf("\n\nupdateSolutionsSet: "); ds1->print(); ds2->print();printf("\n");
    vector<LL> rawPtrs;
    
    rawPtrs.push_back(solutionPtrs.getElem(cm.map(ds1->a1)));
    rawPtrs.push_back(solutionPtrs.getElem(cm.map(ds1->a2)-1));
    if (ds1->isDuo())
    {
        rawPtrs.push_back(solutionPtrs.getElem(cm.map(ds1->b1)));
        rawPtrs.push_back(solutionPtrs.getElem(cm.map(ds1->b2)-1));
    }

    rawPtrs.push_back(solutionPtrs.getElem(cm.map(ds2->a1)));
    rawPtrs.push_back(solutionPtrs.getElem(cm.map(ds2->a2)-1));
    if (ds2->isDuo())
    {
        rawPtrs.push_back(solutionPtrs.getElem(cm.map(ds2->b1)));
        rawPtrs.push_back(solutionPtrs.getElem(cm.map(ds2->b2)-1));
    }

    for (int i=0; i < extraSegmentsPtrs.size(); i++)
    {
        rawPtrs.push_back(extraSegmentsPtrs[i]);
    }
    extraSegmentsPtrs.clear();

    rawPtrs.erase(std::remove(rawPtrs.begin(), rawPtrs.end(), 0), rawPtrs.end());
    sort(rawPtrs.begin(), rawPtrs.end());
    auto uniqueEndIt = unique(rawPtrs.begin(), rawPtrs.end());
    rawPtrs.resize( std::distance(rawPtrs.begin(),uniqueEndIt) );

    /*for (int i=0; i < rawPtrs.size(); ++i)
    {
        printf("p:%X ", rawPtrs[i]);
    }
    printf("\n");*/
    if (rawPtrs[0] == 0) return;
    
    for (int i=0; i < rawPtrs.size(); i++)
    {
        //printf("recSolutions: \n");
    
        DuoSegmentContainer *currentSolution = (DuoSegmentContainer*)rawPtrs[i];
        //printf("currentSolution="); currentSolution->print();printf(" ");

        updateBitTreePtrs(cm, solutionPtrs, currentSolution, -(LL)currentSolution);
        
        //printf("First cross: \n");
        vector<DuoSegment> tmp = currentSolution->getVec();
        vector<DuoSegment> crossSegments = cross_multi_segments(tmp, *ds1);

        DuoSegmentContainer* newDsc = new DuoSegmentContainer(crossSegments);
        updateBitTreePtrs(cm, solutionPtrs, newDsc, (LL)newDsc);
        if (newDsc->isSpecial())
        {
            extraSegmentsPtrs.push_back((LL)newDsc);
        }
        
        //printf("Second cross: \n");
        tmp = currentSolution->getVec();
        crossSegments = cross_multi_segments(tmp, *ds2);

        newDsc = new DuoSegmentContainer(crossSegments);
        updateBitTreePtrs(cm, solutionPtrs, newDsc, (LL)newDsc);
        if (newDsc->isSpecial())
        {
            extraSegmentsPtrs.push_back((LL)newDsc);
        }

        delete currentSolution;
        //printf("done1\n");
    }
}


LL fastSolve1dimension(vector<pair<LL, LL> > v, LL limit)
{
    sort(v.begin(), v.end());
    CoordinatesMapper cm(v, 0, limit);
    BitTree solutionPtrs(cm.length() + 2);
    vector<LL> extraSegmentsPtrs;

    LL a = min(v[0].first, v[0].second);
    LL b = max(v[0].first, v[0].second);

    DuoSegmentContainer* dsc1 = new DuoSegmentContainer(vector<DuoSegment>({DuoSegment(a, b, -1, -1)}));
    DuoSegmentContainer* dsc2 = new DuoSegmentContainer(vector<DuoSegment>({DuoSegment(0, a, b, limit)}));

    updateBitTreePtrs(cm, solutionPtrs, dsc1, (LL)dsc1);
    updateBitTreePtrs(cm, solutionPtrs, dsc2, (LL)dsc2);
    extraSegmentsPtrs.push_back((LL)dsc2);

    for (int i=1; i < v.size(); i++)
    {
        LL a = min(v[i].first, v[i].second);
        LL b = max(v[i].first, v[i].second);
        
        DuoSegment* s1 = new DuoSegment(a,b, -1, -1);
        DuoSegment* s2 = new DuoSegment(0, a, b, limit);

        updateSolutionsSet(cm, solutionPtrs, extraSegmentsPtrs, s1, s2);

        // print_bt_solutions(solutionPtrs, cm.length());
    }

    //printf("Solutions:\n");
    LL bestRes = 0;
    for (int i=0; i < cm.length(); i++)
    {
        LL ptr = solutionPtrs.getElem(i);
        if (ptr)
        {
            DuoSegmentContainer* dsPtr = (DuoSegmentContainer*)ptr;
            //dsPtr->print();printf("\n");
            bestRes= max(bestRes, dsPtr->length());
        }
    }
    return bestRes;
}


#define ASSERT_TRUE(X, Y) { if (!(X == Y)) printf(#X " != " #Y); }

void selftest() {
    BitTree bt(100);
    bt.update(2,2, 10);
    bt.update(5,6,7);

    bt.update(6,8, 2);

    for (int i=0; i < 10; i++)
    {
        printf("%d %lld\n", i, bt.getElem(i));
    }
}


int main() {
    int n;

    //selftest();

    scanf("%d%lld%lld", &n, &G_MAX_X, &G_MAX_Y);

    vector<pair<LL, LL> > xS;
    vector<pair<LL, LL> > yS;
    for (int i=0; i < n; i++)
    {
        LL x1,y1,x2,y2;
        scanf("%lld%lld%lld%lld", &x1, &y1, &x2, &y2);
        xS.push_back(make_pair(x1, x2));
        yS.push_back(make_pair(y1, y2));
    }

    bool useSlow = true;
    if (useSlow && (xS.size() < 5000))
    {
            LL A = slowSolve1dimension(xS, G_MAX_X);
            LL B = slowSolve1dimension(yS, G_MAX_Y);
            printf("%lld\n", A*B);
    }
    else
    {
        LL A = fastSolve1dimension(xS, G_MAX_X);
        LL B = fastSolve1dimension(yS, G_MAX_Y);
        printf("%lld\n", A*B);
    }

    return 0;
}