Skip to content

Instantly share code, notes, and snippets.

@luccasiau
Created April 13, 2015 03:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luccasiau/23016d5534d7b788163a to your computer and use it in GitHub Desktop.
Save luccasiau/23016d5534d7b788163a to your computer and use it in GitHub Desktop.
#include <cstdio>
#include <cstring>
#define MAXN 100010
#define MAXS 1000010
#define max(A,B) (A)>(B)?(A):(B)
int MAX = 1000001;
int bit[MAXS];
void insere(int x){
while(x < 1000005){
bit[x]++;
x += (x & -x);
}
}
int soma_ate(int x){
int s = 0;
while(x > 0){
s += bit[x];
x -= (x & -x);
}
return s;
}
int main(){
int n;
memset(bit,0,sizeof bit);
scanf("%d",&n);
int resp=0;
int hab[MAXN];
int maior=0;
for(int i = 1;i<=n;i++){
scanf("%d",&hab[i]);
maior = max(maior,hab[i]);
}
for(int i = 1;i<=n;i++){
insere(hab[i]);
resp += soma_ate(maior) - soma_ate(hab[i]);
}
printf("%d\n",resp);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment