Georgiy031

Untitled

Sep 8th, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.30 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <unordered_set>
  3. using namespace std;
  4. typedef long long ll;
  5. typedef unsigned long long ull;
  6. #define all(x) x.begin(), x.end()
  7. #define rall(x) x.rbegin(), x.rend()
  8. ll gcd(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); }
  9. ll fac(ll a) { return (a ? a * fac(a - 1) : 1); }
  10.  
  11. int n, m;
  12. vector<int> V;
  13. void build_tree()
  14. {
  15.     int N = 1;
  16.     while (N < n + m) N *= 2;
  17.     V.resize(2 * N, 0);
  18.  
  19.     // инициализируем листы
  20.     for (int i = N; i < 2 * N; i++)
  21.         V[i] = V[i - N];
  22.  
  23.     // и все остальные вершины
  24.     for (int i = N - 1; i > 0; i--)
  25.         V[i] = V[2 * i] + V[2 * i + 1];
  26. }
  27.  
  28. int rmq_up(int l, int r)
  29. {
  30.     int ans = 0;
  31.     int n = V.size() / 2;
  32.     l += n - 1, r += n - 1;
  33.     while (l <= r)
  34.     {
  35.         // если l - правый сын своего родителя,
  36.         // учитываем его фундаментальный отрезок
  37.         if (l & 1)
  38.             ans = ans + V[l];
  39.         // если r - левый сын своего родителя,
  40.         // учитываем его фундаментальный отрезок
  41.         if (!(r & 1))
  42.             ans = ans + V[r];
  43.         // сдвигаем указатели на уровень выше
  44.         l = (l + 1) / 2, r = (r - 1) / 2;
  45.     }
  46.     return ans;
  47. }
  48. int get_pos(int sum) {
  49.     int i = 1;
  50.     while (i < V.size() / 2) {
  51.         if (sum > V[i * 2 + 1]) {
  52.             sum -= V[i * 2 + 1];
  53.             i = i * 2;
  54.         }
  55.         else {
  56.             i = i * 2 + 1;
  57.         }
  58.     }
  59.     return i - V.size() / 2;
  60. }
  61.  
  62. void update(int i, int x)
  63. {
  64.     int N = V.size() / 2;
  65.     i += N - 1;
  66.     V[i] = x;
  67.     while (i /= 2)
  68.         V[i] = V[2 * i] + V[2 * i + 1];
  69. }
  70.  
  71. signed main() {
  72.  
  73.     ios_base::sync_with_stdio(false);
  74.     cin.tie(nullptr);
  75.     cout.tie(nullptr);
  76.  
  77.     int type;
  78.     cin >> n >> m >> type;
  79.     V.assign(m, 1);
  80.     V.resize(n + m, 0);
  81.     build_tree();
  82.     int cur = m;
  83.  
  84.     if (type == 1) {
  85.        
  86.  
  87.         vector<int> pos(m);
  88.         for (int i = 0; i < m; ++i) {
  89.             pos[i] = m - 1 - i;
  90.         }
  91.  
  92.         for (int i = 0; i < n; ++i) {
  93.             int x;
  94.             cin >> x;
  95.             cout << rmq_up(pos[x - 1] + 1, n + m) << " ";
  96.             update(pos[x - 1] + 1, 0);
  97.             update(cur + 1, 1);
  98.             pos[x - 1] = cur;
  99.             ++cur;
  100.         }
  101.     }
  102.     if (type == 2) {
  103.         vector<int> pos(n + m);
  104.         for (int i = 0; i < m; ++i) {
  105.             pos[i] = m - 1 - i;
  106.         }
  107.         for (int i = 0; i < n; ++i) {
  108.             int x;
  109.             cin >> x;
  110.             int res = get_pos(x);
  111.             cout << pos[res] + 1 << " ";
  112.             update(res + 1, 0);
  113.             update(cur + 1, 1);
  114.             pos[cur++] = pos[res];
  115.         }
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment