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

int tab[100000][4];
int pom[100000][4];

void merge(int start, int srodek, int koniec, int index)
{
   int i = start, j = srodek+1, k = 0; // zmienne pomocnicze
 
   while (i <= srodek && j <= koniec)
   {
      if (tab[j][index] < tab[i][index])
      {
         pom[k][0] = tab[j][0];
         pom[k][1] = tab[j][1];
         pom[k][2] = tab[j][2];
         pom[k][3] = tab[j][3];
         j++;
      }
      else
      {
         pom[k][0] = tab[i][0];
         pom[k][1] = tab[i][1];
         pom[k][2] = tab[i][2];
         pom[k][3] = tab[i][3];
         i++;
      }
      k++;
   }
 
   if (i <= srodek)
   {
   while (i <= srodek)
      {
         pom[k][0] = tab[i][0];
         pom[k][1] = tab[i][1];
         pom[k][2] = tab[i][2];
         pom[k][3] = tab[i][3];
         i++;
         k++;
      }
   }
   else
   {
      while (j <= koniec)
      {
         pom[k][0] = tab[j][0];
         pom[k][1] = tab[j][1];
         pom[k][2] = tab[j][2];
         pom[k][3] = tab[j][3];
         j++;
         k++;
      }
   }
 
   for (i = 0; i <= koniec-start; i++)
   {
      tab[start+i][0] = pom[i][0]; 
      tab[start+i][1] = pom[i][1]; 
      tab[start+i][2] = pom[i][2]; 
      tab[start+i][3] = pom[i][3];
   }
}
 
void merge_sort(int start, int koniec, int index)
{
   int srodek;
 
   if (start != koniec)
   {
      srodek = (start + koniec)/2;
      merge_sort(start, srodek, index);
      merge_sort(srodek+1, koniec, index);
      merge(start, srodek, koniec, index);
   }
}


int main ()
{
   ios_base::sync_with_stdio(0);
   int p; //ilość plemion
   cin>>p;
   for (int i=0; i<p; i++) //wczytywanie
   {
      cin>> tab[i][0];
      cin>> tab[i][1];
      cin>> tab[i][2];
      cin>> tab[i][3];
   }
   
//   koksik!
   for (int i=0; i<p; i++)
   {
      for (int j=0; j<p; j++)
      {
         if(i == j)
            continue;
         if(tab[i][0] >= tab[j][1] || tab[j][0] >= tab[i][1] || tab[i][2] >= tab[j][3] || tab[j][2] >= tab[i][3])
         {
            continue; //rozdzielne
         }
         
         if(tab[i][0] > tab[j][0])
            tab[i][0] = tab[j][0];
         if(tab[i][2] > tab[j][2])
            tab[i][2] = tab[j][2];
         if(tab[i][1] < tab[j][1])
            tab[i][1] = tab[j][1];
         if(tab[i][3] < tab[j][3])
            tab[i][3] = tab[j][3];
         
         p--;
         if(i > j)
            i--;
         
         i--;
         
         for( ; j < p; j++)
         {
            tab[j][0] = tab[j + 1][0];
            tab[j][1] = tab[j + 1][1];
            tab[j][2] = tab[j + 1][2];
            tab[j][3] = tab[j + 1][3];
         }
      }
   }
   
   merge_sort(0, p - 1, 3);
   merge_sort(0, p - 1, 2);
   merge_sort(0, p - 1, 1);
   merge_sort(0, p - 1, 0);
   
   cout << p << endl;
   
   for (int i=0; i<p; i++) //wczytywanie
   {
      cout << tab[i][0] << " ";
      cout << tab[i][1] << " ";
      cout << tab[i][2] << " ";
      cout << tab[i][3] << endl;
   }
}