标签:
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> using namespace std; #define M 35000 #define N 50050 #define ll long long #define lowbit(x) (x&-x) int tree[M]; int num[N]; ll tmp[N]; int sum(int rt) { int s=0; while(rt>0){ s+=tree[rt]; rt-=lowbit(rt); } return s; } void update(int rt) { while(rt<=M-1){ tree[rt]++; rt+=lowbit(rt); } } int main() { int n; while(~scanf("%d",&n)){ memset(tree,0,sizeof(tree)); for(int i=1;i<=n;i++){ scanf("%d",&num[i]); num[i]++; } ll ans=0; for(int i=1;i<=n;i++){ tmp[i]= sum(num[i]-1); update(num[i]); } memset(tree,0,sizeof(tree)); for(int i=n;i>=1;i--) { ans+=tmp[i]*(ll)sum(num[i]-1); update(num[i]); } printf("%lld\n",ans); } return 0; }
HOJ 2275 Number sequence(树状数组)
标签:
原文地址:http://blog.csdn.net/kalilili/article/details/43915671