花了近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. MFC应用程序向导生成的文件

    比方说我们用Visio Studio创建了一个MFC应用程序,名称为Mysdi.在创建这个项目的时候,默认的会生成许多类和文件,就关于这些文件的内容和要点展开以下论述. 框架窗口类头文件 向导为项目M ...

  2. MVC5 学习笔记2

    去除VS Browser Link废代码 在webconfig中添加 <configuration> <appSettings> <add key="vs:En ...

  3. MVC-登录并设置角色

    1.新建一个类,设置角色: using System; using System.Collections.Generic; using System.Linq; using System.Text; ...

  4. linux下i2c驱动笔记 转

    1. 几个基本概念 1.1. 设备模型 由 总线(bus_type) + 设备(device) + 驱动(device_driver) 组成,在该模型下,所有的设备通过总线连接起来,即使有些设备没有连 ...

  5. 成为Java GC专家(5)—Java性能调优原则

    并不是每个程序都需要调优.如果一个程序性能表现和预期一样,你不必付出额外的精力去提高它的性能.然而,在程序调试完成之后,很难马上就满足它的性能需求,于是就有了调优这项工作.无论哪种编程语言,对应用程序 ...

  6. sencha architect/sencha touch , to prevent breakpoint lost when you debug

    add this to your loader config: Ext.Loader.setConfig({ disableCaching: false }); or : click 'applica ...

  7. XSS高级实战教程

    1.[yueyan科普系列]XSS跨站脚本攻击--yueyan 2.存储型XSS的成因及挖掘方法--pkav 3.跨站脚本攻击实例解析--泉哥 4.XSS高级实战教程--心伤的瘦子 5.XSS利用与挖 ...

  8. Error -27791: Server xx has shut down the connection prematurely

    最近在进行一次性能测试中遇到一个问题,并发较大的时候会出现LR出现Error -27791: Server xx has shut down the connection prematurely的ER ...

  9. cat主要有三大功能

    cat主要有三大功能:1.一次显示整个文件.$ cat filename2.从键盘创建一个文件.$ cat > filename     只能创建新文件,不能编辑已有文件.3.将几个文件合并为一 ...

  10. HTML页面的导出,包括Excel和Word导出

    //导出到Excel --- 全部导出,可以设置一些隐藏进行导出 protected void btnExport_Click(object sender, EventArgs e)    {     ...