Advertisement
Guest User

Untitled

a guest
May 21st, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.25 KB | None | 0 0
  1. #include <cstdio>
  2. #include <vector>
  3. using std::vector;
  4. vector<int> tree;
  5. int basement_size;
  6.  
  7. void build_tree(const vector<int>& array)
  8. {
  9.     basement_size = 1;
  10.     while (basement_size < array.size())
  11.     {
  12.         basement_size *= 2;
  13.     }
  14.     tree.resize(basement_size * 2);
  15. }
  16.  
  17. void update_tree(int index, const int element)
  18. {
  19.     index += basement_size - 1;
  20.     tree[index] = element;
  21.     while (index > 1)
  22.     {
  23.         index /= 2;
  24.         tree[index] = tree[index * 2] + tree[index * 2 + 1];
  25.     }
  26. }
  27.  
  28. int sum(int left, int right)
  29. {
  30.     auto result(0);
  31.     left += basement_size - 1;
  32.     right += basement_size - 1;
  33.     while (left <= right)
  34.     {
  35.         if (left % 2 != 0)
  36.         {
  37.             result += tree[left];
  38.             left++;
  39.         }
  40.         if (right % 2 == 0)
  41.         {
  42.             result += tree[right];
  43.             right--;
  44.         }
  45.         left /= 2;
  46.         right /= 2;
  47.     }
  48.     return result;
  49. }
  50.  
  51. int main()
  52. {
  53.     int N, K;
  54.     scanf("%d%d", &N, &K);
  55.     vector<int> array(N);
  56.     build_tree(array);
  57.     auto result(0);
  58.     for (auto i(0); i < K; i++)
  59.     {
  60.         for (auto j(0); j < N; j++)
  61.         {
  62.             scanf("%d", &array[j]);
  63.             update_tree(array[j], 1);
  64.             //j или j - 1
  65.  
  66.             //сколько чисел было до него
  67.         }
  68.         for (auto j(basement_size); j < tree.size(); j++)
  69.         {
  70.             result += sum(tree[j], tree[array.size() - 1]);
  71.         }
  72.     }
  73.  
  74.     return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement