Codeforces Round #301 (Div. 2) E . Infinite Inversions 树状数组求逆序数

E. Infinite Inversions

time limit per test

2 seconds

memory limit per test

256 megabytes

input standard input

                              output standard output

There is an infinite sequence consisting of all positive integers in the increasing order: p = {1, 2, 3, ...}. We performed n swapoperations with this sequence. A swap(a, b) is an operation of swapping the elements of the sequence on positions a and b. Your task is to find the number of inversions in the resulting sequence, i.e. the number of such index pairs (i, j), that i < j and pi > pj.

Input

The first line contains a single integer n (1 ≤ n ≤ 105) — the number of swap operations applied to the sequence.

Each of the next n lines contains two integers ai and bi (1 ≤ ai, bi ≤ 109, ai ≠ bi) — the arguments of the swap operation.

Output

Print a single integer — the number of inversions in the resulting sequence.

Sample test(s)

input

24 21 4

output

4

input

31 63 42 5

output

15

题意:一个无限 长的数组(1, 2, 3, 4, .....), n次操作, 每次交换两个位置上的值.

输出最终 有多少逆序对数。

由于这题是 数字可能会很多,,我们只能 离散化之后来求 逆序数了,, 先把所有操作读进来,,离散化 被操作数。  而那些被操作数之间的数字可以缩点来处理(就是把所有的数字看成一个数字来处理)。然后就可以求出结果了。。

  1 #include <set>
  2 #include <map>
  3 #include <cmath>
  4 #include <ctime>
  5 #include <queue>
  6 #include <stack>
  7 #include <cstdio>
  8 #include <string>
  9 #include <vector>
 10 #include <cstdlib>
 11 #include <cstring>
 12 #include <iostream>
 13 #include <algorithm>
 14 using namespace std;
 15 typedef unsigned long long ull;
 16 typedef long long ll;
 17 const int inf = 0x3f3f3f3f;
 18 const double eps = 1e-8;
 19 const int MAXN = 4e5+10;
 20 int a[MAXN], tot, n;
 21 int A[MAXN], B[MAXN];
 22 int lowbit (int x)
 23 {
 24     return x & -x;
 25 }
 26 long long arr[MAXN], M;
 27 void modify (int x, int d)
 28 {
 29     while (x < M)
 30     {
 31         arr[x] += d;
 32         x += lowbit (x);
 33     }
 34 }
 35 int sum(int x)
 36 {
 37     int ans = 0;
 38     while (x)
 39     {
 40         ans += arr[x];
 41         x -= lowbit (x);
 42     }
 43     return ans;
 44 }
 45 int p[MAXN],kk[MAXN];
 46 int main()
 47 {
 48     #ifndef ONLINE_JUDGE
 49         freopen("in.txt","r",stdin);
 50     #endif
 51     while (cin >> n)
 52     {
 53         int x, y;
 54         tot = 0;
 55         memset (arr, 0, sizeof (arr));
 56         memset(kk, 0, sizeof (kk));
 57         long long minv = inf;
 58         long long maxv = 0;
 59         map<int, int>pp;
 60         for (int i = 0; i < n; i++)
 61         {
 62             scanf ("%d%d", &x, &y);
 63             minv = min(minv, (long long)min(x, y));
 64             maxv = max(maxv, (long long)max(x, y));
 65             a[tot++] = x;
 66             a[tot++] = y;
 67             A[i] = x;
 68             B[i] = y;
 69         }
 70         sort (a, a+tot);
 71
 72         tot = unique(a, a+tot) - a;
 73         int ok = 0;
 74         int tmp = tot;
 75         long long j = minv;
 76         int tt;
 77         vector<int>vec;
 78         for (int i = 0; i < tot; )
 79         {
 80             if (a[i] == j)
 81             {
 82                 i++;
 83                 j++;
 84                 ok = 0;
 85             }
 86             else
 87             {
 88                 if (ok == 0)            // 缩点
 89                 {
 90                     ok = j;
 91                     a[tmp++] = j;
 92                     tt = j;
 93                     vec.push_back(tt);
 94                 }
 95                 pp[ok] += a[i]-j;
 96                 j = a[i];
 97             }
 98         }
 99         tot = tmp;
100         sort (a, a+tot);
101         for (int i = 0; i < vec.size(); i++)
102         {
103             int qq = vec[i];
104             if (pp.count(qq) >= 1)
105             {
106                 int ix = lower_bound(a, a+tot, qq)-a+1;
107                 kk[ix] = pp[qq];
108             }
109         }
110         for (int i = 0; i < n; i++)
111         {
112             A[i] = lower_bound(a,a+tot, A[i]) - a + 1;   // 离散化
113             B[i] = lower_bound(a,a+tot, B[i]) - a + 1;
114         }
115         maxv = lower_bound(a, a+tot, maxv) - a + 1;
116         M = maxv+10;
117         for (int i = 1; i <= maxv; i++)
118         {
119             p[i] = i;
120         }
121         for (int i = 0; i < n; i++)
122         {
123             swap(p[A[i]], p[B[i]]);
124         }
125         long long ans = 0;
126         long long cnt = 0;
127         for (int i = 1; i <= maxv; i++)
128         {
129             ans += (long long)(i-1+cnt - sum(p[i]))*max(1,kk[i]);
130             modify(p[i], max(1,kk[i]));
131             cnt += max(1,kk[i]) - 1;
132         }
133         printf("%I64d\n", ans);
134     }
135     return 0;
136 }
时间: 2024-07-28 18:00:24

