Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <sstream>
- #include <cmath>
- #include <algorithm>
- #include <memory.h>
- #include <stdio.h>
- #include <vector>
- #include <stack>
- #include <deque>
- #include <queue>
- #include <vector>
- #include <set>
- #include <iterator>
- #include <map>
- #include <iomanip>
- #include <unordered_set>
- #define int long long
- #define sp system("pause")
- #define pb push_back
- #define double long double
- #define endl "\n"
- #define un unsigned
- #define INF 1000000009
- #define pii pair<int, int>
- #define all(v) v.begin(), v.end()
- using namespace std;
- const int MOD = 1000000000;
- int rnd()
- {
- return (rand() << 16) | rand();
- }
- struct Node
- {
- int x, y, size;
- Node* left = nullptr;
- Node* right = nullptr;
- Node(int x) : x(x), y(rnd()), size(1) {}
- Node() : x(0), y(rnd()), size(1) {}
- };
- int get_size(Node* root)
- {
- if (root == nullptr)
- {
- return 0;
- }
- return root->size;
- }
- Node* update(Node* root)
- {
- root->size = get_size(root->left) + 1 + get_size(root->right);
- return root;
- }
- pair <Node*, Node*> split(Node* root, int pos)
- {
- if (root == nullptr)
- {
- return { nullptr, nullptr };
- }
- if (get_size(root->left) + 1 <= pos)
- {
- auto splitted = split(root->right, pos - get_size(root->left) - 1);
- root->right = splitted.first;
- root = update(root);
- return { root, splitted.second };
- }
- else
- {
- auto splitted = split(root->left, pos);
- root->left = splitted.second;
- root = update(root);
- return { splitted.first, root };
- }
- }
- Node* merge(Node* a, Node* b)
- {
- if (a == nullptr)
- {
- return b;
- }
- if (b == nullptr)
- {
- return a;
- }
- if (a->y < b->y)
- {
- a->right = merge(a->right, b);
- a = update(a);
- return a;
- }
- else
- {
- b->left = merge(a, b->left);
- b = update(b);
- return b;
- }
- }
- Node* find(Node* root, int pos)
- {
- if (root == nullptr)
- {
- return nullptr;
- }
- if (get_size(root->left) + 1 == pos)
- {
- return root;
- }
- if (get_size(root->left) + 1 < pos)
- {
- return find(root->right, pos - get_size(root->left) - 1);
- }
- else
- {
- return find(root->left, pos);
- }
- }
- Node* insert(Node* root, Node* new_node, int pos)
- {
- auto splitted = split(root, pos);
- root = merge(splitted.first, merge(new_node, splitted.second));
- root = update(root);
- return root;
- }
- void answer(Node* tree, int n)
- {
- for (int i = 1; i <= n; i++)
- {
- auto ans = find(tree, i);
- if (ans != nullptr)
- {
- cout << ans->x << " ";
- }
- }
- cout << endl;
- }
- signed main()
- {
- ios_base::sync_with_stdio();
- cin.tie(0);
- cout.tie(0);
- cout.precision(20);
- int n, m;
- cin >> n >> m;
- Node* tree = nullptr;
- for (int i = 1; i <= n; i++)
- {
- Node* new_node = new Node(i);
- tree = insert(tree, new_node, i - 1);
- }
- //answer(tree, n);
- for (int i = 0; i < m; i++)
- {
- int l, r;
- cin >> l >> r;
- auto splitted = split(tree, l - 1);
- auto splitted2 = split(splitted.second, r - l + 1);
- tree = merge(splitted2.first, merge(splitted.first, splitted2.second));
- //answer(tree, n);
- }
- answer(tree, n);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement