Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- struct Str
- {
- int i0, i1, i2;
- Str(int _i0, int _i1, int _i2)
- {
- i0 = _i0;
- i1 = _i1;
- i2 = _i2;
- }
- };
- struct My
- {
- int i1, i2;
- My(int _i1, int _i2)
- {
- i1 = _i1;
- i2 = _i2;
- }
- };
- bool cmp2(Str a, Str b)
- {
- if(a.i0 != b.i0)
- return a.i0 < b.i0;
- else if(a.i1 != b.i1)
- return a.i1 < b.i1;
- else
- return a.i2 < b.i2;
- }
- bool cmp1(My a, My b)
- {
- if(a.i1 != b.i1)
- return a.i1 < b.i1;
- else
- return a.i2 < b.i2;
- }
- int main()
- {
- int n, m;
- cin >> n >> m;
- vector<int> ans(m + 1);
- set<My, decltype(&cmp1)> s1(cmp1);
- s1.insert({-n, 1});
- set<Str, decltype(&cmp2)> s2(cmp2);
- s2.insert(Str(1, n, 0));
- for(int i = 1; i <= m; i++)
- {
- int value;
- cin >> value;
- if(value > 0)
- {
- auto c =* s1.begin();
- c.i1 = -c.i1;
- if(c.i1 < value)
- {
- ans[i] = -1;
- cout <<- 1 << endl;
- continue;
- }
- ans[i] = c.i2;
- cout << c.i2 << endl;
- c.i1 = -c.i1;
- s1.erase(c);
- c.i1 = -c.i1;
- s2.erase(Str(c.i2, c.i1, 0));
- s2.insert(Str(c.i2, value, 1));
- if(c.i1 - value > 0)
- {
- int nw_len = c.i1 - value, l = c.i2 + value;
- s1.insert({-nw_len, l});
- s2.insert(Str(l, nw_len, 0));
- }
- }
- else
- {
- if(ans[ - value] == -1)
- continue;
- auto c = s2.lower_bound(Str(ans[ - value], -1, -1));
- int l, _size, fl;
- // Str(l, _size, fl) = *c;
- l = (c -> i0);
- _size = (c -> i1);
- fl = (c -> i2);
- auto node = s2.end();
- -- node;
- if(c != node)
- {
- ++ c;
- if(!(c -> i2))
- {
- _size += (c -> i1);
- s2.erase(c);
- s1.erase({-(c -> i1), (c -> i0)});
- }
- c = s2.lower_bound(Str(ans[ - value], -1, -1));
- }
- if(c != s2.begin())
- {
- -- c;
- if(!(c -> i2))
- {
- l = (c -> i0);
- _size += (c -> i1);
- s2.erase(c);
- s1.erase({-(c -> i1), (c -> i0)});
- }
- c = s2.lower_bound(Str(ans[-value], -1, -1));
- }
- s2.erase(c);
- s2.insert(Str(l, _size, 0));
- s1.insert({-_size, l});
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement