ivolff

LabASD ST G

Apr 21st, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.89 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4.  
  5. struct line {
  6.     int X;
  7.     int type;
  8.     int UY, DY;
  9. };
  10.  
  11. struct node {
  12.     node *left = nullptr;
  13.     node *right = nullptr;
  14.     int max, tAdd, L, R;
  15.  
  16.     node(int L, int R) {
  17.         this->L = L;
  18.         this->R = R;
  19.         max = 0;
  20.         tAdd = 0;
  21.     }
  22. };
  23.  
  24. void build_tree(node *cur, int deep, int cur_deep, int L, int R) {
  25.     if (cur_deep != deep) {
  26.         cur->left = new node(L, (L + R) / 2);
  27.         cur->right = new node((L + R) / 2, R);
  28.         build_tree(cur->left, deep, cur_deep + 1, L, (R + L) / 2);
  29.         build_tree(cur->right, deep, cur_deep + 1, (L + R) / 2, R);
  30.     }
  31. }
  32.  
  33. void push(node *cur) {
  34.     if (cur->tAdd) {
  35.         if (cur->left != nullptr) {
  36.             cur->left->tAdd += cur->tAdd;
  37.             cur->right->tAdd += cur->tAdd;
  38.         }
  39.         cur->max += cur->tAdd;
  40.         cur->tAdd = 0;
  41.     }
  42. }
  43.  
  44. void add(node *cur, int new_val, int L, int R) {
  45.     push(cur);
  46.     if (cur->L >= L && cur->R <= R) {
  47.         cur->tAdd += new_val;
  48.         push(cur);
  49.         return;
  50.     }
  51.     if (cur->L >= R || cur->R <= L)
  52.         return;
  53.     add(cur->left, new_val, L, R);
  54.     add(cur->right, new_val, L, R);
  55.     cur->max = std::max(cur->left->max, cur->right->max);
  56. }
  57.  
  58. std::pair<int, int> get_max(node *cur) {
  59.     push(cur);
  60.     if (cur->right != nullptr) {
  61.         if (cur->max == cur->left->max + cur->left->tAdd)
  62.             return get_max(cur->left);
  63.         else
  64.             return get_max(cur->right);
  65.     }
  66.     return std::make_pair(cur->max,cur->L);
  67. };
  68.  
  69. bool comp(line A, line B) {
  70.     if (A.X == B.X)
  71.         if (A.type==1)
  72.             return true;
  73.         else if (B.type==1)
  74.             return false;
  75.         else
  76.             return true;
  77.     return A.X < B.X;
  78. }
  79.  
  80. int main() {
  81.     int n, min_y = INT_MAX, max_y = INT_MIN;
  82.     node *head;
  83.     std::vector<line> data;
  84.     std::cin >> n;
  85.     line tmp, tmp0;
  86.     tmp.type = 1;
  87.     tmp0.type = -1;
  88.     for (int i = 0; i < n; i++) {
  89.         std::cin >> tmp.X >> tmp.UY >> tmp0.X >> tmp.DY;
  90.         tmp0.UY = tmp.UY;
  91.         tmp0.DY = tmp.DY;
  92.         max_y = std::max(max_y, tmp.DY);
  93.         min_y = std::min(min_y, tmp.UY);
  94.         data.push_back(tmp);
  95.         data.push_back(tmp0);
  96.     }
  97.     n = max_y - min_y + 1;
  98.     int deep = 1;
  99.     while (!((1 << deep - 1) >= n)) {
  100.         deep++;
  101.     }
  102.     std::stable_sort(data.begin(), data.end(), comp);
  103.     head = new node(0, 1 << deep);
  104.     build_tree(head, deep, 0, 0, 1 << deep);
  105.     std::pair<int,int> ans;
  106.     int maxX,maxY,max=INT_MIN;
  107.     for (int i = 0; i < data.size(); i++) {
  108.         add(head, data[i].type, data[i].UY-min_y, data[i].DY + 1-min_y);
  109.         ans=get_max(head);
  110.         if(max<ans.first){
  111.             max=ans.first;
  112.             maxY=ans.second+min_y;
  113.             maxX=data[i].X;
  114.         }
  115.     }
  116.     std::cout<<max<<"\n"<<maxX<<" "<<maxY;
  117. }
Add Comment
Please, Sign In to add comment