Codeforces Round #301 (Div. 2) E . Infinite Inversions 树状数组求逆序数的相关文章

Codeforces Round #261 (Div. 2) D. Pashmak and Parmida&#39;s problem (树状数组求逆序数 变形)

题目链接 题意: 给出一些数a[n],求(i, j), i<j 的数量,使得:f(1, i, a[i]) > f(j, n, a[j]) . f(lhs, rhs, x) 指在 { [lhs, rhs]范围中,a[k]的值=x } 的数量. 1.  f(1, i, a[i]) 就是指a[i]前面包括a[i]的数中,有几个值=a[i]. 2.  f(j, n, a[j]) 就是指a[j]后面包括a[j]的数中有几个值=a[j]. 虽然a[x]范围不小,但是n的范围是1000,不是很大,所以我们可

Dynamic Inversions II 逆序数的性质 树状数组求逆序数

Dynamic Inversions II Time Limit: 6000/3000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitStatus Problem Description 给出N个数a[1],a[2] ... a[N],a[1]...a[N]是1-N的一个排列,即1 <= a[i] <= N且每个数都不相同.有M个操作,每个操作给出x,y两个数,你将a[x],a[y]交换,然后求交换后数组的逆序

Codeforces Round #301 (Div. 2) E. Infinite Inversions —— 逆序对 离散化 + 树状数组

题目链接:http://codeforces.com/contest/540/problem/E E. Infinite Inversions time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output There is an infinite sequence consisting of all positive integers in

Codeforces Beta Round #79 (Div. 1 Only) B. Buses 树状数组

http://codeforces.com/contest/101/problem/B 给定一个数n,起点是0  终点是n,有m两车,每辆车是从s开去t的,我们只能从[s,s+1,s+2....t-1]处上车,从t处下车., 问能否去到点n,求方案数 设L[x]表示有多少辆车能够到达x处. 只能从t处下车:说明只能单点更新,对于没辆车x,在区间[s,s+1,s+2....t-1]内上车是可以得,那么有多少辆车呢?明显就是∑区间里能到达的点.然后单点更新t即可 数据大,明显不能到达的点是没用的,离

Codeforces Round #277 E. LIS of Sequence(486E) 树状数组乱搞

http://codeforces.com/contest/486/problem/E E. LIS of Sequence time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output The next "Data Structures and Algorithms" lesson will be about Longest I

Codeforces Beta Round #12 (Div 2 Only) D. Ball 树状数组查询后缀、最值

http://codeforces.com/problemset/problem/12/D 这里的BIT查询,指的是查询[1, R]或者[R, maxn]之间的最值,这样就够用了. 设三个权值分别是b[1], b[2], b[2]; 首先,先把b[1]值离散化,离散成一个个id,那么只能是在id比较大的地方找了.然后把b[2]排序,倒序查询,也就是先查询最大的,当然它是没可能自杀的,因为它已经最大了,然后查询完后,把它压进bit后,以后在bit查询,就不需要管b[2]了,因为在bit里面的b[2

loj #535. 「LibreOJ Round #6」花火 树状数组求逆序对+主席树二维数点+整体二分

$ \color{#0066ff}{ 题目描述 }$ 「Hanabi, hanabi--」 一听说祭典上没有烟火,Karen 一脸沮丧. 「有的哦-- 虽然比不上大型烟花就是了.」 还好 Shinobu 早有准备,Alice.Ayaya.Karen.Shinobu.Yoko 五人又能继续愉快地玩耍啦! 「噢--!不是有放上天的烟花嘛!」Karen 兴奋地喊道. 「啊等等--」Yoko 惊呼.Karen 手持点燃引信的烟花,「嗯??」 Yoko 最希望见到的是排列优美的烟火,当然不会放过这个机会-

Codeforces Round #301 (Div. 2) -- (A,B,C,D)

题目传送:Codeforces Round #301 (Div. 2) A. Combination Lock 水题,求最小移动次数,简单贪心一下即可 AC代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <queue> #include <stack> #i

CodeForces Round #301 Div.2

今天唯一的成果就是把上次几个人一起开房打的那场cf补一下. A. Combination Lock 此等水题看一眼样例加上那个配图我就明白题意了,可是手抽没有注释掉freopen,WA了一发. 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 const int maxn = 1000 + 10; 5 6 char s1[maxn], s2[maxn]; 7 8 int main() 9 { 10 int n; cin >>