PKU 2299 solving the number of reverse orders (using a merge or tree array) tree array and getting started

Source: Internet
Author: User

Merge the AC code:

#include <iostream>#include <fstream>using namespace std;#define MAX 500005int a[MAX], t[MAX];__int64 cnt;void Merge(int l, int mid, int r){int i = l, j = mid + 1, k = 0;while(i <= mid && j <= r) {if(a[i] > a[j]) {t[k++] = a[j++];cnt += mid - i + 1;} else {t[k++] = a[i++];}}while(i <= mid) t[k++] = a[i++];while(j <= r) t[k++] = a[j++];for(i = 0; i < k; i++)a[l+i] = t[i];}void MergeSort(int l, int r) {int mid = (l + r) / 2;if(l < r) {MergeSort(l, mid);MergeSort(mid+1, r);Merge(l, mid, r);}}int main(){int n, i;while(scanf("%d", &n) != EOF && n) {for(i = 0; i < n; i++) scanf("%d", &a[i]);cnt = 0;MergeSort(0, n-1);printf("%I64d\n", cnt);}return 0;}

The tree array mainly refers to this image:

inline int Lowbit(int x){return x & (-x);}void Update(int x, int c){int i; for (i = x; i < maxn; i += Lowbit(i)){tree[i] += c; }}int Getsum(int x){ int i;int temp(0); for (i = x; i >= 1; i -= Lowbit(i)){ temp += tree[i]; }return temp;}

The above three functions can be said to be the "housekeeping skills" of the tree array. The efficiency of the tree array is reflected in these three functions.

Lowbit (X) is used to obtain 2 ^ P (where p is the rightmost position of 1 in the binary representation of X). For example, the binary representation of 6 is 110, the rightmost 1 is 1, so lowbit (6) = 2 ^ 1 = 2.
Update (x, c) is to change the value of X to C. If the General array changes x itself, but in the tree array, X + lowbit (x), X + lowbit (x ))),...) The point of this path must change C, so that the sum can be efficiently summed later. (Note that the value of X may be the actual value or the subscript of the array. If it is too large, discretization is required)
Getsum (x) is the result of (1 ,... X-lowbit (x), x-lowbit (x), x), and, in other words, it is equivalent to finding the sum from a [1] to a [X.

The efficiency of the tree array is: Unlike the General array, the general array is a subscript that is constantly added to traverse, while the tree array is constantly updated by adding 2 ^ P, so the efficiency is (logn) level.

The most basic function of a tree array is to calculate the number of points smaller than a certain point X (here the comparison is an abstract concept, which can make the size of the number, the size of the coordinates, quality ).
For example, given an array a [5] = {2, 5, 3, 4, 1 }, evaluate B [I] = the number of places on the left where I is less than or equal to a [I. for example, B [5] = {0, 1, 1, 2, 0}, which is the most orthodox tree Array application, directly traversing the array, get getsum (A [I]) for each position, and then modify the tree array update (A [I], 1. When the range of numbers is large, discretization is required, that is, sorting the order and re-numbering. For example, if a [] = {10000000, 10,200 0, 20,300}, after discretization, a [] = {5, 1, 4, 2, 3 }. (This is an example)

Details: http://download.csdn.net/detail/vsooda/4985249

# Include <iostream> # include <algorithm> # define Max 500010 using namespace STD; typedef struct arr {int N, IND;} arr; arr A [Max]; long sum; int C [Max], AA [Max]; // C Save the number of exchanges, AA Save the discretization result bool CMP (arr a, arr B) {return. n> B. N ;}int lowbit (int x) {return X & (-x) ;}void Update (int x) {While (x <max) {C [x] ++; X + = lowbit (x) ;}} int getsum (int x) {int sum = 0; while (x> 0) {sum + = C [x]; x-= lowbit (x);} return sum;} in T Main () {int N, I, TMP; while (~ Scanf ("% d", & N) {sum = 0ll; memset (C, 0, sizeof (c); for (I = 1; I <= N; I ++) {scanf ("% d", & A [I]. n); A [I]. IND = I; // remember subscript} Sort (a + 1, A + n + 1, CMP); int p = 1; AA [A [1]. IND] = P; TMP = A [1]. n; for (I = 1; I <= N; I ++) // discretization {if (a [I]. N = TMP) AA [A [I]. IND] = P; else {TMP = A [I]. n; P ++; AA [A [I]. IND] = P ;}}for (I = 1; I <= N; I ++) {sum + = getsum (AA [I]); update (AA [I]);} printf ("% LLD \ n", sum);} return 0 ;}

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.