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
#include <iostream>
#include "teatr.h"
#include "message.h"
#include <string>
const int N = 10000100;
int tree[N];
int TREESIZE = 1;
void insert(int p,int w){
    p +=TREESIZE-1;
    tree[p] += w;
    p/=2;
    while(p>0){
        tree[p] += w;
        p/=2;
    }
}

/*
int GetElement(int i)
{
    if(i == 1e5-1) return 4;
    if(i == 1e8-1) return 5;

    return 6;
}

int GetN()
{
    return 1e8;
}
*/
int query(int l,int r){
    l+=TREESIZE-1;
    r+=TREESIZE-1;
    int w = 0;
    while(true){
        if(l==r){
            w+=tree[l];
            break;
        }
        if(l%2==1){
            w+=tree[l];
            l++;
        }
        if(r%2==0){
            w+=tree[r];
            r--;
        }
        if(l>=r){
            break;
        }
        l/=2;
        r/=2;
    }
    return w;
}

void init(){
    for(int i=TREESIZE-1; i>=1; i--){
        tree[i] = tree[i*2]+tree[(i*2)+1];
    }
}



int main() {
    int numberOfNodes = NumberOfNodes();
    int myNodeId = MyNodeId();
    int n = GetN();
    int packetSize = n / numberOfNodes;
    //dla ntego komputera - koniec = myNodeId*packetSize - poczatek = (myNodeId-1)*packetSize + 1; if
    int start = (myNodeId ) * packetSize + 1;
    int end = (myNodeId + 1 )* packetSize ;
    if(packetSize==0) {
        end = -1;
        start = 0;

    }
    if( myNodeId == 0 ){
        start = 0;
    }
    if (myNodeId == numberOfNodes-1) {
        end = n - 1;
    }
    int size = end - start + 1;
    while(TREESIZE<1000100) TREESIZE*=2;
    int offset = start;

    /*if(myNodeId==0){
        printf("packetSize: %d, start: %d, end: %d\n",packetSize,start,end);
    }*/

    for (int i = 0; i <start ; i++) {
        tree[GetElement(i)+TREESIZE-1]++;
    }
    init();
    long long w = 0;

    for(int i=start; i<=end; i++){
        int tmp = GetElement(i);
        int l = tmp+1;
        int p = 1000000;
        w+=query(l,p);
        insert(tmp,1);
    }


    if (myNodeId != 0) {

        PutLL(0, w);
        Send(0);
    }
    else{

        for(int i=1; i<numberOfNodes; i++){
            int r = Receive(i);

            w+=GetLL(r);
        }
        printf("%lld",w);
    }

    return 0;
}