#include<set>
#include<algorithm>
#include<cstdio>
#include "message.h"
#include "teatr.h"
using namespace std;
const int M = 1000007;
const int C = (1<<20);
int drz[2*C];
void wstaw(int n) {
for (int it = n+C-1; it > 0; it /= 2) {
drz[it]++;
}
}
int ileMniejszych(int n) {
int wyn = 0;
for (int it = n+C-1; it > 0; it /= 2) {
if (it %2 == 1) wyn += drz[it-1];
}
return wyn;
}
void wykonaj(int id) {
fill(drz, drz+2*C, 0);
int n = GetN();
long long w = 0;
//liczenie na liczby kolizyj
int a = id%10;
int b = id/10;
if (a < b) {
wstaw(M-GetElement(a) );
for (; b < n; b += 10) {
w += ileMniejszych(M-GetElement(b) );
a += 10;
wstaw(M-GetElement(a) );
}
} else {
for (; a < n; a += 10) {
w += ileMniejszych(M-GetElement(b) );
wstaw(M-GetElement(a) );
b += 10;
}
}
if (id != 0) {
PutLL(0, w);
Send(0);
} else {
long long wyn = w;
for (int i = 1; i < 100; ++i) {
Receive(i);
w = GetLL(i);
wyn += w;
}
printf("%lli\n",wyn);
}
return;
}
int main() {
wykonaj(MyNodeId());
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 | #include<set> #include<algorithm> #include<cstdio> #include "message.h" #include "teatr.h" using namespace std; const int M = 1000007; const int C = (1<<20); int drz[2*C]; void wstaw(int n) { for (int it = n+C-1; it > 0; it /= 2) { drz[it]++; } } int ileMniejszych(int n) { int wyn = 0; for (int it = n+C-1; it > 0; it /= 2) { if (it %2 == 1) wyn += drz[it-1]; } return wyn; } void wykonaj(int id) { fill(drz, drz+2*C, 0); int n = GetN(); long long w = 0; //liczenie na liczby kolizyj int a = id%10; int b = id/10; if (a < b) { wstaw(M-GetElement(a) ); for (; b < n; b += 10) { w += ileMniejszych(M-GetElement(b) ); a += 10; wstaw(M-GetElement(a) ); } } else { for (; a < n; a += 10) { w += ileMniejszych(M-GetElement(b) ); wstaw(M-GetElement(a) ); b += 10; } } if (id != 0) { PutLL(0, w); Send(0); } else { long long wyn = w; for (int i = 1; i < 100; ++i) { Receive(i); w = GetLL(i); wyn += w; } printf("%lli\n",wyn); } return; } int main() { wykonaj(MyNodeId()); return 0; } |
English