skimono

Manager

Dec 5th, 2021 (edited)
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.05 KB | None | 0 0
  1. #pragma GCC optimize("Ofast")
  2. #pragma GCC target("avx2")
  3. #define _CRT_SECURE_NO_WARNINGS
  4. #include <iostream>
  5. #include <vector>
  6. #include <string>
  7. #include <algorithm>
  8. #include <cmath>
  9. #include <stack>
  10. #include <iomanip>
  11. #include <fstream>
  12. #include <string>
  13. #include <set>
  14. #include <deque>
  15. #include <queue>
  16. #include <map>
  17. #include <bitset>
  18. #include <random>
  19.  
  20. using namespace std;
  21.  
  22. typedef long long ll;
  23. typedef unsigned long long ull;
  24. typedef long double ld;
  25. typedef string str;
  26. //typedef __int128 ultraint;
  27. #define endl "\n"
  28. #define sqrt sqrtl
  29.  
  30. //#define pow fast_pow
  31.  
  32. const ll inf = 1e17 + 13;
  33. ld eps = 1e-6;
  34. ll mod = 1e9 + 7;
  35.  
  36. auto cmp = [](const pair<ll, ll>& a, const pair<ll, ll>& b) -> bool {
  37.     return a.first > b.first || (a.first == b.first && a.second < b.second);
  38. };
  39.  
  40. signed main() {
  41. #ifdef _DEBUG
  42.     freopen("input.txt", "r", stdin);
  43.     freopen("output.txt", "w", stdout);
  44. #endif
  45.     ios_base::sync_with_stdio(0);
  46.     cin.tie(NULL);
  47.     cout.tie(NULL);
  48.     ll n, m, i, j, x;
  49.     cin >> n >> m;
  50.     set<pair<ll, ll>> mem1;
  51.     set<pair<ll, ll>, decltype(cmp)> mem2(cmp);
  52.     mem1.insert({ 1, n });
  53.     mem2.insert({ n, 1 });
  54.     vector<ll> mem(m, -1);
  55.     vector <ll> bl(m);
  56.     for (i = 0; i < m; i++) {
  57.         cin >> x;
  58.         if (x > 0) {
  59.             bl[i] = x;
  60.             if (!mem2.empty()) {
  61.                 ll len = mem2.begin()->first;
  62.                 ll pos = mem2.begin()->second;
  63.                 if (len >= x) {
  64.                     mem2.erase(mem2.begin());
  65.                     mem1.erase({ pos, len });
  66.                     mem2.insert({ len - x, pos + x });
  67.                     mem1.insert({ pos + x, len - x });
  68.                     mem[i] = pos;
  69.                 }
  70.             }
  71.         }
  72.         else {
  73.             bl[i] = -inf;
  74.             x = -x;
  75.             x--;
  76.             if (mem[x] != -1) {
  77.                 ll len = bl[x];
  78.                 ll pos = mem[x];
  79.                 auto sup = mem1.lower_bound({ pos, len });
  80.                 if (sup != mem1.begin()) {
  81.                     auto it_prev = prev(sup);
  82.                     ll pos_prev = it_prev->first;
  83.                     ll len_prev = it_prev->second;
  84.                     if (pos_prev + len_prev == pos) {
  85.                         len += len_prev;
  86.                         pos = pos_prev;
  87.                         mem1.erase(it_prev);
  88.                         mem2.erase({ len_prev, pos_prev });
  89.                     }
  90.                 }
  91.                 if (sup != mem1.end()) {
  92.                     ll pos_next = sup->first;
  93.                     ll len_next = sup->second;
  94.                     if (pos + len == pos_next) {
  95.                         len += len_next;
  96.                         mem1.erase(sup);
  97.                         mem2.erase({ len_next, pos_next });
  98.                     }
  99.                 }
  100.                 mem1.insert({ pos, len });
  101.                 mem2.insert({ len, pos });
  102.             }
  103.         }
  104.     }
  105.     for (i = 0; i < m; i++) {
  106.         if (bl[i] != -inf) {
  107.             cout << mem[i] << endl;
  108.         }
  109.     }
  110. }
  111.  
Add Comment
Please, Sign In to add comment