Advertisement
Guest User

Untitled

a guest
Jun 20th, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <stack>
  3. #include <limits>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. struct node
  8. {
  9.     int l,r, Min, best_id;
  10.     node * lp, *rp;
  11.     node () {};
  12.     node(int x, int val, int id): l(x), r(x), Min (val), lp(NULL), rp(NULL), best_id(id) {};
  13.     node (node * left, node *right): lp(left), rp(right), l(left->l), r(right->r)
  14.     {
  15.         if (left->Min < right->Min)
  16.         {
  17.             Min=left->Min;
  18.             best_id= left->best_id;
  19.         }
  20.         else
  21.         {
  22.             Min=right->Min;
  23.             best_id= right->best_id;
  24.         }
  25.     }
  26. };
  27.  
  28. struct interval
  29. {
  30.     int l,r,id;
  31. };
  32.  
  33. node * init(interval * tab, int n)
  34. {
  35.     auto f=[](interval a,interval b) {return a.l < b.l;};
  36.     sort(tab,tab+n,f);
  37.     stack <node *> Q,P;
  38.     node * tmp1, *tmp2;
  39.     for (int i=0;i<n;i++)
  40.         Q.push(new node(tab[i].l, tab[i].r , tab[i].id));
  41.     while (true)
  42.     {
  43.         if (Q.size()==1) return Q.top();
  44.         while(Q.size()>1)
  45.         {
  46.             tmp2=Q.top();
  47.             Q.pop();
  48.             tmp1=Q.top();
  49.             Q.pop();
  50.             P.push(new node(tmp1,tmp2));
  51.         }
  52.         if (!Q.empty())
  53.         {
  54.             P.push(Q.top());
  55.             Q.pop();
  56.         }
  57.  
  58.         if(P.size()==1) return P.top();
  59.         while(P.size()>1)
  60.         {
  61.             tmp1=P.top();
  62.             P.pop();
  63.             tmp2=P.top();
  64.             P.pop();
  65.             Q.push(new node(tmp1,tmp2));
  66.         }
  67.         if (!P.empty())
  68.         {
  69.             Q.push(P.top());
  70.             P.pop();
  71.         }
  72.     }
  73. }
  74.  
  75. pair<int,int> get_min(node * root, int ql)
  76. {
  77.     if (ql > root->r)
  78.         return {INT_MAX,-1};
  79.  
  80.     if (ql <= root->l)
  81.         return {root->Min, root->best_id};
  82.  
  83.     pair <int,int> left, right;
  84.     left=get_min(root->lp,ql);
  85.     right=get_min(root->rp,ql);
  86.  
  87.     if (left.first < right.first)
  88.         return left;
  89.     return right;
  90. }
  91.  
  92. int main ()
  93. {
  94.     ios_base::sync_with_stdio(false);
  95.     int n,l,r;
  96.     node * root;
  97.     cin >> n;
  98.     interval tab[n];
  99.     for (int i=0;i<n;i++)
  100.     {
  101.         tab[i].id=i;
  102.         cin >> tab[i].l >> tab[i].r;
  103.     }
  104.     root=init(tab,n);
  105.  
  106.     cout << "intervals: " << endl;
  107.     for (int i=0;i<n;i++)
  108.     {
  109.         cout << "(id: " << tab[i].id << ") " << tab[i].l << " - " << tab[i].r << endl;
  110.     }
  111.     cout << endl;
  112.  
  113.     for (int i=0;i<n;i++)
  114.         cout << "best for " << tab[i].id << "  :  " << get_min(root, tab[i].r).second << endl;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement