2011-04-07 14:01:31Morris

Binary Indexed Tree (BIT)

/**********************************************************************************/
/*  Problem: d788 "排名順序" from ST | BIT | AVL                              */
/*  Language: C                                                                   */
/*  Result: AC (256ms, 586KB) on ZeroJudge                                        */
/*  Author: morris1028 at 2011-04-07 13:45:04                                     */
/**********************************************************************************/


#include<stdio.h>
#include<stdlib.h>
int C[131073];/*2^17=131072*/
int Lowbit(int N) {
    return N&(-N);
}
int Modify (int N, int L) {
    while(N<=L) {
        C[N]+=1;
        N+=Lowbit(N);
    }
}
int Operator (int N) {
    int Sum=0;
    while(N) {
        Sum+=C[N];
        N-=Lowbit(N);
    }
    return Sum;
}
int Input() {   
    char cha;   
    int x=0;
    while(cha=getchar())
        if(cha!=' ' && cha!='\n') break;   
    x=cha-48;   
    while(cha=getchar()) {
        if(cha==' ' || cha=='\n') break;   
        x=x*10+cha-48;   
    }
    return x;   
}
main() {
    int N, M, a, B2[20]={1}, L;
    for(a=1;a<20;a++)
        B2[a]=B2[a-1]*2;
    while(scanf("%d",&N)==1) {
            for(a=0;a<20;a++)
                if(B2[a]>=N) break;
            L=B2[a];
            for(a=1;a<=L;a++)   C[a]=0;
            
            for(a=1;a<=N;a++) {
                M=Input();
                Modify (M, L);
                printf("%d\n",a-Operator(M-1));
            }
    }
    return 0;
}