CodeForces 540E - Infinite Inversions(离散化+树状数组)

时间:2023-01-02 07:58:50

花了近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 - &lbrack;离散化&plus;树状数组求逆序数&rsqb;&lbrack;杭电2018多校赛2&rsqb;

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

  2. codeforces 652D Nested Segments 离散化&plus;树状数组

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

  3. HDU 6318&period;Swaps and Inversions-求逆序对-线段树 or 归并排序 or 离散化&plus;树状数组 &lpar;2018 Multi-University Training Contest 2 1010&rpar;

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

  4. Ultra-QuickSort&lpar;归并排序&plus;离散化树状数组&rpar;

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

  5. HDU 5862 Counting Intersections&lpar;离散化&plus;树状数组&rpar;

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

  6. BZOJ&lowbar;4627&lowbar;&lbrack;BeiJing2016&rsqb;回转寿司&lowbar;离散化&plus;树状数组

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

  7. poj-----Ultra-QuickSort&lpar;离散化&plus;树状数组&rpar;

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

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

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

  9. hdu 3015 Disharmony Trees &lpar;离散化&plus;树状数组&rpar;

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

随机推荐

  1. php&lowbar;cz

    post.php <?php require_once 'config.php'; $ddh=trim(htmlspecialchars($_POST['ddh'])); //支付宝交易号 $m ...

  2. IE6&sol;7常用的hack

    hack基础: IE6: _selector{property:value;} selector{property:value;property:value !important;} //IE6 不支 ...

  3. 怎么 才能显示Eclipse中Console的全部内容

    可以如下设置 preference->run/debug->console 设置limit console output 为false,方便调试时,查看全部console. 这个真是太有用 ...

  4. ArcGIS地图打印设置

    1.需求:客户自己开发的Engine程序,调用的是LayoutControl,需要连接大型绘图仪进行出图. 业务流程是先框选要打印的地图范围,该范围是自定义大小,框选完成之后进行预览,然后选择打印输出 ...

  5. Linux-IP地址后边加个&sol;8&lpar;16&comma;24&comma;32&rpar;是什么意思&quest;

    是掩码的位数        A类IP地址的默认子网掩码为255.0.0.0(由于255相当于二进制的8位1,所以也缩写成“/8”,表示网络号占了8位);    B类的为255.255.0.0(/16) ...

  6. feature&period;shape和feature&period;shapecopy的区别

    以前在写AE代码的时候也没有注意到feature.shape和feature.shapecopy的区别,觉得两者也差不多: 今天写入库程序才明白过来. 如果取feature.shape,则得到的是该要 ...

  7. padding当高度用时出现的问题

    <div class="wrap"> <div class="sudoku"> <div class="sdk-wrap ...

  8. Maximum Entropy Model&lpar;最大熵模型&rpar;初理解

    0,熵的描述 熵(entropy)指的是体系的混沌的程度(可也理解为一个随机变量的不确定性),它在控制论.概率论.数论.天体物理.生命科学等领域都有重要应用,在不同的学科中也有引申出的更为具体的定义, ...

  9. Swift构造

    构造就是将结构体.类或枚举的实例准备好以便使用的过程.这个过程包括: (1)为实例中的每个存储属性设置初始值. (2)执行必要的准备和初始化工作. 实例的构造过程是通过构造器来完成的. 可以在结构体. ...

  10. decorator 装饰页面,根据不同设备自动切换移动和pc站

    package com.thinkgem.jeesite.modules.sys.interceptor; import javax.servlet.http.HttpServletRequest; ...