-
Notifications
You must be signed in to change notification settings - Fork 0
/
2517.cpp
56 lines (44 loc) · 1.19 KB
/
2517.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
#include<iostream>
#include<vector>
#include<algorithm>
#define N 500005
using namespace std;
typedef vector<int> vi;
int n, arr[N], goal, srt[N], tree[N*3];
int get_tree(int left, int right, int index)
{
if(right<1 || left>goal) return 0;
if(1<=left && right<=goal) return tree[index];
int mid=(left+right)/2;
return get_tree(left, mid, index*2) + get_tree(mid+1, right, index*2+1);
}
int update_tree(int left, int right, int index)
{
if(right<goal || left>goal) return tree[index];
if(left==right) return ++tree[index];
int mid=(left+right)/2;
return tree[index] = update_tree(left, mid, index*2) + update_tree(mid+1, right, index*2+1);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
int i;
cin >> n;
for(i=1; i<=n; i++){
cin >> arr[i];
srt[i]=arr[i];
}
sort(srt+1, srt+n+1);
for(i=1; i<=n; i++) arr[i]=lower_bound(srt+1, srt+n+1, arr[i])-srt;
for(i=1; i<=n; i++){
goal=arr[i]-1;
int ret = get_tree(1, n, 1);
printf("%d\n", i-ret);
goal=arr[i];
update_tree(1, n, 1);
}
return 0;
}