Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- struct node {
- int y, value, cnt;
- node *l, *r;
- node(int value): value(value), l(nullptr), r(nullptr), y(rand() * rand()), cnt(1)
- {}
- ~node() {
- if(l) delete(l);
- if(r) delete(r);
- }
- };
- int cnt(node* t) {
- return t? t->cnt : 0;
- }
- void upd_cnt(node*& t) {
- if(!t) return;
- t->cnt = cnt(t->l) + cnt(t->r) + 1;
- }
- pair<node*, node*> split(node*& t, int x) {
- if(!t) return {nullptr, nullptr};
- int cur_key = cnt(t->l) + 1;
- if(cur_key < x) {
- auto res = split(t->r, x - cur_key);
- t->r = res.first;
- upd_cnt(t); upd_cnt(res.second);
- return {t, res.second};
- } else {
- auto res = split(t->l, x);
- t->l = res.second;
- upd_cnt(t); upd_cnt(res.second);
- return {res.first, t};
- }
- }
- node* merge(node* l, node* r) {
- if(!l || !r) return l? l : r;
- if(l->y > r->y) {
- l->r = merge(l->r, r);
- upd_cnt(l);
- return l;
- } else {
- r->l = merge(l, r->l);
- upd_cnt(r);
- return r;
- }
- }
- int exist(node* t, int x, int add = 0) {
- if(!t) return false;
- int cur_x = add + cnt(t->l);
- if(cur_x == x) return t->value;
- if(cur_x < x) return exist(t->r, x, add + cnt(t->l) + 1);
- else return exist(t->l, x, add);
- }
- void insert(node*& t, node*& n, int idx) {
- auto res = split(t, idx);
- t = merge(merge(res.first, n), res.second);
- upd_cnt(t);
- }
- void erase(node*&t, int idx, int add = 0) {
- if(!t) return;
- int cur_key = add + cnt(t->l);
- if(cur_key == idx) {
- t = merge(t->l, t->r);
- upd_cnt(t);
- return;
- }
- if(cur_key < idx) erase(t->r, idx, add + cnt(t->l) + 1);
- else erase(t->l, idx, add);
- upd_cnt(t);
- }
- struct query {
- int val, idx;
- };
- int to_start = 0;
- int used[1000001];
- int ans[1000001];
- query q[1000001];
- int main() {
- srand(time(0));
- ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
- node* root = nullptr;
- int N, m;
- cin >> N >> m;
- for(int i = 0; i < m; i++) {
- cin >> q[i].val >> q[i].idx;
- q[i].idx--;
- }
- for(int i = 0; i < N; i++) {
- node* n = new node(0);
- insert(root, n, 1);
- }
- for(int i = 0; i < m; i++) {
- int current = exist(root, q[i].idx);
- if(current && current != q[i].val) {
- cout << -1;
- return 0;
- }
- erase(root, q[i].idx);
- node* n = new node(q[i].val);
- insert(root, n, 1);
- }
- for(int i = m-1; i >= 0; i--) {
- node* n = new node(exist(root, 0));
- erase(root, 0);
- insert(root, n, q[i].idx + 1);
- }
- for(int i = 0; i < N; i++) {
- int j = exist(root, i);
- if(used[j] == true) {
- cout << -1;
- return 0;
- }
- if(j) used[j] = true;
- }
- int pos = 1;
- for(int i = 0; i < N; i++) {
- int cur = exist(root, i);
- if(!cur) {
- while(used[pos]) pos++;
- cur = pos++;
- }
- ans[i] = cur;
- }
- for(int i = 0; i < N; i++) cout << ans[i] << " ";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement