花了近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(离散化+树状数组)的更多相关文章

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

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

  2. codeforces 652D Nested Segments 离散化+树状数组

    题意:给你若干个区间,询问每个区间包含几个其它区间 分析:区间范围比较大,然后离散化,按右端点排序,每次更新树状数组中的区间左端点,查询区间和 注:(都是套路) #include<cstdio& ...

  3. HDU 6318.Swaps and Inversions-求逆序对-线段树 or 归并排序 or 离散化+树状数组 (2018 Multi-University Training Contest 2 1010)

    6318.Swaps and Inversions 这个题就是找逆序对,然后逆序对数*min(x,y)就可以了. 官方题解:注意到逆序对=交换相邻需要交换的次数,那么输出 逆序对个数 即可. 求逆序对 ...

  4. Ultra-QuickSort(归并排序+离散化树状数组)

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 50517   Accepted: 18534 ...

  5. HDU 5862 Counting Intersections(离散化+树状数组)

    HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...

  6. BZOJ_4627_[BeiJing2016]回转寿司_离散化+树状数组

    BZOJ_4627_[BeiJing2016]回转寿司_离散化+树状数组 Description 酷爱日料的小Z经常光顾学校东门外的回转寿司店.在这里,一盘盘寿司通过传送带依次呈现在小Z眼前.不同的寿 ...

  7. poj-----Ultra-QuickSort(离散化+树状数组)

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 38258   Accepted: 13784 ...

  8. Code Forces 652D Nested Segments(离散化+树状数组)

     Nested Segments time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  9. hdu 3015 Disharmony Trees (离散化+树状数组)

    Disharmony Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

随机推荐

  1. python xlrd,xlwt 读写excel文件

    python 读excel文件,需要xlrd库.下载地址:https://pypi.python.org/pypi/xlrd python 写excel文件,需要xlwt库.下载地址:https:// ...

  2. CF192div2-C - Purification

    题意: 从给定的图中找出某些点,这些点能够消除同一行和同一列的“怪物”.求使得最少的点的位置. 关键:要想消除整张的图的妖怪,必须选中n个点(对于n行n列来说)!!!!!!!!!!! 做法:对于每一行 ...

  3. php获取请求的方式(get/post)

    $request_method = $_SERVER['REQUEST_METHOD'];//用什么方式访问 get post

  4. 转发:maven打包时始终出现以下提示:-source 1.3 中不支持泛型(请使用 -source 5 或更高版本以启用泛型)

    maven打包时始终出现以下提示: 1.-source 1.3 中不支持泛型(请使用 -source 5 或更高版本以启用泛型)List<User> userList= new Array ...

  5. 如何让Activiti-Explorer使用sql server数据库

    从官网下载的Activiti-explorer的war文件内部默认是使用h2内存数据库的,如果想改用其他的数据库来做持久化,比如sql server,需要做如下配置. 1)修改db.propertie ...

  6. android 业务需求: 先干掉自己, 在重启自己

    // 重启应用 public void restartApp() { Intent intent = new Intent(); // 参数1:包名,参数2:程序入口的activity intent. ...

  7. XML和JSON 序列化以及DataTable转JSON

    using System.IO; using System.Text; using System.Xml.Serialization; using System.Xml; using System.R ...

  8. 带你走进EJB--MDB实现发送邮件(3)

    接上篇,在业务逻辑中已经发送JMS消息,而接下来的消息驱动Bean作为JMS消息监听器,主要是负责监听指定的JMS消息,此时已经接受到JMS的消息,那么MDB的onMessage()方法会被触发.调用 ...

  9. ActionBar官方教程(4)给ActionBar添加操作项及它们的事件处理

    Adding Action Items The action bar provides users access to the most important action items relating ...

  10. Form.block Trigger DML常规写法

    Block的数据源是View的,如果想操作数据,需要注意在以下几个Trigger里面写代码: 一般建议创建View的时候包含rowid字段. on-lock: select inventory_ite ...