nyoj117&& tree-like array for reverse order number

Source: Internet
Author: User

(go) tree array can be used to find the number of reverse, of course, the general use of merging. If the data is not very large, you can insert into the tree array, each insert a number, the number of the count is smaller than he, the corresponding reverse is i-getsum(da Ta[i]), where I is the number of currently inserted numbers, getsum (da ta [i]) is smaller thanda Ta[i] number i-sum (data[i]) that is larger than data[i] number, that is, the number of reverse order, but if the data is larger, you must use the Discretization method. Discretization of a keyword: the so-called discretization is the establishment of a one-on mapping. In order to reverse the order, only the corresponding data must be requested.
The size relationship does not change.    such as: 10 30 20 40 50 with 1 3 2 4 5 The inverse number is the same definition of a struct struct node{int data;     corresponding data int pos; The input order of the data};
After sorting theda ta in ascending order, the POS value corresponds to the position ofthe DA Ta in the array before sorting. Then define an array p[n], which is the mapping of the original array. The following statement establishes a one by one mapping of the original array with 1 to N by size relationship.

Topic Link: Click here

Pre-function

Defines a lowbit function that returns the value represented by the position of the last 1 when the argument is converted to binary.

For example, the return value of Lowbit (34) will be 2, while Lowbit (12) returns 4;lowbit (8) returns 8.

Convert 34 to binary, 0010 0010, here the "last 1" refers to the number from the 2^0 bit forward, see the first 1, that is, the 2^1 bit 1.

program, ((not I) +1) and I indicate the last value of 1,

Still take 34 For example, the result of not 0010 0010 is 1101 1101 (221), plus one after 1101 1110 (222), 0010 0010 and 1101 1110 for and, 0000 0010 (2).

A simple way to find a lowbit: (c + +)

int lowbit (int x) {    return x& (-X);}

New

Define an array BIT to maintain the prefix of a and then:

Specific can be implemented in the following ways: (c + +)

void Build () {for    (int i=1;i<=max_n;i++)    {        bit[i]=a[i];        for (int j=i-1; j>i-lowbit (i); J-=lowbit (j))            Bit[i]+=bit[j];}    }

Modify

Suppose you now want to increase the value of the a[i] Delta,

Then, you need to add K to the value of the range containing a[i] covered by bti[i].

This process can be written as a recursive, or a normal loop.

The number of calculations required is related to the number of bits of the data size n, i.e. the time complexity of this part is O (logn)

modifying C + + notation for functions

void edit (int i, int delta) {for    (int j = i; J <= max_n; j + = Lowbit (j))        Bit[j] + = Delta;

sum
    1. First, the ANS is initialized to 0, and I is counted as K.
    2. Add the value of ans to bit[p]
    3. Subtract the value of I from Lowbit (i)
    4. Repeat steps, or both, until the value of I changes to 0

How to do the summation function in C + +

int sum (int k) {    int ans = 0;    for (int i = k; i > 0; I-= Lowbit (i))        ans + = bit[i];    return ans;}

Complexity of

Initialization complexity optimal for O (N)

One-time query complexity O (LOG (N)) where N is an array size

Single modification Complexity O (LONG (N)) where N is an array size

Space complexity O (N);

Code:

#include <math.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include < Iostream> #include <algorithm>using namespace std;const int Maxn=1000001;const double Eps=1e-6;const double pi= ACOs ( -1.0); #define LOWBIT (x) ((x) & (-X)) struct node{int data,pos;} doo[maxn];int n;int Coo[maxn],poo[maxn];int cmp (    const void *a,const void *b) {node *ta= (node *) A;    Node *tb= (node*) b; return ta->data-tb->data;} int cmp1 (node Aa,node bb) {return aa.data-bb.data;}    void Updata (int pos,int value) {int x=pos;        while (x<=n) {coo[x]+=value;    X+=lowbit (x);    }}int getsum (int pos) {int x=pos,sum=0;        while (x) {sum+=coo[x];    X-=lowbit (x);    }}int Main () {int T;    scanf ("%d", &t);        while (t--) {scanf ("%d", &n);            for (int i=1; i<=n; i++) {scanf ("%d", &doo[i].data);        Doo[i].pos=i;        } qsort (Doo+1,n,sizeof (doo[0]), CMP); SorT (DOO,DOO+N,CMP1);        int id=1;        Poo[doo[1].pos]=1;            for (int i=2; i<=n; i++) if (doo[i].data==doo[i-1].data) Poo[doo[i].pos]=id;        else Poo[doo[i].pos]=++id;        memset (coo,0,sizeof (COO));        Long Long ans=0;            for (int i=1; i<=n; i++) {updata (poo[i],1);        ans+= (Long Long) (I-getsum (Poo[i]));                  } printf ("%lld\n", ans);//int n,s=0;; Violence/int a[100];//scanf ("%d", &n);//for (int i=0; i<n; i++)//scanf ("%d", &a[i ]);//for (int i=0; i<n; i++)//for (int j=i+1; j<n; j + +)//{//if (a[j]&lt ; a[i])//s++;//}//printf ("%d\n", s);//}//} return 0;}

Merging sorting merge algorithm

#include <stdio.h> #include <string.h> #define MAXM 1000003#define INF 0x7fffffff-1;long long Cnt;int arr[ Maxm];int temp1[maxm/2+1], temp2[maxm/2+1];void merge (int array[], int start, int mid, int end)//merging algorithm in merge sort {int N1, n    2,i,k,j;    N1 = Mid-start + 1;    N2 = End-mid;    for (i = 0; i < N1; i++)//Copy the first half of the array {temp1[i] = Array[start + i];    } for (i = 0; i < n2; i++)//copy back half-part array {Temp2[i] = Array[mid + i + 1];               } TEMP1[N1] = temp2[n2] = INF; The following elements are set to a large for (k = start, I = 0, j = 0; k <= end; k++)//scan two fractional groups one by one and place them in the appropriate position {if (Temp1[i] <= te            Mp2[j]) {array[k] = Temp1[i];        i++;                            } else {cnt+=n1-i;            Number of reverse pairs array[k] = temp2[j];        j + +;      }}}//Merge sort void mergesort (int array[], int start, int end) {if (Start < end) {int i;  i = (end + start)/2;                                                   Sort the first half of the MergeSort (array, start, I);                                                   The second half is sorted mergesort (array, i + 1, end);    Merge two parts before and after merging (array, start, I, end);    }}int Main () {//freopen ("11.txt", "R", stdin);    Freopen ("2.txt", "w", stdout);    int t,i,n;    scanf ("%d", &t);        while (t--) {cnt=0;        scanf ("%d", &n);        for (i=0; i<n; i++) {scanf ("%d", arr+i);        } mergesort (arr,0,n-1);    printf ("%lld\n", CNT); } return 0;}


nyoj117&& tree-like array for reverse order number

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.