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
#include <iostream>
#include <cassert>
//#include <cmath>

#include "teatr.h"
#include "message.h"

using namespace std;

//const int rows = 1000 * 1000 * 10;

int node_id;
int no_of_nodes;

/*
int GetN() { return int(1e8); }
int GetElement(int i) { return (i * 1ll * i) % int(1e3) + 1; }

//int GetN() {
//        return rows;
//}

//int GetElement(int i) {
//        assert(0 <= i && i < rows);
//        if (i < 5000) {
//                return (i % 100) + 1;
//        }
//        return (i % 1000) + 1;
//}
*/

const int max_height = 1000000;
unsigned long long t[max_height+1];

void updateBIT(int idx, int max_h)
{ 
        while (idx <= max_h) { 
                t[idx]++;
                idx += idx & (-idx); 
        }
} 

int getSum(int idx) 
{ 
    int sum = 0;
  
    while (idx > 0) { 
        sum += t[idx]; 
        idx -= idx & (-idx); 
    } 
    return sum; 
} 


int main()
{
        node_id = MyNodeId();
        no_of_nodes = NumberOfNodes();

//        cout << "node_id: " << node_id << '\n';
//        cout << "no_of_nodes: " << no_of_nodes << '\n';

        unsigned long long result = 0;
        long long n = GetN();
        int h;
        int max_h;
        int start_elm, stop_elm;

        if (n < 100) { 
                if (node_id > 0) {
                        return 0;
                }

                h = GetElement(0);
                t[h]++;
                for(int i = 1; i < n; ++i) {
                        h = GetElement(i);
                        for(int j = h+1; j <= max_height; ++j) {
                                result += t[j];
                        }
                        t[h]++;
                }

                cout << result << '\n';
                return 0;
        }

        start_elm = node_id * n/no_of_nodes;
        stop_elm  = (node_id+1) * n/no_of_nodes;

//        cout << node_id << ": " << start_elm << " - " << stop_elm << '\n';

        //calc max_h
        max_h = 0;
        for(int i = start_elm; i < stop_elm; ++i) {
                h = GetElement(i);
                if (h > max_h) {
                        max_h = h;
                }
        }

        for(int i = 0; i < no_of_nodes; ++i) {
                if (i != node_id) {
                        PutInt(i, max_h);
                        Send(i);
                }
        }

        for(int i = 0; i < no_of_nodes; ++i) {
                if (i != node_id) {
                        Receive(i);
                        h = GetInt(i);
                        if (h > max_h) {
                                max_h = h;
                        }
                }
        }
        
        // all nodes know max_h

//        cout << node_id << ": " << "max_h " << max_h << '\n';

        for(int i = stop_elm; i < n; ++i) {
                h = GetElement(i);
                updateBIT(h, max_h);
        }

        for (int i = stop_elm-1; i >= start_elm; i--) { 
                h = GetElement(i);
                result += getSum(h-1); 
                updateBIT(h, max_h); 
        }         

        if (node_id > 0) {
                PutLL(0, result);
                Send(0);
                return 0;
        } else {
                for(int i = 1; i < no_of_nodes; ++i) {
                        long long tmp;
                        Receive(i);
                        tmp = GetLL(i);
                        result += tmp;
                }
        }

        cout << result << endl;
        
        return 0;
}