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 ...
随机推荐
- resharper10 注册方法
注册工具:http://pan.baidu.com/s/1bnFjGfX 注册方法: 1 编辑Products.json文件,留下自己要注册的产品路径即可. 2 运行patch.exe 3 使用Ser ...
- smile domain name www.bn-nd.com for sell. Please contact boyanzheng at foxmail.com 微笑的域名。请联系邮箱。
- Window.Event.KeyCode的含义
Window.Event.KeyCode=13的含义(转载) 2011-04-16 09:41:18| 分类: html | 标签:keycode event realkey var do ...
- sqlite3:深入理解sqlite3_stmt 机制
我们在使用sqlite3的过程中,涉及到批量操作时(批量插入.批量读...),总会遇到 sqlite3_stmt这个数据类型,按照官方解释说法是这样的:sqlite3_stmt是C接口中“准备语句对象 ...
- python实战教程之自动扫雷(自己存下来学习之用)
3.python的第三方库win32api,win32gui,win32con,Pillow,numpy,opencv可通过 pip install --upgrade SomePackage 来进行 ...
- SQL Server数据库的除法默认向下取整,要返回小数的解决方法
num1; / 1000.0 num2; * 1.0 num3; num4; 结果:
- QT_7_资源文件_对话框_QMessageBox_界面布局_常用控件
资源文件 1.1. 将资源导入到项目下 1.2. 添加文件—>Qt -->Qt Resource File 1.3. 起名称 res ,生成res.qrc文件 1.4. 右键 open i ...
- String s = “1a2a3a4a” 解码为 “1234”
将字符串 String s = “1a2a3a4a” 解码为 “1234” public class Program2 { public static void main(String[] args ...
- MySQL-06 数据备份和恢复
学习目标 数据备份 数据恢复 数据库迁移 导入和导出 数据备份 系统意外崩溃或者服务器硬件损坏都有可能导致数据库丢失,因此生产环境中数据备份非常重要. MySQLdump命令备份 该命令可以将数据库备 ...
- HTML中pre标签的用法
我们经常会在要保持文本格式的时候使用pre标签,比如当我们要展示源代码的时候,只要放一个pre标签,然后把源代码直接复制,粘贴,然后在页面上就可以保持好格式.不会像放在其它标签里那样,把换行和空格都自 ...