poj 2299 Ultra-QuickSort

题目连接

http://poj.org/problem?id=2299 

Ultra-QuickSort

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.

SampleInput

5
9
1
0
5
4
3
1
2
3
0

SampleOutput

6

0

用线段树求逆序数。。

 1 #include<algorithm>
 2 #include<iostream>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<vector>
 7 #include<map>
 8 #include<set>
 9 using std::cin;
10 using std::cout;
11 using std::endl;
12 using std::find;
13 using std::sort;
14 using std::set;
15 using std::map;
16 using std::pair;
17 using std::vector;
18 using std::multiset;
19 using std::multimap;
20 #define all(c) (c).begin(), (c).end()
21 #define iter(c) decltype((c).begin())
22 #define cls(arr,val) memset(arr,val,sizeof(arr))
23 #define cpresent(c, e) (find(all(c), (e)) != (c).end())
24 #define rep(i, n) for (int i = 0; i < (int)(n); i++)
25 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
26 #define pb(e) push_back(e)
27 #define mp(a, b) make_pair(a, b)
28 #define lc (root<<1)
29 #define rc (root<<1|1)
30 #define mid ((l+r)>>1)
31 const int Max_N = 500100;
32 typedef unsigned long long ull;
33 struct Node { int val; };
34 struct P {
35     int v, id;
36     friend bool operator<(const P &a, const P &b) {
37         return a.v == b.v ? a.id < b.id : a.v < b.v;
38     }
39 }rec[Max_N];
40 struct SegTree {
41     Node seg[Max_N << 2];
42     inline void init() {
43         cls(seg, 0);
44     }
45     inline void insert(int root, int l, int r, int p) {
46         if (p > r || p < l) return;
47         if (p <= l && p >= r) { seg[root].val++; return; }
48         insert(lc, l, mid, p);
49         insert(rc, mid + 1, r, p);
50         seg[root].val = seg[lc].val + seg[rc].val;
51     }
52     inline ull query(int root, int l, int r, int x, int y) {
53         if (x > r || y < l) return 0;
54         if (x <= l && y >= r) return seg[root].val;
55         ull ret = 0;
56         ret += query(lc, l, mid, x, y);
57         ret += query(rc, mid + 1, r, x, y);
58         return ret;
59     }
60 }seg;
61 int main() {
62 #ifdef LOCAL
63     freopen("in.txt", "r", stdin);
64     freopen("out.txt", "w+", stdout);
65 #endif
66     int n;
67     while (~scanf("%d", &n) && n) {
68         seg.init();
69         ull res = 0;
70         for (int i = 1; i <= n; i++) scanf("%d", &rec[i].v), rec[i].id = i;
71         sort(rec + 1, rec + n + 1);
72         for (int i = 1; i <= n; i++) {
73             res += seg.query(1, 1, n, rec[i].id, n);
74             seg.insert(1, 1, n, rec[i].id);
75         }
76         printf("%lld
", res);
77     }
78     return 0;
79 }
View Code

By: GadyPu 博客地址:http://www.cnblogs.com/GadyPu/ 转载请说明
原文地址:https://www.cnblogs.com/GadyPu/p/4572450.html