#include <iostream> #include <vector> #include <algorithm> using namespace std; bool DEBUGER = false; struct Node { long long int begin, end; vector<Node*> children; }; void sortVector(vector<Node*> &vectorToSort) { sort(vectorToSort.begin(), vectorToSort.end(), [](const Node* a, const Node* b) -> bool { return (a->end - a->begin) > (b->end - b->begin); }); } //void addInterval(Node* root, Node* toAdd) //{ //// for(int i = 0; i < root->children.size(); i++) //// { //// //// } // if(DEBUGER)cout<<"brzegi roota "<<root->begin<<", "<<root->end<<" oraz brzegi drugiego "<<toAdd->begin<< ", " << toAdd->end<<endl; // if(toAdd->begin >= toAdd->end or (toAdd->begin == root->begin and toAdd->end == root->end)) // return; // if(root->children.empty()) { // toAdd->begin = max(toAdd->begin, root->begin); // toAdd->end = min(toAdd->end, root->end); // if(DEBUGER)cout<<"-1"<<endl; // root->children.push_back(toAdd); // return; // } // // int i = 0; // auto iter = root->children.begin(); // while(root->children[i]->end <= toAdd->begin) // { // i++; // if(root->children.size() == i) // { // if(DEBUGER)cout<<"0"<<endl; // root->children.push_back(toAdd); // return; // } // iter++; // } // if(toAdd->end <= root->children[i]->begin) // { //// cout<<"tutaj"<<toAdd->begin<<" " << toAdd->end<<endl; // if(DEBUGER)cout<<"1"<<endl; // root->children.insert(iter, toAdd); // return; // } // if(root->children[i]->end >= toAdd->end and root->children[i]->begin <= toAdd->begin) // { //// cout<<" a jednak tutaj"<<toAdd->begin<<" " << toAdd->end<<endl; // if(DEBUGER)cout<<"2"<<endl; // addInterval(root->children[i], toAdd); // return; // } // if(toAdd->begin < root->children[i]->begin) // { // if(DEBUGER)cout<<"3"<<endl; // Node* pom = new Node; // pom->begin = toAdd->begin; // pom->end = root->children[i]->begin; // toAdd->begin = pom->end; // addInterval(root->children[i], toAdd); // root->children.insert(iter, pom); // return; // } // // // Node* newNode = new Node; // newNode->begin = toAdd->begin; // newNode->end = root->children[i]->end; // addInterval(root->children[i], newNode); // toAdd->begin = root->children[i]->end; // i++; // if(i == root->children.size()) // { // if(DEBUGER)cout<<"4"<<endl; // root->children.push_back(toAdd); // return; // } // iter++; // if(toAdd->begin <= root->children[i]->begin){ // if(DEBUGER)cout<<"5"<<endl; // Node* secondNewNode = new Node; // secondNewNode->begin = toAdd->begin; // secondNewNode->end = min(root->children[i]->begin, toAdd->end); // toAdd->begin = root->children[i]->begin; // addInterval(root->children[i], toAdd); // if(secondNewNode->begin < secondNewNode->end) // root->children.insert(iter, secondNewNode); // return; // } // //} void addInterval(Node* root, Node* toAdd, int whereStart=0) { if(DEBUGER)cout<<"brzegi roota "<<root->begin<<", "<<root->end<<" oraz brzegi drugiego "<<toAdd->begin<< ", " << toAdd->end<<endl; if(toAdd->begin >= toAdd->end or (toAdd->begin == root->begin and toAdd->end == root->end)) return; if(root->children.empty()) { toAdd->begin = max(toAdd->begin, root->begin); toAdd->end = min(toAdd->end, root->end); if(DEBUGER)cout<<"-1"<<endl; root->children.push_back(toAdd); return; } int i = whereStart; auto iter = root->children.begin() + whereStart; while(root->children[i]->end <= toAdd->begin) { i++; if(root->children.size() == i) { if(DEBUGER)cout<<"0"<<endl; root->children.push_back(toAdd); return; } iter++; } if(toAdd->begin < root->children[i]->begin) { Node* pom = new Node; pom->end = toAdd->end; pom->begin = min(root->children[i]->begin, toAdd->end); toAdd->end = min(root->children[i]->begin, toAdd->end); addInterval(root, pom, i); root->children.insert(iter, toAdd); return; } Node* pom = new Node; pom->end = toAdd->end; pom->begin = min(root->children[i]->end, toAdd->end); toAdd->end = min(root->children[i]->end, toAdd->end); addInterval(root->children[i], toAdd); addInterval(root, pom, i); // root->children.insert(iter, toAdd); } void printTree(Node* root) { if(DEBUGER)cout<<root->begin<<' '<<root->end<<" number of children: "<<root->children.size()<<endl; for(auto & i : root->children) printTree(i); } long long int maxInterval(Node* root) { long long int maxRootInterval = root->end - root->begin; long long int result = 0; for(int i = 0; i < root->children.size(); i++) { result = max(result, maxInterval(root->children[i])); maxRootInterval -= (root->children[i]->end - root->children[i]->begin); } if(DEBUGER)cout<<root->begin<<" "<<root->end<<" "<<maxRootInterval<<endl; return max(maxRootInterval, result); } long long int algorithmForOneAx(vector<Node*> nodes, long long int size) { Node* nodeX = new Node; nodeX->begin = 0; nodeX->end = size; sortVector(nodes); for(auto & node : nodes) { addInterval(nodeX, node); if(DEBUGER)printTree(nodeX); if(DEBUGER)cout<<"-------------------------------------------------"<<endl; } // printTree(nodeX); return maxInterval(nodeX); } int main() { ios_base::sync_with_stdio(false); long long int numberOfAnimals, sizeX, sizeY; cin >> numberOfAnimals >> sizeX >> sizeY; vector<Node*> nodeX; vector<Node*> nodeY; for(int i = 0; i < numberOfAnimals; i++) { long long int x1, x2, y1, y2; cin>> x1 >> y1 >> x2 >> y2; Node* newNode = new Node; newNode->end = max(x1, x2); newNode->begin = min(x1, x2); nodeX.push_back(newNode); newNode = new Node; newNode->begin = min(y1, y2); newNode->end = max(y1, y2); nodeY.push_back(newNode); } long long int ile = algorithmForOneAx(nodeX, sizeX); long long int ile2 = 0; if(!DEBUGER) { ile2 = algorithmForOneAx(nodeY, sizeY); long long int maxField = ile * ile2; cout<<maxField<<endl; } if(DEBUGER)cout<<ile<<" " << ile2<<endl; // long long int maxField = algorithmForOneAx(nodeY, sizeY); return 0; }
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 | #include <iostream> #include <vector> #include <algorithm> using namespace std; bool DEBUGER = false; struct Node { long long int begin, end; vector<Node*> children; }; void sortVector(vector<Node*> &vectorToSort) { sort(vectorToSort.begin(), vectorToSort.end(), [](const Node* a, const Node* b) -> bool { return (a->end - a->begin) > (b->end - b->begin); }); } //void addInterval(Node* root, Node* toAdd) //{ //// for(int i = 0; i < root->children.size(); i++) //// { //// //// } // if(DEBUGER)cout<<"brzegi roota "<<root->begin<<", "<<root->end<<" oraz brzegi drugiego "<<toAdd->begin<< ", " << toAdd->end<<endl; // if(toAdd->begin >= toAdd->end or (toAdd->begin == root->begin and toAdd->end == root->end)) // return; // if(root->children.empty()) { // toAdd->begin = max(toAdd->begin, root->begin); // toAdd->end = min(toAdd->end, root->end); // if(DEBUGER)cout<<"-1"<<endl; // root->children.push_back(toAdd); // return; // } // // int i = 0; // auto iter = root->children.begin(); // while(root->children[i]->end <= toAdd->begin) // { // i++; // if(root->children.size() == i) // { // if(DEBUGER)cout<<"0"<<endl; // root->children.push_back(toAdd); // return; // } // iter++; // } // if(toAdd->end <= root->children[i]->begin) // { //// cout<<"tutaj"<<toAdd->begin<<" " << toAdd->end<<endl; // if(DEBUGER)cout<<"1"<<endl; // root->children.insert(iter, toAdd); // return; // } // if(root->children[i]->end >= toAdd->end and root->children[i]->begin <= toAdd->begin) // { //// cout<<" a jednak tutaj"<<toAdd->begin<<" " << toAdd->end<<endl; // if(DEBUGER)cout<<"2"<<endl; // addInterval(root->children[i], toAdd); // return; // } // if(toAdd->begin < root->children[i]->begin) // { // if(DEBUGER)cout<<"3"<<endl; // Node* pom = new Node; // pom->begin = toAdd->begin; // pom->end = root->children[i]->begin; // toAdd->begin = pom->end; // addInterval(root->children[i], toAdd); // root->children.insert(iter, pom); // return; // } // // // Node* newNode = new Node; // newNode->begin = toAdd->begin; // newNode->end = root->children[i]->end; // addInterval(root->children[i], newNode); // toAdd->begin = root->children[i]->end; // i++; // if(i == root->children.size()) // { // if(DEBUGER)cout<<"4"<<endl; // root->children.push_back(toAdd); // return; // } // iter++; // if(toAdd->begin <= root->children[i]->begin){ // if(DEBUGER)cout<<"5"<<endl; // Node* secondNewNode = new Node; // secondNewNode->begin = toAdd->begin; // secondNewNode->end = min(root->children[i]->begin, toAdd->end); // toAdd->begin = root->children[i]->begin; // addInterval(root->children[i], toAdd); // if(secondNewNode->begin < secondNewNode->end) // root->children.insert(iter, secondNewNode); // return; // } // //} void addInterval(Node* root, Node* toAdd, int whereStart=0) { if(DEBUGER)cout<<"brzegi roota "<<root->begin<<", "<<root->end<<" oraz brzegi drugiego "<<toAdd->begin<< ", " << toAdd->end<<endl; if(toAdd->begin >= toAdd->end or (toAdd->begin == root->begin and toAdd->end == root->end)) return; if(root->children.empty()) { toAdd->begin = max(toAdd->begin, root->begin); toAdd->end = min(toAdd->end, root->end); if(DEBUGER)cout<<"-1"<<endl; root->children.push_back(toAdd); return; } int i = whereStart; auto iter = root->children.begin() + whereStart; while(root->children[i]->end <= toAdd->begin) { i++; if(root->children.size() == i) { if(DEBUGER)cout<<"0"<<endl; root->children.push_back(toAdd); return; } iter++; } if(toAdd->begin < root->children[i]->begin) { Node* pom = new Node; pom->end = toAdd->end; pom->begin = min(root->children[i]->begin, toAdd->end); toAdd->end = min(root->children[i]->begin, toAdd->end); addInterval(root, pom, i); root->children.insert(iter, toAdd); return; } Node* pom = new Node; pom->end = toAdd->end; pom->begin = min(root->children[i]->end, toAdd->end); toAdd->end = min(root->children[i]->end, toAdd->end); addInterval(root->children[i], toAdd); addInterval(root, pom, i); // root->children.insert(iter, toAdd); } void printTree(Node* root) { if(DEBUGER)cout<<root->begin<<' '<<root->end<<" number of children: "<<root->children.size()<<endl; for(auto & i : root->children) printTree(i); } long long int maxInterval(Node* root) { long long int maxRootInterval = root->end - root->begin; long long int result = 0; for(int i = 0; i < root->children.size(); i++) { result = max(result, maxInterval(root->children[i])); maxRootInterval -= (root->children[i]->end - root->children[i]->begin); } if(DEBUGER)cout<<root->begin<<" "<<root->end<<" "<<maxRootInterval<<endl; return max(maxRootInterval, result); } long long int algorithmForOneAx(vector<Node*> nodes, long long int size) { Node* nodeX = new Node; nodeX->begin = 0; nodeX->end = size; sortVector(nodes); for(auto & node : nodes) { addInterval(nodeX, node); if(DEBUGER)printTree(nodeX); if(DEBUGER)cout<<"-------------------------------------------------"<<endl; } // printTree(nodeX); return maxInterval(nodeX); } int main() { ios_base::sync_with_stdio(false); long long int numberOfAnimals, sizeX, sizeY; cin >> numberOfAnimals >> sizeX >> sizeY; vector<Node*> nodeX; vector<Node*> nodeY; for(int i = 0; i < numberOfAnimals; i++) { long long int x1, x2, y1, y2; cin>> x1 >> y1 >> x2 >> y2; Node* newNode = new Node; newNode->end = max(x1, x2); newNode->begin = min(x1, x2); nodeX.push_back(newNode); newNode = new Node; newNode->begin = min(y1, y2); newNode->end = max(y1, y2); nodeY.push_back(newNode); } long long int ile = algorithmForOneAx(nodeX, sizeX); long long int ile2 = 0; if(!DEBUGER) { ile2 = algorithmForOneAx(nodeY, sizeY); long long int maxField = ile * ile2; cout<<maxField<<endl; } if(DEBUGER)cout<<ile<<" " << ile2<<endl; // long long int maxField = algorithmForOneAx(nodeY, sizeY); return 0; } |