SGU180 (tree-like array, reverse order, discrete)

Source: Internet
Author: User

Inversionstime Limit per test:0.25 sec.
Memory limit per test:4096 Kbinput:standard
Output:standard




There is N integers (1<=n<=65537) A1, A2,.. An (0<=ai<=10^9). You need to find amount of such pairs (I, j) that 1<=i<j<=n and A[i]>a[j].
InputThe first line of the input contains the number N. The second line contains N numbers A1 ... An.
OutputWrite amount of such pairs.
Sample Test (s)
Input5
2 3 1) 5 4
Output3

This problem needs to be discrete, the tree-like array is discrete after the inverse, statistics added to the element when the current array

How many larger numbers already exist, which is the contribution of the number to the latter in reverse order, and then you can

Solved, the general need discretization.

1#include <cstdio>2#include <algorithm>3#include <cmath>4#include <cstring>5#include <iostream>6 #defineN 700077 using namespacestd;8 9 intN;Ten Long Longans; One intB[n],c[n]; A structNode - { -     intZhi,id; the }a[n]; -  - BOOLcmp (Node x,node y) - { +     returnx.zhi<Y.zhi; - } + intLowbit (intx) A { at     returnx& (-x); - } - voidChangeintXinty) - { -      for(inti=x;i<=n;i+=lowbit (i)) -c[i]+=y; in } - intQueryintx) to { +     intres=0; -      for(inti=x;i>=1; i-=lowbit (i)) theres+=C[i]; *     returnRes;  $ }Panax Notoginseng intMain () - { thescanf"%d",&n); +      for(intI=1; i<=n;i++) A     { thescanf"%d",&A[i].zhi); +A[i].id=i;  -     } $Sort (A +1, a+n+1, CMP); $     intCnt=0; -      for(intI=1; i<=n;i++) -     { the         if(a[i].zhi!=a[i-1].zhi) b[a[i].id]=++CNT; -         Elseb[a[i].id]=CNT;Wuyi     } the      for(intI=1; i<=n;i++) -     { WuChange (B[i),1); -Ans=ans+query (N)-query (B[i]);  About     } $printf"%lld", ans); -}

SGU180 (tree-like array, reverse order, discrete)

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.