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
151
152
153
154
155
156
157
158
159
160
#include "teatr.h"
#include "message.h"

#include <algorithm>
#include <stdint.h>
#include <cstdio>
#include <vector>
#include <set>

using namespace std;

/******************************************************/

static uint32_t ID, NODES, N;
void init() {
    NODES = NumberOfNodes();
    ID = MyNodeId();
    N = GetN();
}

/******************************************************/

struct Node {
    int Lvalue, Rvalue, count;
    Node *Lchild, *Rchild;
    Node(int Lvalue, int Rvalue) {
        this->Lchild = this->Rchild = 0;
        this->Lvalue = Lvalue;
        this->Rvalue = Rvalue;
        this->count = 0;
    }

    void add(int k, int v) {
        if (k<Lvalue || k>Rvalue) return;
        count += v;
        if (Lchild) Lchild->add(k, v);
        if (Rchild) Rchild->add(k, v);
    }

    int getRsum(int k) {
        if (k>Rvalue) return 0;
        if (k<Lvalue) return count;
        int result = 0;
        if (Lchild) result += Lchild->getRsum(k);
        if (Rchild) result += Rchild->getRsum(k);
        return result;
    }

    void clear() {
        if (Lchild){ Lchild->clear(); delete Lchild; }
        if (Rchild){ Rchild->clear(); delete Rchild; }
    }

};

struct Tree {
    Node *root;

    Tree(vector<int> &P) {
        vector<Node*> V;
        sort(P.begin(), P.end());
        for(int i=0; i<P.size(); ++i) {
            if (i>0 && P[i]==V.back()->Lvalue) continue;
            V.push_back(new Node(P[i], P[i]));
        }
        P.clear();
        while (V.size() > 1) {
            int new_size = 0;
            for(int i=1; i<V.size(); i+=2) {
                Node *node = new Node(V[i-1]->Lvalue, V[i]->Rvalue);
                node->Lchild = V[i-1]; node->Rchild = V[i];
                V[new_size] = node;
                ++new_size;
            }
            if (V.size()%2 == 1){
                V[new_size] = V.back();
                ++new_size;
            }
            V.resize(new_size);
        }
        this->root = V[0];
    }

    void add(int k, int v){ this->root->add(k, v); }
    int getRsum(int k) { return this->root->getRsum(k); }
    void clear() {
        root->clear();
        delete root;
    }
};

uint64_t solve_local(int first, int last) {
    vector<int> P;
    P.resize(last-first+1); for(int i=0; i<P.size(); ++i) P[i] = GetElement(first+i);
    Tree T(P);
    P.resize(last-first+1); for(int i=0; i<P.size(); ++i) P[i] = GetElement(first+i);
    uint64_t result = 0;
    for(int i=0; i<P.size(); ++i) {
        result += T.getRsum(P[i]);
        T.add(P[i], 1);
    }
    T.clear();
    return result;
}

int main() {
    init();

    if (N <= 2000000) {
        if (ID == 0) {
            uint64_t result = solve_local(0, N - 1);
            printf("%llu\n", result);
        }
    } else {
        int num_of_groups = min((uint32_t)40, NODES-1);
        vector<pair<uint32_t, uint32_t> > bound(num_of_groups);
        for(int i=0; i<num_of_groups; ++i) {
            int left = ((uint64_t)(N)*(uint64_t)(i))/num_of_groups;
            int right = ((uint64_t)(N)*(uint64_t)(i+1))/num_of_groups-1;
            bound[i] = make_pair(left, right);
        }

        if (ID == 0) {
            uint64_t result = 0;
            for(int i=0; i<num_of_groups; ++i){
                PutInt(i+1, bound[i].first);
                PutInt(i+1, bound[i].second);
                Send(i+1);
            }

            vector<uint64_t> P(1000005, 0);
            for(int i=0; i<num_of_groups; ++i){
                vector<uint64_t> V(1000005, 0);
                for (int x=bound[i].first; x<=bound[i].second; ++x) ++V[GetElement(x)];
                for (int x=1000000; x>0; --x) result += (uint64_t)V[x] * (uint64_t)P[x+1];
                uint64_t vv = 0;
                for (int x=1000000; x>0; --x) {
                    vv += V[x];
                    P[x] += vv;
                }
            }

            for(int i=0; i<num_of_groups; ++i) {
                Receive(i+1);
                uint64_t sub_result = GetLL(i+1);
                result += sub_result;
            }
            printf("%lld\n", result);
        } else if (ID <= num_of_groups) {
            Receive(0);
            uint32_t left = GetInt(0);
            uint32_t right = GetInt(0);
            uint64_t result = solve_local(left, right);
            PutLL(0, result);
            Send(0);
        }
    }

    return 0;
}