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
2
4 2
1 4
output
4
input
3
1 6
3 4
2 5
output
15

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

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

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

 #include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const double eps = 1e-;
const int MAXN = 4e5+;
int a[MAXN], tot, n;
int A[MAXN], B[MAXN];
int lowbit (int x)
{
return x & -x;
}
long long arr[MAXN], M;
void modify (int x, int d)
{
while (x < M)
{
arr[x] += d;
x += lowbit (x);
}
}
int sum(int x)
{
int ans = ;
while (x)
{
ans += arr[x];
x -= lowbit (x);
}
return ans;
}
int p[MAXN],kk[MAXN];
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
while (cin >> n)
{
int x, y;
tot = ;
memset (arr, , sizeof (arr));
memset(kk, , sizeof (kk));
long long minv = inf;
long long maxv = ;
map<int, int>pp;
for (int i = ; i < n; i++)
{
scanf ("%d%d", &x, &y);
minv = min(minv, (long long)min(x, y));
maxv = max(maxv, (long long)max(x, y));
a[tot++] = x;
a[tot++] = y;
A[i] = x;
B[i] = y;
}
sort (a, a+tot); tot = unique(a, a+tot) - a;
int ok = ;
int tmp = tot;
long long j = minv;
int tt;
vector<int>vec;
for (int i = ; i < tot; )
{
if (a[i] == j)
{
i++;
j++;
ok = ;
}
else
{
if (ok == ) // 缩点
{
ok = j;
a[tmp++] = j;
tt = j;
vec.push_back(tt);
}
pp[ok] += a[i]-j;
j = a[i];
}
}
tot = tmp;
sort (a, a+tot);
for (int i = ; i < vec.size(); i++)
{
int qq = vec[i];
if (pp.count(qq) >= )
{
int ix = lower_bound(a, a+tot, qq)-a+;
kk[ix] = pp[qq];
}
}
for (int i = ; i < n; i++)
{
A[i] = lower_bound(a,a+tot, A[i]) - a + ; // 离散化
B[i] = lower_bound(a,a+tot, B[i]) - a + ;
}
maxv = lower_bound(a, a+tot, maxv) - a + ;
M = maxv+;
for (int i = ; i <= maxv; i++)
{
p[i] = i;
}
for (int i = ; i < n; i++)
{
swap(p[A[i]], p[B[i]]);
}
long long ans = ;
long long cnt = ;
for (int i = ; i <= maxv; i++)
{
ans += (long long)(i-+cnt - sum(p[i]))*max(,kk[i]);
modify(p[i], max(,kk[i]));
cnt += max(,kk[i]) - ;
}
printf("%I64d\n", ans);
}
return ;
}

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

  1. Codeforces Round #381 (Div. 2) D dfs序+树状数组

    D. Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  2. HDU 6318 - Swaps and Inversions - [离散化+树状数组求逆序数][杭电2018多校赛2]

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=6318 Problem Description Long long ago, there was an ...

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

    Dynamic Inversions II Time Limit: 6000/3000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Other ...

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

    题目链接 题意:给出数组A,定义f(l,r,x)为A[]的下标l到r之间,等于x的元素数.i和j符合f(1,i,a[i])>f(j,n,a[j]),求i和j的种类数. 我们可以用map预处理出  ...

  5. 【Codeforces Round #433 (Div. 1) C】Boredom(树状数组)

    [链接]h在这里写链接 [题意] 给你一个n*n的矩阵. 其中每一列都有一个点. 任意两个点构成了矩形的两个对角点 ->即任意两个点确定了一个矩形. ->总共能确定n*(n-1)/2个矩形 ...

  6. SGU180 Inversions(树状数组求逆序数)

    题目: 思路:先离散化数据然后树状数组搞一下求逆序数. 离散化的方法:https://blog.csdn.net/gokou_ruri/article/details/7723378 自己对用树状数组 ...

  7. 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 ...

  8. codeforces 540E 离散化技巧+线段树/树状数组求逆序对

    传送门:https://codeforces.com/contest/540/problem/E 题意: 有一段无限长的序列,有n次交换,每次将u位置的元素和v位置的元素交换,问n次交换后这个序列的逆 ...

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

    http://codeforces.com/problemset/problem/12/D 这里的BIT查询,指的是查询[1, R]或者[R, maxn]之间的最值,这样就够用了. 设三个权值分别是b ...

随机推荐

  1. python批量改动指定文件夹文件名称

    这小样例仅仅要是说明用python怎么批量改动指定文件夹的文件名称: 记得要把脚本跟改动的文件放在同一个文件夹下 #encoding:utf-8 import os import sys files ...

  2. Python标准库:内置函数bytearray([source[, encoding[, errors]]])

    返回一个新字节数组.这个数组里的元素是可变的.而且每一个元素的值范围: 0 <= x < 256.能够通过"字节与字节数组操作"章节来查看相关字节数组的内容.以下说明一 ...

  3. [Javascript] Intro to Recursion

    Recursion is a technique well suited to certain types of tasks. In this first lesson we’ll look at s ...

  4. Android UI WebView的使用:

    Android UI WebView的使用: /** * @author smiling * @date 2016/10 */ 布局: <?xml version="1.0" ...

  5. select 响应时间 js

    HTML form select表单标签案例代码如下: 跳转菜单的需要放在head头部标签内的JS脚本代码:<script type="text/javascript"> ...

  6. html图像入门

    在HTML中,图像由<img>标签定义. <img>是空标签,意思是说,它只包含属性,并且没有闭合标签. 要在页面上显示图像,需要使用源属性src, src指的是"s ...

  7. nginx添加缓存

    nginx的具体逻辑是什么样的? 分布式session spring session redis过滤器 有4种方案: 一直访问一台 //如果这台机器垮掉了,怎么办? session同步 序列化传输 / ...

  8. bash:ifconfig command not found for contos7.0

    CentOS7刚发布,我忍不住把DELL T410从CentOS6升级到CentOS7.好不容易等安装结束后,立即配置网络,然后在yum源上安装环境.可是执行ifconfig的时候系统提示让我傻了眼: ...

  9. CodeSMART for VS.NET插件工具

    今天无聊,想起以前看过的微软的Visual Studio的插件,所以就找了找. 微软的Visual Studio本身就非常强大了,但是仍然有不足的地方,比如下面要介绍的我喜欢的代码格式化功能的这个插件 ...

  10. Oracle 错误码

    Oracle作为一款比较优秀同时也比较难以掌握的大型数据库,在我们学习使用的过程中,不可避免的会遇到一些错误,为此 Oracle 给出了一套完备的错误消息提示机制 我们可以根据Oracle给出的消息提 ...