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
#include <iostream>
#include <stdio.h>
using namespace std;

  int nextInt() {
    int x;
    cin >> x;
    return x;
  }

   /////////////////////////////////


  struct  Node {
    int index;
    int partnerIndex;
    int coordinate;
  };

  int nNodes;
  int boundX;
  int boundY;

  Node xNodes[500000 * 2];
  Node yNodes[500000 * 2];

  int max(int a, int b) {
    return a > b ? a : b;
  }

  int mod(int mod, int value) {
    return (value + mod) % mod;
  }

  bool areSorted(Node nodes[], Node nodeA, Node nodeB) {
    return nodeA.coordinate < nodeB.coordinate
        || nodeA.coordinate == nodeB.coordinate
            && nodes[nodeA.partnerIndex].coordinate < nodes[nodeB.partnerIndex].coordinate;
  }

  void sortByCoordinate(Node nodes[], int left, int right) {
    if (left >= right) {
      return;
    }
    Node pivot = nodes[(left + right) / 2];
    int iLeft = left;
    int iRight = right;
    while (true) {
      while (areSorted(nodes, nodes[iLeft], pivot)) {
        iLeft++;
      }
      while (areSorted(nodes, pivot, nodes[iRight])) {
        iRight--;
      }
      if (iLeft < iRight) {
        int iLeftNodePartnerIndex = nodes[iLeft].partnerIndex;
        int iRightNodePartnerIndex = nodes[iRight].partnerIndex;
        nodes[iLeftNodePartnerIndex].partnerIndex = iRight;
        nodes[iRightNodePartnerIndex].partnerIndex = iLeft;

        Node swap;
        swap.coordinate = nodes[iLeft].coordinate;
        swap.index = nodes[iLeft].index;
        swap.partnerIndex = nodes[iLeft].partnerIndex;
        nodes[iLeft].coordinate = nodes[iRight].coordinate;
        nodes[iLeft].index = nodes[iRight].index;
        nodes[iLeft].partnerIndex = nodes[iRight].partnerIndex;
        nodes[iRight].coordinate = swap.coordinate;
        nodes[iRight].index = swap.index;
        nodes[iRight].partnerIndex = swap.partnerIndex;

        iLeft++;
        iRight--;
      } else {
        sortByCoordinate(nodes, left, iRight);
        sortByCoordinate(nodes, iRight + 1, right);
        break;
      }
    }
  }

  void sortByCoordinate(Node nodes[], int size) {
    sortByCoordinate(nodes, 0, size - 1);
  }

  int solve(Node nodes[], int circle) {
    int bestScore = 0;
    int scoreOnLevel[nNodes + 1];
    int level = 0;

    Node genesis = nodes[0];
    Node current = genesis;
    level++;
    scoreOnLevel[level] = 0;

    do {
      Node next = nodes[mod(circle, current.index + 1)];

      int distance = mod(circle, next.coordinate - current.coordinate);
      scoreOnLevel[level] += distance;

      bool isOpening = next.partnerIndex > next.index;
      if (isOpening) {
        level++;
        scoreOnLevel[level] = 0;
      } else {
        bestScore = max(bestScore, scoreOnLevel[level]);
        level--;
      }

      current = next;
    } while (current.index != genesis.index);
    return bestScore;
  }

  void add(int index, int a, int b, Node nodes[]) {
    int i1 = 2 * index;
    int i2 = i1 + 1;
    nodes[i1].index = i1;
    nodes[i2].index = i2;
    nodes[i1].partnerIndex = i2;
    nodes[i2].partnerIndex = i1;
    nodes[i1].coordinate = a;
    nodes[i2].coordinate = b;
  }

  int main() {
    int n;
    n = nextInt();
    nNodes = 2 * n;
    boundX = nextInt();
    boundY = nextInt();

    for (int i = 0; i < n; i++) {
      int x1, y1, x2, y2;
      x1 = nextInt();
      y1 = nextInt();
      x2 = nextInt();
      y2 = nextInt();
      add(i, x1, x2, xNodes);
      add(i, y1, y2, yNodes);
    }

    sortByCoordinate(xNodes, nNodes);
    sortByCoordinate(yNodes, nNodes);

    for (int index = 0; index < nNodes; index++) {
      xNodes[index].index = index;
      yNodes[index].index = index;
    }

    long long solutionX = solve(xNodes, boundX);
    long long solutionY = solve(yNodes, boundY);

    printf("%llu\n", solutionX * solutionY);
    return 0;
  }