Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <string>
- #include <ctime>
- #include <fstream>
- #include <vector>
- #include <iostream>
- using namespace std;
- vector<int> res;
- struct Node {
- int Y, size, data, empty;
- Node *Left, *Right;
- Node(int y, int d, int emp = 0) {
- empty = emp;
- Y = y;
- data = d;
- size = 1;
- Left = Right = nullptr;
- }
- };
- int getSize(Node *treap) {
- return treap ? treap->size : 0;
- }
- void updSize(Node *treap) {
- if (treap) treap->size = getSize(treap->Left) + getSize(treap->Right) + 1;
- }
- int getEmptyCnt(Node *treap) {
- return treap ? treap->empty : 0;
- }
- void updEmptyCnt(Node *treap) {
- if (treap)
- treap->empty = getEmptyCnt(treap->Left) + getEmptyCnt(treap->Right) + (treap->data == 0 ? 1 : 0);
- }
- void bigUpd(Node *treap) {
- updSize(treap);
- updEmptyCnt(treap);
- }
- void veryBigUpd(Node *&treap) {
- if (treap == nullptr) return;
- veryBigUpd(treap->Left);
- veryBigUpd(treap->Right);
- bigUpd(treap);
- }
- void split(Node *treap, int pos, Node *&lRes, Node *&rRes) {
- if (treap == nullptr) {
- lRes = rRes = nullptr;
- return;
- }
- int XX = getSize(treap->Left) + 1;
- if (pos <= XX) {
- split(treap->Left, pos, lRes, treap->Left);
- rRes = treap;
- } else {
- split(treap->Right, pos - XX, treap->Right, rRes);
- lRes = treap;
- }
- bigUpd(treap);
- }
- void merge(Node *&treap, Node *l, Node *r) {
- if (!l || !r) {
- treap = l ? l : r;
- } else if (l->Y > r->Y) {
- merge(l->Right, l->Right, r);
- treap = l;
- } else {
- merge(r->Left, l, r->Left);
- treap = r;
- }
- bigUpd(treap);
- }
- void delNextZero(Node *&treap) {
- if (treap == nullptr) return;
- if (getEmptyCnt(treap->Left) != 0) {
- delNextZero(treap->Left);
- } else if (treap->data == 0) {
- merge(treap, treap->Left, treap->Right);
- } else if (getEmptyCnt(treap->Right) != 0) {
- delNextZero(treap->Right);
- }
- bigUpd(treap);
- }
- void printTreap(Node *&treap) {
- if (treap == nullptr) return;
- printTreap(treap->Left);
- // cout << treap->data << " ";
- res.push_back(treap->data);
- printTreap(treap->Right);
- }
- void insertWithDelNextZero(Node *&treap, int pos, Node *toIns) {
- Node *t1, *t2;
- split(treap, pos, t1, t2);
- // veryBigUpd(t2);
- delNextZero(t2);
- merge(treap, t1, toIns);
- merge(treap, treap, t2);
- }
- int main() {
- srand(time(nullptr));
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- Node *treap = nullptr;
- // for (int j = 0; j < 100; ++j) {
- // treap = nullptr;
- // int n, m;
- // ifstream cin("input.txt");
- // cin >> n >> m;
- // for (int i = 0; i < m; ++i) {
- // merge(treap, treap, new Node(rand(), 0, 1));
- // bigUpd(treap);
- // }
- // for (int i = 1; i <= n; ++i) {
- // int x;
- // cin >> x;
- // insertWithDelNextZero(treap, x, new Node(rand(), i, 0));
- //// printTreap(treap);
- //// cout << endl;
- // }
- // printTreap(treap);
- // cout << endl;
- // }
- // cout << endl;
- int n, m;
- // ifstream cin("input.txt");
- cin >> n >> m;
- for (int i = 0; i < m + 1; ++i) {
- merge(treap, treap, new Node(rand(), 0, 1));
- bigUpd(treap);
- }
- for (int i = 1; i <= n; ++i) {
- int x;
- cin >> x;
- insertWithDelNextZero(treap, x, new Node(rand(), i, 0));
- // veryBigUpd(treap);
- // printTreap(treap);
- // cout << endl;
- }
- printTreap(treap);
- int j;
- for (j = res.size() - 1; j >= 0; j--) {
- if (res[j] != 0) break;
- }
- cout << j + 1 << endl;
- for (int i = 0; i < j + 1; ++i) {
- cout << res[i] << " ";
- }
- // cout << getSize(treap) << endl;
- // printTreap(treap);
- // cout << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment