-
Notifications
You must be signed in to change notification settings - Fork 3
/
CountOfSmallerNumberBeforeItself.cpp
127 lines (123 loc) · 3.22 KB
/
CountOfSmallerNumberBeforeItself.cpp
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
#include <fstream>
#include <iostream>
#include <vector>
#include <string.h>
using namespace std;
class STNode{
public:
int start, end;
int count;
STNode * left, * right;
STNode(int start, int end){
this->start = start;
this->end = end;
this->count = 0;
this->left = NULL;
this->right = NULL;
}
};
class SegmentTree{
public:
STNode * _root;
SegmentTree(int start, int end){
_root = build(start, end);
}
STNode * build(int start, int end){
if(start > end){
return NULL;
}
STNode * root = new STNode(start, end);
if(start < end){
int mid = start + (end - start) / 2;
root->left = build(start, mid);
root->right = build(mid+1, end);
}
return root;
}
int query(STNode* root, int start, int end){
if(root == NULL || start > end){
return 0;
}
if(start == root->start && end == root->end){
return root->count;
}
int mid = root->start + (root->end - root->start) / 2;
int leftCount = 0;
if(start <= mid){
if(mid < end){
leftCount = query(root->left, start, mid);
}
else{
leftCount = query(root->left, start, end);
}
}
int rightCount = 0;
if(mid < end){
if(start <= mid){
rightCount = query(root->right, mid+1, end);
}
else{
rightCount = query(root->right, start, end);
}
}
return leftCount + rightCount;
}
void update(STNode* root, int index, int value){
if(root == NULL){
return;
}
if(root->start == index && index == root->end){
root->count += value;
return;
}
int mid = root->start + (root->end - root->start) / 2;
if(index <= mid){
update(root->left, index, value);
}
else{
update(root->right, index, value);
}
root->count = root->left->count + root->right->count;
}
};
class Solution {
public:
/**
* @param A: An integer array
* @return: Count the number of element before this element 'ai' is
* smaller than it and return count number array
*/
vector<int> countOfSmallerNumberII(vector<int> &A) {
// write your code here
SegmentTree *stree = new SegmentTree(0, 10001);
vector<int> result;
for(int i = 0; i < A.size(); i++){
int count = stree->query(stree->_root, 0, A[i]-1);
result.push_back(count);
stree->update(stree->_root, A[i], 1);
}
delete stree;
return result;
}
};
vector<int> loadData(string filename){
vector<int> result;
std::fstream fs;
fs.open(filename, std::fstream::in);
while((fs.rdstate() & std::ifstream::eofbit) == 0){
char num[24]={0};
fs.getline(num, sizeof(num), ',');
int n = atoi(num);
result.push_back(n);
}
fs.close();
return result;
}
int main(void){
vector<int> A = loadData("19.in");
Solution sol;
vector<int> result = sol.countOfSmallerNumberII(A);
for(int x : result){
}
return 0;
}