POJ 2299 Ultra-QuickSort(线段树入门)

时间:2021-12-05 16:59:07

Ultra-QuickSort

Time Limit: 7000MS
Memory Limit: 65536K

Description

In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping two 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 how many swap operations Ultra-QuickSort needs to perform in order to sort a given input sequence.

Input

The input contains several test cases. Every test case begins with a line that contains a single integer n < 500,000 -- the length of the input sequence. Each of the 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 not be processed.

Output

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

Sample Input

5
9
1
0
5
4
3
1
2
3
0

Sample Output

6
0

::本题其实就是要求出有多少逆序对。本题还要虚拟化,因为0<=a[i]<=999,999,999,开一个数组大小为1,000,000,000*4铁定超内存

   1: #include <iostream>

   2: #include <cstdio>

   3: #include <cstring>

   4: #include <algorithm>

   5: using namespace std;

   6: #define lson l,m,rt<<1

   7: #define rson m+1,r,rt<<1|1

   8: typedef long long ll;

   9: const int maxn=555555;

  10: int col[maxn<<2];

  11: int a[maxn],b[maxn],n;

  12:  

  13: void build(int l,int r,int rt)

  14: {

  15:     col[rt]=0;

  16:     if(l==r) return ;

  17:     int m=(r+l)>>1;

  18:     build(lson);

  19:     build(rson);

  20: }

  21:  

  22: int find(int x)

  23: {

  24:     int l=1,r=n;

  25:     while(l<=r)

  26:     {

  27:         int m=(l+r)>>1;

  28:         if(x==a[m]) return m;

  29:         if(x>a[m]) l=m+1;

  30:         else r=m-1;

  31:     }

  32:     return 0;

  33: }

  34:  

  35: void update(int p,int l,int r,int rt)

  36: {

  37:     col[rt]++;

  38:     if(l==r) return ;

  39:     int m=(l+r)>>1;

  40:     if(p<=m) update(p,lson);

  41:     else update(p,rson);

  42: }

  43:  

  44: int query(int p,int l,int r,int rt)

  45: {

  46:     if(p<=l) return col[rt];

  47:     int m=(l+r)>>1;

  48:     int ans=0;

  49:     if(p<=m)

  50:         ans=col[rt<<1|1]+query(p,lson);

  51:     else

  52:         ans=query(p,rson);

  53:     return ans;

  54: }

  55:  

  56: int main()

  57: {

  58:     int i;

  59:     while(scanf("%d",&n),n)

  60:     {

  61:         build(1,n,1);

  62:         for(i=1; i<=n; i++)

  63:         {

  64:             scanf("%d",a+i);

  65:             b[i]=a[i];

  66:         }

  67:         sort(a+1,a+n+1);//让数组a升序排序,那么等下b就可以通过a来求出对应的树是第几大(这算是虚拟化吧)

  68:         ll sum=0;

  69:         for(i=1; i<=n; i++)

  70:         {

  71:             int t=find(b[i]);

  72:             sum+=(ll)query(t,1,n,1);

  73:             update(t,1,n,1);

  74:         }

  75:         printf("%lld\n",sum);

  76:     }

  77:     return 0;

  78: }