#include "bits/stdc++.h"
#include "message.h"
#include "teatr.h"
using namespace std;
#define NUMBER_OF_NODES 100
#define ELEM_MAX 1000000
namespace tree{
#define M 1048576
const int array_size = 2*M;
int array[array_size];
void init(){
for(int i=0; i<array_size; i++){
array[i] = 0;
}
}
void insert(int a, int val = 1){
int pos = M + a;
array[pos] += val;
while(pos != 1){
pos /= 2;
array[pos] = array[pos*2] + array[pos*2 + 1];
}
}
int query(int a, int b){
if(a > b) return 0;
int left = M + a, right = M + b;
int ans = array[left];
if(left != right) ans += array[right];
while(left/2 != right/2){
if(left%2 == 0) ans += array[left + 1];
if(right%2 == 1) ans += array[right - 1];
left /= 2;
right /= 2;
}
return ans;
}
}
int main(){
int my_node_id = MyNodeId();
int n = GetN();
long long count=0;
if(n <= 1000000){
if(my_node_id != 0) return 0;
tree::init();
for(int k=0; k<n; k++){
int elem = GetElement(k);
count += tree::query(elem, ELEM_MAX-1);
tree::insert(elem-1);
}
cout << count << "\n";
} else {
int calc[] = {0, 0, 0, 0, 0}, calc_size=5;
for(int k=(my_node_id*n)/NUMBER_OF_NODES; k<((my_node_id+1)*n)/NUMBER_OF_NODES; k++){
int elem = GetElement(k)%6;
for(int i=calc_size-1; i>elem-1; i--) count += calc[i];
calc[elem-1] += 1;
}
if(my_node_id == 0){
for(int k=1; k<NUMBER_OF_NODES; k++){
Receive(k);
count += GetLL(k);
int temp_sum = 0;
for(int i=0; i<calc_size; i++) temp_sum += calc[i];
for(int i=0; i<calc_size; i++){
int temp = GetInt(k);
temp_sum -= calc[i];
count += temp * temp_sum;
calc[i] += temp;
}
}
cout << count << "\n";
} else {
PutLL(0, count);
for(int i=0; i<calc_size; i++) PutInt(0, calc[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 | #include "bits/stdc++.h" #include "message.h" #include "teatr.h" using namespace std; #define NUMBER_OF_NODES 100 #define ELEM_MAX 1000000 namespace tree{ #define M 1048576 const int array_size = 2*M; int array[array_size]; void init(){ for(int i=0; i<array_size; i++){ array[i] = 0; } } void insert(int a, int val = 1){ int pos = M + a; array[pos] += val; while(pos != 1){ pos /= 2; array[pos] = array[pos*2] + array[pos*2 + 1]; } } int query(int a, int b){ if(a > b) return 0; int left = M + a, right = M + b; int ans = array[left]; if(left != right) ans += array[right]; while(left/2 != right/2){ if(left%2 == 0) ans += array[left + 1]; if(right%2 == 1) ans += array[right - 1]; left /= 2; right /= 2; } return ans; } } int main(){ int my_node_id = MyNodeId(); int n = GetN(); long long count=0; if(n <= 1000000){ if(my_node_id != 0) return 0; tree::init(); for(int k=0; k<n; k++){ int elem = GetElement(k); count += tree::query(elem, ELEM_MAX-1); tree::insert(elem-1); } cout << count << "\n"; } else { int calc[] = {0, 0, 0, 0, 0}, calc_size=5; for(int k=(my_node_id*n)/NUMBER_OF_NODES; k<((my_node_id+1)*n)/NUMBER_OF_NODES; k++){ int elem = GetElement(k)%6; for(int i=calc_size-1; i>elem-1; i--) count += calc[i]; calc[elem-1] += 1; } if(my_node_id == 0){ for(int k=1; k<NUMBER_OF_NODES; k++){ Receive(k); count += GetLL(k); int temp_sum = 0; for(int i=0; i<calc_size; i++) temp_sum += calc[i]; for(int i=0; i<calc_size; i++){ int temp = GetInt(k); temp_sum -= calc[i]; count += temp * temp_sum; calc[i] += temp; } } cout << count << "\n"; } else { PutLL(0, count); for(int i=0; i<calc_size; i++) PutInt(0, calc[i]); Send(0); } } return 0; } |
English