Advertisement
fiffeek

Untitled

Oct 26th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.72 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. #define MOD 1000000000
  7.  
  8. struct Tree {
  9.     unsigned long s = 1;
  10.     vector<long long> tree;
  11.  
  12.     Tree(unsigned long n) {
  13.         while (s < n) {
  14.             s *= 2;
  15.         }
  16.         tree.resize(2 * s, 0);
  17.     }
  18.  
  19.     void update(unsigned long index, long long sum) {
  20.         index += s;
  21.  
  22.         while (index) {
  23.             tree[index] += sum;
  24.             index /= 2;
  25.         }
  26.     }
  27.  
  28.     long long sum(unsigned long s_index, unsigned long e_index) {
  29.         s_index += s, e_index += s;
  30.         long long res = 0;
  31.  
  32.         res += tree[s_index];
  33.         if (e_index != s_index) res += tree[e_index];
  34.         res %= MOD;
  35.  
  36.         while (s_index / 2 < e_index / 2) {
  37.             if (!(s_index & 1)) res += tree[s_index + 1];
  38.             res %= MOD;
  39.             if (e_index & 1) res += tree[e_index - 1];
  40.             res %= MOD;
  41.  
  42.             s_index /= 2;
  43.             e_index /= 2;
  44.         }
  45.  
  46.         return res;
  47.     }
  48. };
  49.  
  50. int main() {
  51.    unsigned long n, k;
  52.    cin >> n >> k;
  53.    vector<unsigned long> vi;
  54.    vector<Tree> trees;
  55.  
  56.    for (unsigned long i = 0; i < k; ++i) {
  57.        trees.emplace_back(Tree(n));
  58.    }
  59.  
  60.    for (unsigned long i = 0; i < n; ++i) {
  61.        int aux;
  62.        cin >> aux;
  63.  
  64.        vi.push_back(aux);
  65.    }
  66.  
  67.    for (unsigned long x = 0; x < n; ++x) {
  68.        for (long long j = (long) k - 1; j >= 0; j--) {
  69.            if (j == 0) {
  70.                trees[j].update(vi[x] - 1, 1);
  71.                continue;
  72.            }
  73.  
  74.            long long adder = trees[j - 1].sum(vi[x], n - 1);
  75.            trees[j].update(vi[x] - 1, adder);
  76.        }
  77.    }
  78.  
  79.    cout << (trees[k - 1].sum(0, n - 1) % MOD) << endl;
  80.    return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement