Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #include <unordered_set>
- using namespace std;
- typedef long long ll;
- typedef unsigned long long ull;
- #define all(x) x.begin(), x.end()
- #define rall(x) x.rbegin(), x.rend()
- ll gcd(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); }
- ll fac(ll a) { return (a ? a * fac(a - 1) : 1); }
- int n, m;
- vector<int> V;
- void build_tree()
- {
- int N = 1;
- while (N < n + m) N *= 2;
- V.resize(2 * N, 0);
- // инициализируем листы
- for (int i = N; i < 2 * N; i++)
- V[i] = V[i - N];
- // и все остальные вершины
- for (int i = N - 1; i > 0; i--)
- V[i] = V[2 * i] + V[2 * i + 1];
- }
- int rmq_up(int l, int r)
- {
- int ans = 0;
- int n = V.size() / 2;
- l += n - 1, r += n - 1;
- while (l <= r)
- {
- // если l - правый сын своего родителя,
- // учитываем его фундаментальный отрезок
- if (l & 1)
- ans = ans + V[l];
- // если r - левый сын своего родителя,
- // учитываем его фундаментальный отрезок
- if (!(r & 1))
- ans = ans + V[r];
- // сдвигаем указатели на уровень выше
- l = (l + 1) / 2, r = (r - 1) / 2;
- }
- return ans;
- }
- int get_pos(int sum) {
- int i = 1;
- while (i < V.size() / 2) {
- if (sum > V[i * 2 + 1]) {
- sum -= V[i * 2 + 1];
- i = i * 2;
- }
- else {
- i = i * 2 + 1;
- }
- }
- return i - V.size() / 2;
- }
- void update(int i, int x)
- {
- int N = V.size() / 2;
- i += N - 1;
- V[i] = x;
- while (i /= 2)
- V[i] = V[2 * i] + V[2 * i + 1];
- }
- signed main() {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- int type;
- cin >> n >> m >> type;
- V.assign(m, 1);
- V.resize(n + m, 0);
- build_tree();
- int cur = m;
- if (type == 1) {
- vector<int> pos(m);
- for (int i = 0; i < m; ++i) {
- pos[i] = m - 1 - i;
- }
- for (int i = 0; i < n; ++i) {
- int x;
- cin >> x;
- cout << rmq_up(pos[x - 1] + 1, n + m) << " ";
- update(pos[x - 1] + 1, 0);
- update(cur + 1, 1);
- pos[x - 1] = cur;
- ++cur;
- }
- }
- if (type == 2) {
- vector<int> pos(n + m);
- for (int i = 0; i < m; ++i) {
- pos[i] = m - 1 - i;
- }
- for (int i = 0; i < n; ++i) {
- int x;
- cin >> x;
- int res = get_pos(x);
- cout << pos[res] + 1 << " ";
- update(res + 1, 0);
- update(cur + 1, 1);
- pos[cur++] = pos[res];
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment