Inverse logarithm of a tree-like array

Source: Internet
Author: User

Ultra-quicksort In this problem, you has to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping, adjacent sequence elements until the sequence is Sorted in ascending order. For the input sequence
9 1 0 5 4,
Ultra-quicksort produces the output
0 1 4 5 9.
Your task is to determine what many swap operations Ultra-quicksort needs to perform in order to sort a given input sequenc E.

Input

The input contains several test cases. Every test case begins with a line this contains a single integer n < 500,000-the length of the input sequence. Each of the following n lines contains a single integer 0≤a[i]≤999,999,999, the i-th input sequence element. Input is terminated by a sequence of length n = 0. This sequence must is processed.

Output

For every input sequence, your program prints a single line containing an integer number OP, the minimum number of swap op Erations necessary to sort the given input sequence.

Sample Input

59105431230

Sample Output

60

The title is the sum of the numbers in front of each number that is larger than it is the inverse logarithm. As long as it is discretization can be done. I-query (Index[i]): Current is the number of I,query (Index[i]) is it before it is smaller than its numbers, minus it is the number of the front larger than it.
1#include <iostream>2#include <stdio.h>3#include <string.h>4#include <algorithm>5 #defineLowbit (x) x& (-X)6 #definell Long Long7 using namespacestd;8 Const intMAX = 5e5+5;9 intN, Tree[max],index[max];Ten structnod{ One     intNum,id; A }nod[max]; - BOOLCMP (nod A, nod b) { -     returnA.num <B.num; the } - voidAddintXinty) { -      while(X <MAX) { -TREE[X] + =y; +X + =lowbit (x); -     } + } A  at intQueryintx) { -     intsum =0; -      while(X >0){ -Sum + =Tree[x]; -X-=lowbit (x); -     } in     returnsum; - } to intMain () { +      while(SCANF ("%d", &n) &&N) { -          for(inti =1; I <= N; i + +){ thescanf"%d",&nod[i].num); *Nod[i].id =i; $         }Panax NotoginsengSort (nod+1, nod+1+n,cmp); -          for(inti =1; I <= N; i + +) index[nod[i].id] =i; thememset (Tree,0,sizeof(tree)); +ll ans =0; A          for(inti =1; I <= N; i + +){ theAdd (Index[i],1); +             //printf ("%d+++++%d\n", Index[i],query (Index[i])); -Ans + =-query (Index[i]); $         } $printf"%lld\n", ans); -     } -}

Enemy is weak

The Romans had attacked again. This time they was much more than the Persians but Shapur was ready to defeat them. He says: "A lion is never afraid of a hundred sheep".

Nevertheless Shapur have to find weaknesses in the Roman army to defeat them. So he gives the army a weakness number.

In Shapur's opinion the weakness of an army are equal to the number of triplets i ,? J ,? k  such that  i ? <? j ? <? k  and  a i ? a J ? a K  where   a x  is the Power of man standing at position   x . The Roman Army has a special trait-powers of all the people in it is distinct.

Help Shapur find out how weak the Romans is.

Input

The first line of input contains a single number n (3?≤? N? ≤?106)-the number of men in Roman army. Next line contains n different positive integers ai ( C17>1?≤? i? ≤? n,? 1?≤? a i? ≤?109)-powers of men in the Roman army.

Output

A single integer number, the weakness of the Roman army.

%lld specificator to read or write 64-bit integers in C + +. It's preffered to use cout (also ).

Example

Input
3
3 2 1
Output
1
Input
3
2 3 1
Output
0
Input
4
10 8 3 1
Output
4
Input
4
1 5 4 3
Output
1


This is to ask for three increments, also can be regarded as the number of I, the number of the front larger than it and the number of smaller than it. The following k is larger than the number in front of it,
Ask for the number smaller than it is: Because K is the number of numbers in front of number I, so the number of smaller
(N-index[i])-(I-1-K) is n-i-index[i]+k+1, (N-index[i]) is smaller than the number of
Total, subtract it from the number of smaller than the previous number is the amount of the back, the front is smaller than the number of
The number is (I-1-k), so (N-index[i])-(i-1-k) is pushed
1#include <iostream>2#include <stdio.h>3#include <string.h>4#include <algorithm>5 #defineLowbit (x) x& (-X)6 #definell Long Long7 using namespacestd;8 Const intMAX = 1e6+Ten;9 intN,tree[max],index[max];Ten intNex[max],pre[max]; One structnod{ A     intVal,id; - }nod[max]; - BOOLCMP (nod A, nod b) { the     returnA.val >B.val; - } - voidAddintx) { -      while(X <MAX) { +TREE[X] + +; -X + =lowbit (x); +     } A } at intQueryintx) { -     intsum =0; -      while(X >0){ -Sum + =Tree[x]; -X-=lowbit (x); -     } in     returnsum; - } to intMain () { +scanf"%d",&n); -      for(inti =1; I <= N; i + +) scanf ("%d", &nod[i].val), nod[i].id=i; theSort (nod+1, nod+1+n,cmp); *      for(inti =1; I <= N; i + +) index[nod[i].id] =i; $ll ans =0, K;Panax Notoginseng      -      for(inti =1; I <= N; i + +){ theK =query (Index[i]); +Ans + = k* (n-i-index[i]+k+1); A Add (Index[i]); the     } +cout << ans <<Endl; -     return 0; $}

Inverse logarithm of a tree-like array

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.