#include <algorithm> #include <cmath> #include <iostream> #include <string> #include <vector> #include "message.h" #include "teatr.h" using namespace std; typedef long long ll; typedef unsigned int ui; typedef pair<int, int> pii; typedef vector<int> vi; // increases value at ind by val void update(vector<int> &bit, unsigned int ind, int val) { // adds last set bit while(ind < bit.size()) { bit[ind] += val; ind += (ind & -ind); } } // queries sum of values 1 to ind int query_sum(vector<int> &bit, unsigned int ind) { // remove last set bit int sum = 0; while(ind > 0) { sum += bit[ind]; ind -= (ind & -ind); } return sum; } void print(vector<int> &v) { for (int i : v) cout << i << " "; cout << endl; } long long sum(vector<int> &v, int begin) { long long res = 0; for (int i = begin; i < v.size(); ++i) { res += v[i]; } return res; } //int GetN() { return 12; } //int GetElement(int i) { static vector<int> in = {1, 3, 2, 5, 1, 3, 4, 2, 3, 2, 5, 3}; return in[i]; } int main() { const int SIZE = 5; long long n = GetN(); int begin = n * MyNodeId() / NumberOfNodes(); int end = n * (MyNodeId() + 1) / NumberOfNodes(); long long inversions = 0; vector<int> cnt(SIZE, 0); for (int i = begin; i < end; ++i) { int e = GetElement(i); ++cnt[e - 1]; // cnt on BIT to get 2046 (message limit) inversions += sum(cnt, e); } //cout << begin << " " << end << " " << inversions << endl; //print(cnt); if (MyNodeId() == 0) { long long result = inversions; int rec_inversions; vector<int> recieved(SIZE); for (int i = 1; i < NumberOfNodes(); ++i) { Receive(i); result += GetLL(i); for (int j = 0; j < cnt.size(); ++j) { recieved[j] = GetInt(i); result += recieved[j] * sum(cnt, j + 1); } for (int j = 0; j < cnt.size(); ++j) { cnt[j] += recieved[j]; } } cout << result << endl; } else { PutLL(0, inversions); for (int i = 0; i < cnt.size(); ++i) { PutInt(0, cnt[i]); } Send(0); } 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 | #include <algorithm> #include <cmath> #include <iostream> #include <string> #include <vector> #include "message.h" #include "teatr.h" using namespace std; typedef long long ll; typedef unsigned int ui; typedef pair<int, int> pii; typedef vector<int> vi; // increases value at ind by val void update(vector<int> &bit, unsigned int ind, int val) { // adds last set bit while(ind < bit.size()) { bit[ind] += val; ind += (ind & -ind); } } // queries sum of values 1 to ind int query_sum(vector<int> &bit, unsigned int ind) { // remove last set bit int sum = 0; while(ind > 0) { sum += bit[ind]; ind -= (ind & -ind); } return sum; } void print(vector<int> &v) { for (int i : v) cout << i << " "; cout << endl; } long long sum(vector<int> &v, int begin) { long long res = 0; for (int i = begin; i < v.size(); ++i) { res += v[i]; } return res; } //int GetN() { return 12; } //int GetElement(int i) { static vector<int> in = {1, 3, 2, 5, 1, 3, 4, 2, 3, 2, 5, 3}; return in[i]; } int main() { const int SIZE = 5; long long n = GetN(); int begin = n * MyNodeId() / NumberOfNodes(); int end = n * (MyNodeId() + 1) / NumberOfNodes(); long long inversions = 0; vector<int> cnt(SIZE, 0); for (int i = begin; i < end; ++i) { int e = GetElement(i); ++cnt[e - 1]; // cnt on BIT to get 2046 (message limit) inversions += sum(cnt, e); } //cout << begin << " " << end << " " << inversions << endl; //print(cnt); if (MyNodeId() == 0) { long long result = inversions; int rec_inversions; vector<int> recieved(SIZE); for (int i = 1; i < NumberOfNodes(); ++i) { Receive(i); result += GetLL(i); for (int j = 0; j < cnt.size(); ++j) { recieved[j] = GetInt(i); result += recieved[j] * sum(cnt, j + 1); } for (int j = 0; j < cnt.size(); ++j) { cnt[j] += recieved[j]; } } cout << result << endl; } else { PutLL(0, inversions); for (int i = 0; i < cnt.size(); ++i) { PutInt(0, cnt[i]); } Send(0); } return 0; } |