Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int INF = 1e9 + 7;
- const long long MOD = 1e9;
- struct node {
- int key, prior, size;
- long long sum;
- node* l;
- node* r;
- node (int x) {
- key = x;
- prior = rand();
- r = l = nullptr;
- size = 1;
- sum = x;
- }
- };
- node* root;
- long long get_sum(node* v) {
- return v == nullptr ? 0 : v->sum;
- }
- int get_size(node* v) {
- return v == nullptr ? 0 : v->size;
- }
- void recalc(node* v) {
- if (v == nullptr)
- return;
- v->size = get_size(v->l) + get_size(v->r) + 1;
- v->sum = get_sum(v->l) + get_sum(v->r) + v->key;
- }
- node* merge(node* l, node* r) {
- if (l == nullptr)
- return r;
- if (r == nullptr)
- return l;
- if (l->prior > r->prior) {
- l->r = merge(l->r, r);
- recalc(l);
- return l;
- }
- r->l = merge(l, r->l);
- recalc(r);
- return r;
- }
- pair<node*, node*> split(node* v, int key) {
- if (v == nullptr)
- return make_pair(nullptr, nullptr);
- if (get_size(v->l) >= key) {
- auto p = split(v->l, key);
- v->l = p.second;
- recalc(v);
- return make_pair(p.first, v);
- }
- auto p = split(v->r, key - get_size(v->l) - 1);
- v->r = p.first;
- recalc(v);
- return make_pair(v, p.second);
- }
- void insert(int x) {
- node *v = new node(x);
- auto p = split(root, x);
- root = merge(merge(p.first, v), p.second);
- }
- void del(node* v, int x) {
- auto p = split(v, x);
- auto a = p.first;
- p = split(p.second, x + 1);
- root = merge(a, p.second);
- }
- bool check(node* v, int x) {
- if (v == nullptr)
- return false;
- if (v->key == x)
- return true;
- if (v->key < x)
- return check(v->r, x);
- return check(v->l, x);
- }
- int kth(node* v, int k) {
- if (get_size(v->l) == k)
- return v->key;
- if (get_size(v->l) > k)
- return kth(v->l, k);
- return kth(v->r, k - get_size(v->l) - 1);
- }
- int next(node* v, int x) {
- if (v == nullptr)
- return INF;
- if (v->key > x)
- return min(next(v->l, x), v->key);
- return next(v->r, x);
- }
- int prev(node* v, int x) {
- if (v == nullptr)
- return -INF;
- if (v->key < x)
- return max(prev(v->r, x), v->key);
- return prev(v->l, x);
- }
- void out(node* v) {
- if (v == nullptr)
- return;
- out(v->l);
- cout << v->key << ' ';
- out(v->r);
- }
- int main()
- {
- //freopen("input.txt", "r", stdin);
- //freopen("movetofront.in", "r", stdin);
- //freopen("output.txt", "w", stdout);
- //freopen("movetofront.out", "w", stdout);
- srand(time(NULL));
- int n, m;
- cin >> n >> m;
- for (int i = 1; i <= n; ++i)
- insert(i);
- // out(root);
- // cout << endl;
- for (int i = 0; i < m; ++i) {
- int l, r;
- cin >> l >> r;
- // cout << "l = " << l << " r = " << r << endl;
- auto a = split(root, r);
- // cout << "a.first: ";
- // out(a.first);
- // cout << endl;
- // cout << "a.second: ";
- // out(a.second);
- // cout << endl;
- auto b = split(a.first, l - 1);
- // cout << "b.first: ";
- // out(b.first);
- // cout << endl;
- // cout << "b.second: ";
- // out(b.second);
- // cout << endl;
- root = merge(b.second, merge(b.first, a.second));
- // out(root);
- // cout << endl;
- //cout << get_size(root) << endl;
- }
- out(root);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement