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
#include "teatr.h"
#include <iostream>
#include "message.h"

using namespace std;

struct rNode
{
  int eStart;
  int eMed;
  int eFinish;
  long long eValue;
  struct rNode *up;
  struct rNode *left;
  struct rNode *right;
};

rNode *Dat[1000010];
rNode *Root;

struct rNode* newNode(rNode *aUp, int aStart, int aFinish)
{
  struct rNode* oNode = new rNode;

  oNode->up     = aUp;
  oNode->eStart = aStart;
  oNode->eMed   =(aStart+aFinish)/2;
  oNode->eFinish= aFinish;
  oNode->eValue = 0;

  oNode->left = nullptr;
  oNode->right = nullptr;
  return oNode;
}

rNode* TreeCreate( rNode *aUp, int aStart, int aFinish){
    rNode * oNode = newNode( aUp, aStart,aFinish);
    if(aStart<aFinish){
        int oMed = (aStart+aFinish)/2;
        oNode->left = TreeCreate( oNode, aStart, oMed);
        oNode->right = TreeCreate( oNode, oMed+1, aFinish);
    }else{
        Dat[aStart] = oNode;
    }
    return oNode;
}

long long TestTall(int aHeigth){
    rNode *oNode = Root;
    long long oSum = 0;
    while(oNode!=nullptr){
        if(aHeigth<=oNode->eMed){
            rNode *oTemp = oNode->right;
            if(oTemp!=nullptr){oSum+=oTemp->eValue;};
            oNode = oNode->left;
        }else{
            oNode = oNode->right;
        }
    }
    return oSum;
}

void SidTall(int aHeigth){
    rNode* oNode = Dat[aHeigth];
    while (oNode!=nullptr){
        oNode->eValue++;
        oNode = oNode->up;
    }
}

void ReadData(){
    int ItemCount = GetN();
    long long oSum = 0;
    for(int oLoop=0;oLoop<ItemCount;oLoop++){
        int oItem = GetElement(oLoop);
        oSum += TestTall(oItem);
        SidTall(oItem);
    }
    cout << oSum <<endl;
}

int main()
{
    Root = TreeCreate(nullptr,0,1000001);
    if(MyNodeId()==0){
        ReadData();
    }
    return 0;
}