Codevs 4163 The number of reverse pairs-tree Array method

Source: Internet
Author: User

4163 Hzwer and reverse ordertime limit: ten sspace limit: 256000 KBtitle level: Golden GoldTitle Description Description

Hzwer in the study of reverse order.

For the sequence {a}, if the ordinal pair (i,j) satisfies: I<j,a[i]>a[j], then (I,J) is a pair of inverse pairs.

Given a sequence {a}, the number of inverse pairs is obtained.

Input data is large, please use scanf instead of CIN to read in.

Enter a description Input Description

The first row is a number n, which indicates that {a} has n elements.

Next n number, describe {a}.

Output description Output Description

A number that represents the number of reverse orders.

Sample input Sample Input

5

3 1 5) 2 4

Sample output Sample Output

4

Data range and Tips Data Size & Hint

For 10% data, 1<=n<=100.

For 20% data, 1<=n<=10000.

For 30% data, 1<=n<=100000.

For 100% data, 1<=n<=1000000,1<=a[i]<=10^8.

#include <cstdio>#include<iostream>using namespacestd; #include<algorithm>#include<cstring>#defineMAXN 1000001structNode {Long Longv; intID; BOOL operator< (ConstNode &p)Const{returnv<p.v;}}; Node A[MAXN+Ten];Long Long intc[maxn+Ten],b[maxn+Ten];intN;inlineintLowbit (intx) {    returnx& (-x);}Long LongQueryintx) {    Long Longans=0;  while(x) {ans+=C[x]; X-=lowbit (x); }    returnans;}voidChangeintx) {     while(x<=N) {c[x]++; X+=lowbit (x); }}intMain () {scanf ("%d",&N); Memset (A,0,sizeof(a)); Memset (c,0,sizeof(c)); memset (b,0,sizeof(b));  for(intI=1; i<=n;++i) {scanf ("%d",&(A[I].V)); A[i].id=i; } sort (A+1, a+n+1); intpre=-1; intPrevalue=0;  for(intI=1; i<=n;++i) {if(pre!=a[i].v) {Pre=a[i].v; A[I].V=++Prevalue; }        Elsea[i].v=Prevalue; }     for(intI=1; i<=n;++i) b[a[i].id]=a[i].v; Long Long ints=0;  for(inti=n;i>=1;--i) {change (b[i]); S+=query (b[i]-1); } cout<<s<<Endl; return 0;}
View Code

Codevs 4163 The number of reverse pairs-tree Array method

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.