OJ题号:ZHOJ1055
思路:树状数组。
首先将数据离散化,然后用线段树维护小于当前高度的山峰已经出现过的数量。
#include<cstdio>
#include<cstring>
#include<algorithm>
const int N=,root=;
int n;
class FenwickTree {
private:
int val[N<<];
public:
FenwickTree() {
memset(val,,sizeof val);
}
int lowbit(const int x) {
return x&-x;
}
int query(int p) {
int ans=;
while(p) {
ans+=val[p];
p-=lowbit(p);
}
return ans;
}
void modify(int p) {
while(p<=n) {
val[p]++;
p+=lowbit(p);
}
}
};
FenwickTree tree;
int main() {
scanf("%d",&n);
int a[n+],b[n+];
for(int i=;i<=n;i++) {
scanf("%d",&a[i]);
b[i]=a[i];
}
std::sort(&b[],&b[n+]);
int ans[n+];
for(int i=;i<=n;i++) {
int t=std::lower_bound(&b[],&b[n+],a[i])-&b[];
ans[i]=tree.query(t-);
tree.modify(t);
}
for(int i=;i<=n;i++) printf("%d ",ans[i]);
printf("\n");
return ;
}