CF #301 E:Infinite Inversions(逆序数,树状数组)
A-Combination Lock B-School Marks C-Ice Cave D-Bad Luck Island E-Infinite Inversions
题意就是有一个序列1,2,3,4。。。。。
现在有n次交换,每次都把ab交换求最终形成的序列的逆序数;
逆序数分为两部分。一部分是交换过位置的,另一部分是没有交换过的。
离散化后,利用树状数组求出交换过的位置的逆序数的个数。
第二部分:
看一个样例:
2
1 6
9 5
得到的序列为6 2 3 4 9 1 7 8 5
首先对于数值6,其下标为1。在区间[1, 6]中,共有6个数。减去该区间中有3个是交换过位置的,则6-3 = 3是数值6对于当前序列所构成的逆序数个数。
对于数值9,下标为5,则在区间[5,9],共有5个数。减去该区间有3个是换过位置的,则逆序数为2。
对于数值1,其下标为6,在区间[1,6]中,~~~~~也有3个逆序数。
……
问题变为求一个区间的中有几个是交换过位置的,其实只要知道其下标的排名。例如下标1的排名为1,下标5排名2,下标6排名3,下标9排名4。
区间[1,6],则是3-1+1 = 3;
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<map>
#include<cmath>
#include<queue>
using namespace std;
#define N 2010005
#define INF 0x3f3f3f3f struct node
{
int x, y;
}b[N];
map<int, int>M;
int a[N], cnt, Tree[N], V[N]; int lowbit(int x)
{
return x&(-x);
} void Update(int pos, int num)
{
while(pos<=cnt)
{
Tree[pos]+=num;
pos+=lowbit(pos);
}
} int GetSum(int pos)
{
int s=;
while(pos)
{
s+=Tree[pos];
pos-=lowbit(pos);
}
return s;
} int main()
{
int n;
while(scanf("%d", &n)!=EOF)
{
memset(a, , sizeof(a));
memset(b, , sizeof(b));
memset(V, , sizeof(V));
cnt=;
for(int i=; i<n; i++)
{
scanf("%d %d", &b[i].x, &b[i].y);
a[cnt++]=b[i].x, a[cnt++]=b[i].y;
}
sort(a+, a+cnt);
cnt = unique(a, a+cnt)-a-;
for(int i=; i<=cnt; i++)
M[a[i]]=i, V[i]=i;
for(int i=; i<n; i++)
{
int p=M[b[i].x];
int q=M[b[i].y];
swap(V[p], V[q]);
}
long long ans=;
memset(Tree, , sizeof(Tree));
for(int i=; i<=cnt; i++)
{
int x=V[i];
ans+=(i--GetSum(x));
Update(x, );
}
for(int i=; i<=cnt; i++)
ans+=abs(a[V[i]]-a[i]-V[i]+i);
printf("%lld\n", ans);
}
return ;
}
CF #301 E:Infinite Inversions(逆序数,树状数组)的更多相关文章
- POJ 2299 Ultra-QuickSort 逆序数 树状数组 归并排序 线段树
题目链接:http://poj.org/problem?id=2299 求逆序数的经典题,求逆序数可用树状数组,归并排序,线段树求解,本文给出树状数组,归并排序,线段树的解法. 归并排序: #incl ...
- Ultra-QuickSort---poj2299 (归并排序.逆序数.树状数组.离散化)
题目链接:http://poj.org/problem?id=2299 题意就是求把数组按从小到大的顺序排列,每次只能交换相邻的两个数, 求至少交换了几次 就是求逆序数 #include<std ...
- ACM学习历程—HDU5592 ZYB's Premutation(逆序数 && 树状数组 && 二分)(BestCoder Round #65 1003)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5592 题目大意就是给了每个[1, i]区间逆序对的个数,要求复原原序列. 比赛的时候2B了一发. 首先 ...
- cdoj 841 休生伤杜景死惊开 逆序数/树状数组
休生伤杜景死惊开 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) 陆伯言军陷八卦 ...
- hdu 1394 Minimum Inversion Number 逆序数/树状数组
Minimum Inversion Number Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showprob ...
- zoj 3157 Weapon 逆序数/树状数组
B - Weapon Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Submit Sta ...
- [BZOJ 3295] [luogu 3157] [CQOI2011]动态逆序对(树状数组套权值线段树)
[BZOJ 3295] [luogu 3157] [CQOI2011] 动态逆序对 (树状数组套权值线段树) 题面 给出一个长度为n的排列,每次操作删除一个数,求每次操作前排列逆序对的个数 分析 每次 ...
- Dynamic Inversions 50个树状数组
Dynamic Inversions Time Limit: 30000/15000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others ...
- Bzoj 2141: 排队 分块,逆序对,树状数组
2141: 排队 Time Limit: 4 Sec Memory Limit: 259 MBSubmit: 1310 Solved: 517[Submit][Status][Discuss] D ...
随机推荐
- Java 之泛型通配符 ? extends T 与 ? super T 解惑
简述 大家在平时的工作学习中, 肯定会见过不少如下的语句: List<? super T> List<? extends T> 我们都知道, 上面的代码时关于 Java 泛型的 ...
- 【sql绕过】Bypass waf notepad of def
文章是通过阅读<[独家连载]我的WafBypass之道 (SQL注入篇)>写的阅读笔记. Waf的类型 1.云waf云waf通常是CDN包含的waf,DNS在解析的时候要解析到cdn上面制 ...
- 三篇很好的讲解keppalived的博客
VRRP协议介绍 参考资料: RFC 3768 1. 前言 VRRP(Virtual Router Redundancy Protocol)协议是用于实现路由器冗余的协议,最新协议在RFC3768中定 ...
- [device]/proc/devices and /dev/
1. /proc/devices和/dev cat /proc/devices 列出在当前运行的内核中已经注册的设备名称以及设备的Major主设备号.其中的设备信息是驱动程序在加载时生成的,也可以说是 ...
- st16c554
/* * st16c554.c * * TWO ST16C554 driver for AMCC PPC405EP * * Author: Li Zhi You/Zhu jiang <godis ...
- getopt--parse command line options
getopt解析命令行选项 getopt, getopt_long, getopt_long_only, optarg, optind, opterr, optopt - Parse command- ...
- DRBD安装配置、工作原理及故障恢复
一.DRBD简介 DRBD的全称为:Distributed ReplicatedBlock Device(DRBD)分布式块设备复制,DRBD是由内核模块和相关脚本而构成,用以构建高可用性的集群.其实 ...
- crontab中运行python程序出错,提示ImportError: No module named解决全过程
将一个python脚本放入crontab执行时,提示如下错:ImportError: No module named hashlib但是在shell中直接执行时没有任何问题,google之后,得到线索 ...
- 得到application
ServletContext application = request.getSession().getServletContext(); String basePath = (Str ...
- 解决阿里云部署 office web apps ApplicationFailedException 报错问题
查找这个问题,确实花费了很长时间,所以具体解析一下问题原因吧. 报错如下: 问题详情链接 New-OfficeWebAppsFarm:Office Online服务无法启动.有关详细信息,请参阅Win ...