51Nod 1019 逆序数(线段树)

题目链接:逆序数

模板题。

 1 #include <bits/stdc++.h>
 2
 3 using namespace std;
 4
 5 #define rep(i, a, b) for (int i(a); i <= (b); ++i)
 6 #define lson i << 1, L, mid
 7 #define rson i << 1 | 1, mid + 1, R
 8
 9 const int N = 100010;
10
11 long long ans = 0;
12
13 struct node{
14     int x, y;
15     friend bool operator < (const node &a, const node &b){
16         return a.x < b.x;
17     }
18 } a[N];
19
20 int tree[N << 2];
21 int c[N];
22 int n;
23
24 inline void pushup(int i){
25     tree[i] = tree[i << 1] + tree[i << 1 | 1];
26 }
27
28 void build(int i, int L, int R){
29     tree[i] = 0;
30     if (L == R) return ;
31     int mid = (L + R) >> 1;
32     build(lson);
33     build(rson);
34 }
35
36 void update(int i, int L, int R, int pos, int val){
37     if (L == R && L == pos){
38         tree[i] += val;
39         return;
40     }
41
42     int mid = (L + R) >> 1;
43     if (pos <= mid) update(lson, pos, val);
44     else update(rson, pos, val);
45
46     pushup(i);
47 }
48
49 int query(int i, int L, int R, int l, int r){
50     if (L == l && R == r) return tree[i];
51     int mid = (L + R) >> 1;
52     if (r <= mid) return query(lson, l, r);
53     else if (l > mid) return query(rson, l, r);
54     else return query(lson, l, mid) + query(rson, mid + 1, r);
55 }
56
57 int main(){
58
59     scanf("%d", &n);
60
61     rep(i, 1, n){
62         scanf("%d", &a[i].x);
63         a[i].y = i;
64     }
65
66     sort(a + 1, a + n + 1);
67     c[a[1].y] = 1; rep(i, 2, n) c[a[i].y] = a[i].x == a[i - 1].x ? c[a[i - 1].y] : c[a[i - 1].y] + 1;
68
69     build(1, 1, n);  ans = 0;
70
71     rep(i, 1, n){
72         ans += (long long)query(1, 1, n, min(c[i] + 1, n), n);
73         update(1, 1, n, c[i], 1);
74     }
75
76     printf("%lld\n", ans);
77
78     return 0;
79 }
时间: 2024-08-03 07:09:23

51Nod 1019 逆序数(线段树)的相关文章

poj 2274 The Race(逆序数+线段树)

The Race Time Limit: 15000MS   Memory Limit: 65536K Total Submissions: 3237   Accepted: 664 Case Time Limit: 3000MS Description During the Annual Interstellar Competition for Tuned Spaceships, N spaceships will be competing. Each spaceship i is tuned

HDU 1394 &lt;Minimum Inversion Number&gt; &lt;逆序数&gt;&lt;线段树&gt;

Description The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that satisfy i < j and ai > aj. For a given sequence of numbers a1, a2, ..., an, if we move the first m >= 0 numbers to the end of the seq

51nod 1019 逆序数

在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序.一个排列中逆序的总数就称为这个排列的逆序数. 如2 4 3 1中,2 1,4 3,4 1,3 1是逆序,逆序数是4.给出一个整数序列,求该序列的逆序数. Input第1行:N,N为序列的长度(n <= 50000) 第2 - N + 1行:序列中的元素(0 <= Aii <= 10^9)Output输出逆序数Sample Input 4 2 4 3 1 Sample Output 4 逆序数

归并求逆序数 &amp;&amp; 线段树求逆序数

Brainman Time Limit: 1000 MS Memory Limit: 30000 KB 64-bit integer IO format: %I64d , %I64u   Java class name: Main [Submit] [Status] [Discuss] Description Background Raymond Babbitt drives his brother Charlie mad. Recently Raymond counted 246 toothp

51Nod 1019 逆序数 (归并排序)

1 #include <iostream> 2 #include <cstring> 3 4 using namespace std; 5 const int maxn = 50005; 6 int a[maxn]; 7 int res[maxn]; 8 int ans; 9 10 //归并排序 11 void merge(int l, int r){ 12 //cout<<l<<" "<<r<<endl; 13

1019 逆序数

1019 逆序数 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序.一个排列中逆序的总数就称为这个排列的逆序数. 如2 4 3 1中,2 1,4 3,4 1,3 1是逆序,逆序数是4.给出一个整数序列,求该序列的逆序数. Input 第1行:N,N为序列的长度(n <= 50000) 第2 - N + 1行:序列中的元素(0 <= A[i] <= 10^9) Ou

HDU 4417 类似求逆序数的树状数组

Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2250    Accepted Submission(s): 1092 Problem Description Mario is world-famous plumber. His “burly” figure and amazing jumping ability

逆序数和线段树的关系

考虑一下逆序数的定义:::::::::::::::::::::::::::::::::::::::: { 2, 4, 3, 1 } 分别是(2,1), (4,3), (4,1), (3,1) 也就是说针对一个数.判断在这个序列中这个数位置后面的数有多少个比它小. 也就是说针对一个数.判断在这个序列中这个数位置之前有多少个数比它大. 那就是询问当前数~n 区间上的出现了多少个数. (由于我们询问顺序是从前到后(左到右).所以在当前数前面且比它大的都更新过了.) 注意这里把极端列为n. 如果不是n就

逆序对 线段树&amp;树状数组

17年的时候在HDU新生赛的时候遇到这样一道题目, 当时对于这种题目, 只会n^2去数左边比他大的个数 再相加一下 就是答案了. 无奈n是1e5 毫无疑问的T了. 后来学长说这个不就是归并排序吗, 你去学一下归并就可以做了, 然后我去学了归并, 又交了一发, 结果竟然还是T(这Y的不是耍我玩吗). 然后从另一位学长哪里听说了用线段树去求逆序对, 把n^2变成nlogn就不会T了,最后, 我又学了线段树,终于这回AC了.写这个帖子的时候,我顺便去HDU找了找这道题目,结果找不到这道题目,竟然没挂出