Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <vector>
- using std::vector;
- vector<int> tree;
- int basement_size;
- void build_tree(const vector<int>& array)
- {
- basement_size = 1;
- while (basement_size < array.size())
- {
- basement_size *= 2;
- }
- tree.resize(basement_size * 2);
- }
- void update_tree(int index, const int element)
- {
- index += basement_size - 1;
- tree[index] = element;
- while (index > 1)
- {
- index /= 2;
- tree[index] = tree[index * 2] + tree[index * 2 + 1];
- }
- }
- int sum(int left, int right)
- {
- auto result(0);
- left += basement_size - 1;
- right += basement_size - 1;
- while (left <= right)
- {
- if (left % 2 != 0)
- {
- result += tree[left];
- left++;
- }
- if (right % 2 == 0)
- {
- result += tree[right];
- right--;
- }
- left /= 2;
- right /= 2;
- }
- return result;
- }
- int main()
- {
- int N, K;
- scanf("%d%d", &N, &K);
- vector<int> array(N);
- build_tree(array);
- auto result(0);
- for (auto i(0); i < K; i++)
- {
- for (auto j(0); j < N; j++)
- {
- scanf("%d", &array[j]);
- update_tree(array[j], 1);
- //j или j - 1
- //сколько чисел было до него
- }
- for (auto j(basement_size); j < tree.size(); j++)
- {
- result += sum(tree[j], tree[array.size() - 1]);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement