Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <cstdio>
- #include <vector>
- #include <cmath>
- #include <string>
- #include <algorithm>
- #include <string>
- #include <deque>
- #include <iomanip>
- #include <cstddef>
- #define F first
- #define S second
- using namespace std;
- const long double eps = 0.000000005;
- const long double pi = 3.1415926535897932;
- const long double inf = 1e9;
- struct node
- {
- node* l;
- node* r;
- int v;
- int p;
- int w;
- node()
- {
- l = nullptr;
- r = nullptr;
- v = 0;
- p = rand();
- w = 1;
- }
- node(int _v)
- {
- l = nullptr;
- r = nullptr;
- v = _v;
- p = rand();
- w = 1;
- }
- };
- using pnode = node*;
- int weight(pnode& t)
- {
- if (t == nullptr)
- return 0;
- else
- return t->w;
- }
- void upd(pnode& t)
- {
- /*if (t == nullptr)
- t->w = 0;
- else*/
- if (t)
- t->w = weight(t->l) + weight(t->r) + 1;
- }
- void split(pnode t, pnode& t1, pnode& t2, int k)
- {
- if (t == nullptr)
- {
- t1 = nullptr;
- t2 = nullptr;
- return;
- }
- if (weight(t->l) >= k)
- {
- split(t->l, t1, t->l, k);
- t2 = t;
- }
- else
- {
- split(t->r, t->r, t2, k - weight(t->l) - 1);
- t1 = t;
- }
- upd(t);
- }
- void merge(pnode& t, pnode l, pnode r)
- {
- if (l == nullptr || r == nullptr)
- t = l ? l : r;
- else
- {
- if (l->p > r->p)
- {
- merge(l->r, l->r, r);
- t = l;
- }
- else
- {
- merge(r->l, l, r->l);
- t = r;
- }
- }
- upd(t);
- }
- int main()
- {
- ios_base::sync_with_stdio(0);
- //freopen("input.txt", "r", stdin);
- // freopen("output.txt", "w", stdout);
- int n, k;
- cin >> n >> k;
- k--;
- pnode t = nullptr, m = nullptr, r = nullptr, l = nullptr;
- //upd(t);
- for (int i = 0; i < n; i++)
- {
- merge(t, t, new node(i + 1));
- }
- for (int i = 0; i < n; i++)
- {
- split(t, l, r, k % t->w);
- split(r, m, r, 1);
- cout << m->v << " ";
- merge(t, r, l);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement