//#define TEST_PROGRAMU
#include "message.h"
#ifndef TEST_PROGRAMU
#include "teatr.h"
#endif
#include "bits/stdc++.h"
#ifdef TEST_PROGRAMU
int GetN() { return int(1e8); }
int GetElement(int i) { return (i * 1ll * i) % int(1e6) + 1; }
#endif
using namespace std;
int fitBit(int v) {
int i=1;
while(v>i) i<<=1;
return i;
}
/**
* Pomocnicza struktura drzewiasta, do zlicznia ilości elementów.
*/
class SumTree {
private:
/** Wartość pierwszego elementu */
const int start;
/** Ilość liści */
const int size;
/** Indeks dla liści */
const int leafs;
/** Ilość większych niż zakres */
int bigger;
int* nodes;
public:
SumTree(int start, int size) : start(start), size(size), leafs(fitBit(size)-1), bigger(0) {
nodes=new int[((leafs+1)<<1)-1];
}
~SumTree() {
delete[] nodes;
}
private:
public:
int process(int h) {
h-=start;
if(h<0) { // nie z naszej grupy
return 0;
}
if(h>=size) { // nie z naszej grupy
++bigger; // ale jako duży
return 0;
}
int res=0;
int ti=leafs+h;
while(ti>0) {
if((ti&1)==1) res+=nodes[ti+1]; // rodzeństwo z prawej (większa wartość)
nodes[ti]++; // zwiększamy wartość
ti=(ti-1)>>1; // i do góry
}
res+=bigger;
return res;
}
};
/** Maksymalna wartość dla wzrostu */
const int maxHeight=1000000;
int main() {
ios_base::sync_with_stdio(false);
/** Ilość dla jednej instacji do przetworzenia */
const int perNode=(maxHeight+NumberOfNodes()-1)/NumberOfNodes();
/** Początek grupy dla tej instancji */
const int thisStart=MyNodeId()*perNode;
SumTree st(thisStart, perNode);
const int size=GetN();
uint64_t sum=0;
for(int i=0;i<size;++i) {
sum+=st.process(GetElement(i));
}
if(MyNodeId()>0) { // wszyscy wysyłają do 1-ki
PutLL(0, sum);
Send(0);
return 0;
}
// odbieramy od wszystkich
for(int i=1;i<NumberOfNodes();++i) {
Receive(i);
sum+=GetLL(i);
}
cout<<sum<<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 | //#define TEST_PROGRAMU #include "message.h" #ifndef TEST_PROGRAMU #include "teatr.h" #endif #include "bits/stdc++.h" #ifdef TEST_PROGRAMU int GetN() { return int(1e8); } int GetElement(int i) { return (i * 1ll * i) % int(1e6) + 1; } #endif using namespace std; int fitBit(int v) { int i=1; while(v>i) i<<=1; return i; } /** * Pomocnicza struktura drzewiasta, do zlicznia ilości elementów. */ class SumTree { private: /** Wartość pierwszego elementu */ const int start; /** Ilość liści */ const int size; /** Indeks dla liści */ const int leafs; /** Ilość większych niż zakres */ int bigger; int* nodes; public: SumTree(int start, int size) : start(start), size(size), leafs(fitBit(size)-1), bigger(0) { nodes=new int[((leafs+1)<<1)-1]; } ~SumTree() { delete[] nodes; } private: public: int process(int h) { h-=start; if(h<0) { // nie z naszej grupy return 0; } if(h>=size) { // nie z naszej grupy ++bigger; // ale jako duży return 0; } int res=0; int ti=leafs+h; while(ti>0) { if((ti&1)==1) res+=nodes[ti+1]; // rodzeństwo z prawej (większa wartość) nodes[ti]++; // zwiększamy wartość ti=(ti-1)>>1; // i do góry } res+=bigger; return res; } }; /** Maksymalna wartość dla wzrostu */ const int maxHeight=1000000; int main() { ios_base::sync_with_stdio(false); /** Ilość dla jednej instacji do przetworzenia */ const int perNode=(maxHeight+NumberOfNodes()-1)/NumberOfNodes(); /** Początek grupy dla tej instancji */ const int thisStart=MyNodeId()*perNode; SumTree st(thisStart, perNode); const int size=GetN(); uint64_t sum=0; for(int i=0;i<size;++i) { sum+=st.process(GetElement(i)); } if(MyNodeId()>0) { // wszyscy wysyłają do 1-ki PutLL(0, sum); Send(0); return 0; } // odbieramy od wszystkich for(int i=1;i<NumberOfNodes();++i) { Receive(i); sum+=GetLL(i); } cout<<sum<<endl; return 0; } |
English