Fenwick tree collection title

Single-point update interval query

Enemy soldiers lineup 

Pit: cin cout timeout has been changed to scanf and printf pass by

/ * Input 
t samples 
N (N <= 50000) 
of the positive integer i ai represents the i th engineers have camps at the start of the personal ai (1 <= ai <= 50 ). 

Next, each row having a command, command has four forms: 
(. 1) ij of the Add, i, and j is a positive integer, denotes the i th camp increase personal j (j is not more than 30) 
(2) Sub ij of, i and j It is a positive integer, denotes the i th camps reduce j individuals (j does not exceed 30); 
(. 3) Query ij of, i and j are positive integers, i <= j, presents to ask the total number of i-th to j-th camps; 
(4) end indicates the end, the last command that appears in each set of data; 

. 1 
10 
. 1. 5. 4. 3 2. 6. 7. 9 10. 8 
Query. 3. 1 
the Add. 3. 6 
Query 2. 7 
Sub 10 2 
the Add. 6. 3 
Query. 3 10 
end   


output 
Case . 1: 
. 6 
33 is 
59 
* / 

#include < String .h> 
#include <stdio.h>
 the using  namespace STD;
int n-, T [ 50005 ];
 int lowbit ( int X) 
{ 
     return X & (- X); 
} 

void Update ( int X, int Val) // single point updating 
{
     the while (X <= n-) { 
        
        T [X] = + Val; 
        X + = lowbit (X); // update Fenwick tree leaf node C from the bottom up, from left to right to update 
    } 
} 

int ASK ( int X) // the request interval [1, i] for all elements i.e., the prefix and seeking and 
{
     int ANS = 0 ;
     the while (X>0){
        ans+=t[x];//从右往左累加求和
        x-=lowbit(x);
    }
    return ans;
}

int main()
{
    int tt,cnt=0,b,c,i,a;
    char s[10];
    scanf("%d",&tt);
    while(tt--)
    {
        scanf("%d",&n);
        memset(t,0,sizeof(t));
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a);
            update(i,a);
        }
         printf("Case %d:\n",++cnt);
        while(scanf("%s",s))
        {
             if(s[0]=='E') break;
             scanf("%d%d",&c,&b);
             if(s[0]=='S') update(c,-b);
            
            if(s[0]=='A') update(c,b);
            
            if(s[0]=='Q') 
            {
                printf("%d\n",ask(b)-ask(c-1)); 
            }               
        }
    }
    return 0;
} 
View Code

 Ultra-QuickSort

The title of the discrete data, demand data using the Fenwick tree reverse order , that is, the number of exchanges ultrafast row.

/ * 
In this issue, you have to analyze specific sorting algorithm. The algorithm by switching adjacent two sequence elements, 
until the sequence is sorted in ascending order, to process the n different sequences of integers. For an input sequence 
. 9. 1. 4 0. 5, 

Ultra-generating outputs the QuickSort 
01,459. 
Your task is to determine how many times Ultra-QuickSort exchange operations you can perform on a given input sequence order. 

Entry 
input contains several test cases. Are at the beginning of each test case contains a single integer n <500,000 (the length of the input sequence) line. 
The next n lines each row contains an integer, i.e. 0≤a [i] ≤999,999,999, i.e. the i-th element of the input sequence. Input from the termination sequences of length n = 0. 
Shall not handle this sequence. 

Output 
for each input sequence, the program prints a line comprising a single line integer op, op given input sequence is the minimum number of switching operations required for sorting. 

INPUT 
. 5 
. 9 
. 1 
0 
. 5 
. 4 
. 3 
. 1 
2 
. 3 
0 

the Output 
. 6 
0 

* /  

#include < String .h>
#include <stdio.h> 
#include <algorithm>
 #define MAXN 500.01 thousand
 the using  namespace STD;
 Long  Long  int   n-, A [MAXN], B [MAXN], T [MAXN];
 int lowbit ( int X) 
{ 
     return X & ( - X); 
} 

void update ( int X, int Val) // single point updating W 
{
     the while (X <= n-) { 
        
        T [X] + = Val; 
        X + = lowbit (X); // a leaf node update up Fenwick tree C, from left to right to update 
    } 
} 

Long Long  int ASK ( int X) // the request interval [1, i] and all the elements required i.e. prefixes and 
{
     Long  Long  int   ANS = 0 ;
     the while (X> 0 ) { 
        ANS + T = [X]; // cumulative sum from right to left 
        X-= lowbit (X); 
    } 
    return ANS; 
} 



int main () 
{ 
    Long  Long  int ANS = 0 ;
     // where subscript a starting sequence generally be from 0 may be 
    the while (Scanf ( " % LLD " , & n-)) 
    {
        IF (! n-) BREAK ;
         for ( int I = . 1 ; I <= n-; I ++ ) 
        { 
            Scanf ( " % LLD " , & A [I]); 
            B [I] = A [I]; // B is a temporary array, to obtain discrete mapping relationship 
        }
         // below uses the STL sort (sorted), UNIQUE (de-emphasis), lower_bound (lookup) function 
        sort (B + . 1 , B + n-+ . 1 ); / / sort 
        int size = UNIQUE (B + . 1 , B + . 1 + n-) - B - . 1 ; // to weight and weight to obtain the length size
        for ( int I = . 1 ; I <= n-; I ++ ) 
        { 
            A [I] = lower_bound (B + . 1 , B + . 1 + size, A [I]) - B; // Find by binary quickly the elements and mapping association 
            Update (A [I], . 1 ); 
            ANS + = (I- ASK (A [I])); 
            
        } 
        the printf ( " % LLD \ n- " , ANS); 
        ANS = 0 ; 
        Memset (A, 0 , the sizeof (A)); 
        Memset (B, 0 , the sizeof (B)); 
        Memset (T,0,sizeof(t));
    }
    return 0;
} 
View Code

 

Guess you like

Origin www.cnblogs.com/young-children/p/11755911.html