#include "message.h" #include "teatr.h" #include "bits/stdc++.h" using namespace std; #define MAX_WZROST 1000005 int n, numNodes, id; class Drzewo{ private: int n; int wys; int * tab; public: void Plus(int node, int delta = 1){ node += n; for(int i=0;i<=wys;i++){ tab[node] += delta; node /= 2; } } int Count(int node){ node += n; int out = 0; out += tab[node]; for(int i=0;i<wys;i++){ if(node % 2 == 1) out += tab[node-1]; node /= 2; } return out; } Drzewo(int x){ wys = 0; while(x>0){ x/=2; wys++; } n=1; for(int i=0;i<wys;i++) n *= 2; tab = new int[2*n+3]; } void TabFl(){ cout << "Tab "; for(int i=0;i<2*n;i++) cout << i << "=" << tab[i] <<" , "; cout << endl; } ~Drzewo(){ delete [] tab; } }; int pocz(int nr){ return n/numNodes * nr; } int kon(int nr){ if(nr == numNodes - 1) return n-1; else return pocz(nr + 1) -1 ; } void zlicz(int nr, int * tab){ int a = pocz(nr); int b = kon(nr); for(int i=a; i<=b; i++) tab[ GetElement(i) ]++; } int main() { int i, pr1, pr2, po1, po2; n = GetN(); numNodes = NumberOfNodes(); id = MyNodeId(); if((numNodes % 2) == 0){ if(id< numNodes/2){ pr1 = 0; pr2 = id - 1; po1 = id + numNodes/2; po2 = numNodes - 1; }else{ pr1 = id - (numNodes/2 -1); pr2 = id-1; po1 = 2; // brak po2 = 1; // brak } }else{ if(id<= numNodes/2){ pr1 = 0; pr2 = id - 1; po1 = id + numNodes/2 + 1; po2 = numNodes - 1; }else{ pr1 = id - (numNodes/2); pr2 = id-1; po1 = 2; // brak po2 = 1; // brak } } // zlicz przed -> cumulative (większych od) // zlicz po -> cumulative (niższych od) int przed[MAX_WZROST] = {0}, po[MAX_WZROST] = {0}; for(i=pr1; i<= pr2; i++) zlicz(i, przed); for(i=po1; i<= po2; i++) zlicz(i, po); // kumulacja for(i=1e6; i>=0; i--) przed[i] += przed[i+1]; for(i=1; i <= 1e6+1 ; i++) po[i] += po[i-1]; long long ile = 0; Drzewo * drz = new Drzewo(1e6+5); // idź po bloku int begin = pocz(id); int end = kon(id); int wzrost; for( i=end; i>=begin; i-- ){ wzrost = GetElement(i); // vs. inne bloki ile += przed[ wzrost + 1 ]; ile += po[ wzrost - 1 ]; // w obrebie danego bloku ile += drz->Count( wzrost - 1 ); drz->Plus(wzrost); } delete drz; if(id>0){ PutLL(0, ile); Send(0); } long long sum = ile; if(id==0){ for(i=1;i<numNodes;i++){ Receive(i); sum += GetLL(i); } cout << sum << "\n"; } }
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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | #include "message.h" #include "teatr.h" #include "bits/stdc++.h" using namespace std; #define MAX_WZROST 1000005 int n, numNodes, id; class Drzewo{ private: int n; int wys; int * tab; public: void Plus(int node, int delta = 1){ node += n; for(int i=0;i<=wys;i++){ tab[node] += delta; node /= 2; } } int Count(int node){ node += n; int out = 0; out += tab[node]; for(int i=0;i<wys;i++){ if(node % 2 == 1) out += tab[node-1]; node /= 2; } return out; } Drzewo(int x){ wys = 0; while(x>0){ x/=2; wys++; } n=1; for(int i=0;i<wys;i++) n *= 2; tab = new int[2*n+3]; } void TabFl(){ cout << "Tab "; for(int i=0;i<2*n;i++) cout << i << "=" << tab[i] <<" , "; cout << endl; } ~Drzewo(){ delete [] tab; } }; int pocz(int nr){ return n/numNodes * nr; } int kon(int nr){ if(nr == numNodes - 1) return n-1; else return pocz(nr + 1) -1 ; } void zlicz(int nr, int * tab){ int a = pocz(nr); int b = kon(nr); for(int i=a; i<=b; i++) tab[ GetElement(i) ]++; } int main() { int i, pr1, pr2, po1, po2; n = GetN(); numNodes = NumberOfNodes(); id = MyNodeId(); if((numNodes % 2) == 0){ if(id< numNodes/2){ pr1 = 0; pr2 = id - 1; po1 = id + numNodes/2; po2 = numNodes - 1; }else{ pr1 = id - (numNodes/2 -1); pr2 = id-1; po1 = 2; // brak po2 = 1; // brak } }else{ if(id<= numNodes/2){ pr1 = 0; pr2 = id - 1; po1 = id + numNodes/2 + 1; po2 = numNodes - 1; }else{ pr1 = id - (numNodes/2); pr2 = id-1; po1 = 2; // brak po2 = 1; // brak } } // zlicz przed -> cumulative (większych od) // zlicz po -> cumulative (niższych od) int przed[MAX_WZROST] = {0}, po[MAX_WZROST] = {0}; for(i=pr1; i<= pr2; i++) zlicz(i, przed); for(i=po1; i<= po2; i++) zlicz(i, po); // kumulacja for(i=1e6; i>=0; i--) przed[i] += przed[i+1]; for(i=1; i <= 1e6+1 ; i++) po[i] += po[i-1]; long long ile = 0; Drzewo * drz = new Drzewo(1e6+5); // idź po bloku int begin = pocz(id); int end = kon(id); int wzrost; for( i=end; i>=begin; i-- ){ wzrost = GetElement(i); // vs. inne bloki ile += przed[ wzrost + 1 ]; ile += po[ wzrost - 1 ]; // w obrebie danego bloku ile += drz->Count( wzrost - 1 ); drz->Plus(wzrost); } delete drz; if(id>0){ PutLL(0, ile); Send(0); } long long sum = ile; if(id==0){ for(i=1;i<numNodes;i++){ Receive(i); sum += GetLL(i); } cout << sum << "\n"; } } |