#include <iostream> #include <vector> #include "message.h" #include "teatr.h" using namespace std; const int H = 20; const int MAX_ELEMENT = 1000000; int count_of_value[1 + MAX_ELEMENT]; int index_of_value[1 + MAX_ELEMENT]; long long tree[1 << (H + 1)]; vector<int> unique_values; void update(int i, int j) { i += 1 << H; j += 1 << H; while (i < j) { if (i & 1) { ++tree[i]; ++i; } if (!(j & 1)) { ++tree[j]; --j; } i >>= 1; j >>= 1; } if (i == j) { ++tree[i]; } } int query(int i) { int s = 0; i += 1 << H; while (i) { s += tree[i]; i >>= 1; } return s; } int main() { int n = GetN(); for (int i = 0; i < n; ++i) { ++count_of_value[GetElement(i)]; } for (int i = 1; i <= MAX_ELEMENT; ++i) { if (count_of_value[i] > 0) { index_of_value[i] = unique_values.size(); unique_values.push_back(i); } } int size = unique_values.size() / NumberOfNodes() + !!(unique_values.size() % NumberOfNodes()); int index_from = MyNodeId() * size; int index_to = index_from + size - 1; if (index_from >= (int)unique_values.size()) { PutLL(0, 0); Send(0); return 0; } if (index_to >= (int)unique_values.size()) { index_to = unique_values.size() - 1; } int value_from = unique_values[index_from]; int value_to = unique_values[index_to]; long long answer = 0; long long count_of_bigger_values = 0; for (int i = 0; i < n; ++i) { int value = GetElement(i); if (value < value_from) { continue; } if (value > value_to) { ++count_of_bigger_values; continue; } int index = index_of_value[value]; answer += count_of_bigger_values + query(index); if (index > index_from) { update(index_from, index - 1); } } if (MyNodeId() > 0) { PutLL(0, answer); Send(0); return 0; } int numberOfNodes = NumberOfNodes(); for (int i = 1; i < numberOfNodes; ++i) { Receive(i); answer += GetLL(i); } cout << answer << endl; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | #include <iostream> #include <vector> #include "message.h" #include "teatr.h" using namespace std; const int H = 20; const int MAX_ELEMENT = 1000000; int count_of_value[1 + MAX_ELEMENT]; int index_of_value[1 + MAX_ELEMENT]; long long tree[1 << (H + 1)]; vector<int> unique_values; void update(int i, int j) { i += 1 << H; j += 1 << H; while (i < j) { if (i & 1) { ++tree[i]; ++i; } if (!(j & 1)) { ++tree[j]; --j; } i >>= 1; j >>= 1; } if (i == j) { ++tree[i]; } } int query(int i) { int s = 0; i += 1 << H; while (i) { s += tree[i]; i >>= 1; } return s; } int main() { int n = GetN(); for (int i = 0; i < n; ++i) { ++count_of_value[GetElement(i)]; } for (int i = 1; i <= MAX_ELEMENT; ++i) { if (count_of_value[i] > 0) { index_of_value[i] = unique_values.size(); unique_values.push_back(i); } } int size = unique_values.size() / NumberOfNodes() + !!(unique_values.size() % NumberOfNodes()); int index_from = MyNodeId() * size; int index_to = index_from + size - 1; if (index_from >= (int)unique_values.size()) { PutLL(0, 0); Send(0); return 0; } if (index_to >= (int)unique_values.size()) { index_to = unique_values.size() - 1; } int value_from = unique_values[index_from]; int value_to = unique_values[index_to]; long long answer = 0; long long count_of_bigger_values = 0; for (int i = 0; i < n; ++i) { int value = GetElement(i); if (value < value_from) { continue; } if (value > value_to) { ++count_of_bigger_values; continue; } int index = index_of_value[value]; answer += count_of_bigger_values + query(index); if (index > index_from) { update(index_from, index - 1); } } if (MyNodeId() > 0) { PutLL(0, answer); Send(0); return 0; } int numberOfNodes = NumberOfNodes(); for (int i = 1; i < numberOfNodes; ++i) { Receive(i); answer += GetLL(i); } cout << answer << endl; return 0; } |