A request sequence number in the left of each smaller number than its number (Fenwick tree)

Given a sequence of positive integers N A (N <= 10 ^ 5, A [i] <= 10 ^ 5), for each number sequence, a sequence which is smaller than the number of its left obtained number.

 

Ideas: Classic Application tree array (bare title)

. 1 #include <the iostream>
 2 #include <algorithm>
 . 3 #include <CString>
 . 4  
. 5  the using  namespace STD;
 . 6  
. 7  const  int N = 100010 ;
 . 8  
. 9  int C [N];
 10  
. 11  int lowbit ( int X) { / / obtaining a rightmost. 1 
12 is      return X & (- X);
 13 is  }
 14  
15  int GetSum ( int X) { // interval and 1 ~ x log (N) 
16      int SUM =0 ;
17     for(int i=x;i>0;i-=lowbit(i)){
18         sum += c[i] ;
19     }
20     return sum ;
21 }
22 
23 void update(int x,int v){//单点更新 log(N)
24     for(int i=x;i<=N;i+=lowbit(i)){
25         c[i] += v ;
26     }
27 }
28 
29 
30 int main(){
31     int n, x ;
32     cin >> n ;
33     
34     memset(c,0,sizeof c) ;
35     
36     for(int i=0;i<n;i++){
37         cin >> x ;
38         update(x,1) ;
39         cout << getSum(x-1) << endl ;        
40     }
41     
42     return 0 ;
43 } 

Guess you like

Origin www.cnblogs.com/gulangyuzzz/p/12048533.html