CodeForces 540E - Infinite Inversions(离散化+树状数组)
花了近5个小时,改的乱七八糟,终于A了。
一个无限数列,1,2,3,4,...,n....,给n个数对<i,j>把数列的i,j两个元素做交换。求交换后数列的逆序对数。
很容易想到离散化+树状数组,但是发现那些没有交换的数也会产生逆序对数,但我没有算。
经明神提示, 把没有用到的数字段化成点。然后用树状数组算一下就好了。
然后我用一个数组记录每个点的长度。比如 <1,2><5,6>,1,2,3,4,5,6只有1,2,5,6用到了,那么离散化为1,2,3,4,5,f[1]=f[2]=f[4]=f[5]=1,f[3]=2,数对变为<1,2><4,5>
莫名有wa了第七组样例,看了数据(捂脸)后改为longlong,有莫名re一发,无脑改了数组长度,然后。。。就a了。。。
#include <iostream>
#include <algorithm>
#include <stdio.h>
using namespace std; typedef long long ll; struct node
{
int val;
int pos;
bool operator < (const node a) const
{
return val < a.val;
}
} x[500005]; int a[500005];
int b[500005];
int c[500005];
int d[500005]; int f[500005]; int lowbit(int x)
{
return x & (-x);
} int sum(int n)
{ int ans = 0;
while (n > 0)
{
ans += c[n];
n -= lowbit(n);
}
return ans;
} void update(int pos, int n, int num)
{ while(pos <= n)
{
c[pos] += num;
pos += lowbit(pos);
}
} int main()
{
int n;
scanf("%d", &n); for (int i = 0; i < 2 * n; ++i)
{
scanf("%d", &x[i].val);
x[i].pos = i;
}
// 离散化
sort(x, x + 2 * n);
int cnt = 0;
for (int i = 0; i < 2 * n; ++i)
{
if (i == 0 || x[i].val > x[i - 1].val)
{
++cnt;
if (x[i].val - x[i - 1].val > 1)
{
f[cnt++] = x[i].val - x[i - 1].val - 1;
}
f[cnt] = 1;
}
if (x[i].pos & 1)
b[x[i].pos / 2] = cnt;
else
a[x[i].pos / 2] = cnt;
}
for (int i = 1; i <= cnt; ++i) d[i] = i; //交换
for (int i = 0; i < n; ++i)
{
swap(d[a[i]], d[b[i]]);
} ll ans = 0;
ll tot = 0;
for (int i = 1; i <= cnt; ++i)
{
ll temp = sum(d[i]);
ans += (tot - temp) * f[i];
update(d[i], cnt, f[i]);
tot += f[i];
} printf("%lld", ans); return 0;
} /**
Input:
5
2 5
6 3
4 6
5 4
2 5 2
1 2
5 6 output:
5
2
*/
CodeForces 540E - Infinite Inversions(离散化+树状数组)的更多相关文章
- HDU 6318 - Swaps and Inversions - [离散化+树状数组求逆序数][杭电2018多校赛2]
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=6318 Problem Description Long long ago, there was an ...
- codeforces 652D Nested Segments 离散化+树状数组
题意:给你若干个区间,询问每个区间包含几个其它区间 分析:区间范围比较大,然后离散化,按右端点排序,每次更新树状数组中的区间左端点,查询区间和 注:(都是套路) #include<cstdio& ...
- HDU 6318.Swaps and Inversions-求逆序对-线段树 or 归并排序 or 离散化+树状数组 (2018 Multi-University Training Contest 2 1010)
6318.Swaps and Inversions 这个题就是找逆序对,然后逆序对数*min(x,y)就可以了. 官方题解:注意到逆序对=交换相邻需要交换的次数,那么输出 逆序对个数 即可. 求逆序对 ...
- Ultra-QuickSort(归并排序+离散化树状数组)
Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 50517 Accepted: 18534 ...
- HDU 5862 Counting Intersections(离散化+树状数组)
HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...
- BZOJ_4627_[BeiJing2016]回转寿司_离散化+树状数组
BZOJ_4627_[BeiJing2016]回转寿司_离散化+树状数组 Description 酷爱日料的小Z经常光顾学校东门外的回转寿司店.在这里,一盘盘寿司通过传送带依次呈现在小Z眼前.不同的寿 ...
- poj-----Ultra-QuickSort(离散化+树状数组)
Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 38258 Accepted: 13784 ...
- Code Forces 652D Nested Segments(离散化+树状数组)
Nested Segments time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- hdu 3015 Disharmony Trees (离散化+树状数组)
Disharmony Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
随机推荐
- sharepoint online
http://office.microsoft.com/en-001/sharepoint/sharepoint-online-online-collaboration-software-FX1037 ...
- appcache checking update
<!DOCTYPE html> <html manifest="a.appcache"> <head> <title></ti ...
- IIS MIME类型问题(html5 video 本地打开可以,IIS打开不了)
问题: mediaelement js(html 5 视频插件)网页用ie9本地打开可以,iis,vs2010在ie9上运行不了,chrome可以 (在博客园里有个人跟我遇到相同的问题:http:// ...
- 安装ubuntu12.04LTS卡住以及花屏问题
昨天在XP下用grub4dos安装了ubuntu12.04LTS,总体上还算比较顺利,中途有碰到两个异常问题,解决了记录一下. 问题一:安装过程中读取ISO镜像文件时,卡在"checking ...
- [BZOJ 4103] [Thu Summer Camp 2015] 异或运算 【可持久化Trie】
题目链接:BZOJ - 4103 题目分析 THUSC滚粗之后一直没有写这道题,从来没写过可持久化Trie,发现其实和可持久化线段树都是一样的.嗯,有些东西就是明白得太晚. 首先Orz ZYF-ZYF ...
- Spring+MyBatis实践—工程配置
初次实践:Spring+MyBatis技术搭建框架,采用Bootstrap前端开源框架. 简介: MyBatis是支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis 消除 了几乎所 ...
- [jobdu]数组中出现次数超过一半的数字
找到以后要再扫一遍确认. http://zhedahht.blog.163.com/blog/static/25411174201085114733349/ #include <iostream ...
- 一个简单的有向图Java实现
最近看了点有向图的内容,参考开源项目做了一个简单版本,直接贴代码. /** * 有向图接口,定义需要实现的各个方法,可以选择使用邻接矩阵或者邻接链表来实现 * @param <V> V代表 ...
- Android开发UI之动画侦听
动画侦听使用了AnimationListener接口,需要实现三个方法onAnimationStart().onAnimationRepeat().onAnimationEnd() 代码: 实现But ...
- bzoj1324
经典例题 在<最小割模型在信息学竞赛中的应用>有详细的解答就不赘述了 主要想说,其实这题的几个结论其实是很好猜出来的: 当摸不清题目本质的时候,不妨多找几种情况,猜测一下 顺便推广一下几个 ...