Advertisement
Georgiy1108

Untitled

Jan 12th, 2020
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.02 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. struct Str
  6. {
  7.     int i0, i1, i2;
  8.     Str(int _i0, int _i1, int _i2)
  9.     {
  10.         i0 = _i0;
  11.         i1 = _i1;
  12.         i2 = _i2;
  13.     }
  14. };
  15.  
  16. struct My
  17. {
  18.     int i1, i2;
  19.     My(int _i1, int _i2)
  20.     {
  21.         i1 = _i1;
  22.         i2 = _i2;
  23.     }
  24. };
  25.  
  26. bool cmp2(Str a, Str b)
  27. {
  28.     if(a.i0 != b.i0)
  29.         return a.i0 < b.i0;
  30.     else if(a.i1 != b.i1)
  31.         return a.i1 < b.i1;
  32.     else
  33.         return a.i2 < b.i2;
  34. }
  35.  
  36. bool cmp1(My a, My b)
  37. {
  38.     if(a.i1 != b.i1)
  39.         return a.i1 < b.i1;
  40.     else
  41.         return a.i2 < b.i2;
  42. }
  43.  
  44. int main()
  45. {
  46.     int n, m;
  47.     cin >> n >> m;
  48.     vector<int> ans(m + 1);
  49.     set<My, decltype(&cmp1)> s1(cmp1);
  50.     s1.insert({-n, 1});
  51.     set<Str, decltype(&cmp2)> s2(cmp2);
  52.     s2.insert(Str(1, n, 0));
  53.     for(int i = 1; i <= m; i++)
  54.     {
  55.         int value;
  56.         cin >> value;
  57.         if(value > 0)
  58.         {
  59.             auto c =* s1.begin();
  60.             c.i1 = -c.i1;
  61.             if(c.i1 < value)
  62.             {
  63.                 ans[i] = -1;
  64.                 cout <<- 1 << endl;
  65.                 continue;
  66.             }
  67.             ans[i] = c.i2;
  68.             cout << c.i2 << endl;
  69.             c.i1 = -c.i1;
  70.             s1.erase(c);
  71.             c.i1 = -c.i1;
  72.             s2.erase(Str(c.i2, c.i1, 0));
  73.             s2.insert(Str(c.i2, value, 1));
  74.             if(c.i1 - value > 0)
  75.             {
  76.                 int nw_len = c.i1 - value, l = c.i2 + value;
  77.                 s1.insert({-nw_len, l});
  78.                 s2.insert(Str(l, nw_len, 0));
  79.             }
  80.         }
  81.         else
  82.         {
  83.             if(ans[ - value] == -1)
  84.                 continue;
  85.             auto c = s2.lower_bound(Str(ans[ - value], -1, -1));
  86.             int l, _size, fl;
  87. //          Str(l, _size, fl) = *c;
  88.             l = (c -> i0);
  89.             _size = (c -> i1);
  90.             fl = (c -> i2);
  91.             auto node = s2.end();
  92.             -- node;
  93.             if(c != node)
  94.             {
  95.                 ++ c;
  96.                 if(!(c -> i2))
  97.                 {
  98.                     _size += (c -> i1);
  99.                     s2.erase(c);
  100.                     s1.erase({-(c -> i1), (c -> i0)});
  101.                 }
  102.                 c = s2.lower_bound(Str(ans[ - value], -1, -1));
  103.             }
  104.             if(c != s2.begin())
  105.             {
  106.                 -- c;
  107.                 if(!(c -> i2))
  108.                 {
  109.                     l = (c -> i0);
  110.                     _size += (c -> i1);
  111.                     s2.erase(c);
  112.                     s1.erase({-(c -> i1), (c -> i0)});
  113.                 }
  114.                 c = s2.lower_bound(Str(ans[-value], -1, -1));
  115.             }
  116.             s2.erase(c);
  117.             s2.insert(Str(l, _size, 0));
  118.             s1.insert({-_size, l});
  119.         }
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement