codeforces 301 E. Infinite Inversions
题目:
2 seconds
256 megabytes
standard input
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.
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.
Print a single integer — the number of inversions in the resulting sequence.
终于脑补出来了。。,总是看不懂E文题解
由于a[i],b[i]很大,但是我们可以分为两部分计算
part1:首先计算只交换的逆序数,这个可以使离散+BIT(树状数组)
part2:未出现的数怎么计算,其实为出现的也只跟 出现的数有关,因为未出现的数的相对顺序不变。
举个例子:交换后:1 9 3 6 7 2 4 8 5
这里只有 1 3 8 没有交换,且出现
所以先是算9不满足,然后 6 也没有 7也没有(这里指计算为出现与出现部分
然后2:总共有 9 3 6 7四个 未出现的有3,那么怎么计算?
2在的位置是6那么前面有6-2个大于他的数,减去之前出现的(交换出现 只与2相关)就是答案
可以发现 5 也是如此推算:9 3 6 7 8 --》9-5=4;4-3=1;
如此云云
codeforces 301 E. Infinite Inversions的更多相关文章
- CF #301 E:Infinite Inversions(逆序数,树状数组)
A-Combination Lock B-School Marks C-Ice Cave D-Bad Luck Island E-Infinite Inversions E:Infini ...
- Codeforces Round #301 (Div. 2) E . Infinite Inversions 树状数组求逆序数
E. Infinite Inversions ...
- 线段树 离散化 E. Infinite Inversions E. Physical Education Lessons
题目一:E. Infinite Inversions 这个题目没什么思维量,还比较简单,就是离散化要加上每一个值的后面一个值,然后每一个值放进去的不是1 ,而是这个值与下一个点的差值. 因为这个数代表 ...
- 【Codeforces Round #301 (Div. 2) E】Infinite Inversions
[链接] 我是链接,点我呀:) [题意] 给你一个无限长的序列1,2,3,4... 然后给你n个操作. 每个操作ai,bi; 表示调换位置为ai和位置为bi的数的位置. (ai,bi<=10^9 ...
- CodeForces 540E - Infinite Inversions(离散化+树状数组)
花了近5个小时,改的乱七八糟,终于A了. 一个无限数列,1,2,3,4,...,n....,给n个数对<i,j>把数列的i,j两个元素做交换.求交换后数列的逆序对数. 很容易想到离散化+树 ...
- codeforces 540E"Infinite Inversions"
传送门 题意: 给你一个无限大的整数序列 p = {1, 2, 3, ...}: 有 n 次操作,每次操作交换第 ai 个数和第 aj 个数: 求序列中逆序对的个数: 题解: 考虑交换完后的序列,存 ...
- Infinite Inversions(树状数组+离散化)
思路及代码参考:https://blog.csdn.net/u014800748/article/details/45420085 There is an infinite sequence cons ...
- CodeForces 622 A.Infinite Sequence
A.Infinite Sequence time limit per test 1 second memory limit per test 256 megabytes input standard ...
- codeforces 675A A. Infinite Sequence(水题)
题目链接: A. Infinite Sequence time limit per test 1 second memory limit per test 256 megabytes input st ...
随机推荐
- 读取Chrome书签文件
使用C#读取Chrome浏览器的本地书签文件,当前文件在C盘下用户文件夹\AppData\Local\Google\Chrome\User Data\Default\下的Bookmarks 打开这个文 ...
- Java之流水号生成器实现
参考:https://www.jianshu.com/p/331b872e9c8f 1.建立一张存放的表 CREATE TABLE `sys_serial_number` ( `id` bigint( ...
- java 面试题整理
java面试题 1.接口和抽象类的区别 抽象类 接口 抽象类中可以有默认方法 在java8之前,不能有默认方法 extends implements 抽象类中可以有构造器 接口中不能有构造器 抽象类中 ...
- Apache安装和文件配置
Apache和Tomcat的区别是:静态文件和动态页面,C++和Java的区别. 对比.
- 计算机网络之传输层 下(TCP)
1. TCP的特点 特点:它是一个点到点的通信机制,只能有一个发送方和一个接收方:它提供是一个可靠的,按序的字节流机制:使用流水线机制,通过拥塞控制和流量控制的机制设置窗口尺寸:发送方和接收方都有缓存 ...
- (15) openssl签署和自签署证书的多种实现方式
1.采用自定义配置文件的实现方法 1.1 自建CA 自建CA的机制:1.生成私钥:2.创建证书请求:3.使用私钥对证书请求签名. 由于测试环境,所以自建的CA只能是根CA. 所使用的配置文件如下: [ ...
- CentOS 7的docker安装初始化
1: 安装必要的一些系统工具 sudo yum install -y yum-utils device-mapper-persistent-data lvm2 2: 添加软件源信息 添加阿里源这样下载 ...
- blocking and nonblocking assign
key word: 仿真建模 clock采样block/nonblock blocking时,有时候clk会sample edge后的data: nobocking时,clk sample 以前的d ...
- 「问题思考」python的递归中return返回none
代码: #求最大公约数 def gcd(x,y): if x < y: swap = x x = y y = swap if x%y == 0: return y else: gcd(y,x%y ...
- 03 数据解析-Xpath
Xpath解析 XPath在Python的爬虫学习中,起着举足轻重的地位,对比正则表达式 re两者可以完成同样的工作,实现的功能也差不多,但XPath明显比re具有优势,在网页分析上使re退居二线. ...