Codeforces Round #301 (Div. 2) E . Infinite Inversions 树状数组求逆序数
2 seconds
256 megabytes
output standard output
There is an infinite sequence consisting of all positive integers in the increasing order: p = {1, 2, 3, ...}. We performed n swapoperations with this sequence. A swap(a, b) is an operation of swapping the elements of the sequence on positions a and b. Your task is to find the number of inversions in the resulting sequence, i.e. the number of such index pairs (i, j), that i < j and pi > pj.
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of swap operations applied to the sequence.
Each of the next n lines contains two integers ai and bi (1 ≤ ai, bi ≤ 109, ai ≠ bi) — the arguments of the swap operation.
Print a single integer — the number of inversions in the resulting sequence.
2
4 2
1 4
4
3
1 6
3 4
2 5
15
题意:一个无限 长的数组(1, 2, 3, 4, .....), n次操作, 每次交换两个位置上的值.
输出最终 有多少逆序对数。
由于这题是 数字可能会很多,,我们只能 离散化之后来求 逆序数了,, 先把所有操作读进来,,离散化 被操作数。 而那些被操作数之间的数字可以缩点来处理(就是把所有的数字看成一个数字来处理)。然后就可以求出结果了。。
#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const double eps = 1e-;
const int MAXN = 4e5+;
int a[MAXN], tot, n;
int A[MAXN], B[MAXN];
int lowbit (int x)
{
return x & -x;
}
long long arr[MAXN], M;
void modify (int x, int d)
{
while (x < M)
{
arr[x] += d;
x += lowbit (x);
}
}
int sum(int x)
{
int ans = ;
while (x)
{
ans += arr[x];
x -= lowbit (x);
}
return ans;
}
int p[MAXN],kk[MAXN];
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
while (cin >> n)
{
int x, y;
tot = ;
memset (arr, , sizeof (arr));
memset(kk, , sizeof (kk));
long long minv = inf;
long long maxv = ;
map<int, int>pp;
for (int i = ; i < n; i++)
{
scanf ("%d%d", &x, &y);
minv = min(minv, (long long)min(x, y));
maxv = max(maxv, (long long)max(x, y));
a[tot++] = x;
a[tot++] = y;
A[i] = x;
B[i] = y;
}
sort (a, a+tot); tot = unique(a, a+tot) - a;
int ok = ;
int tmp = tot;
long long j = minv;
int tt;
vector<int>vec;
for (int i = ; i < tot; )
{
if (a[i] == j)
{
i++;
j++;
ok = ;
}
else
{
if (ok == ) // 缩点
{
ok = j;
a[tmp++] = j;
tt = j;
vec.push_back(tt);
}
pp[ok] += a[i]-j;
j = a[i];
}
}
tot = tmp;
sort (a, a+tot);
for (int i = ; i < vec.size(); i++)
{
int qq = vec[i];
if (pp.count(qq) >= )
{
int ix = lower_bound(a, a+tot, qq)-a+;
kk[ix] = pp[qq];
}
}
for (int i = ; i < n; i++)
{
A[i] = lower_bound(a,a+tot, A[i]) - a + ; // 离散化
B[i] = lower_bound(a,a+tot, B[i]) - a + ;
}
maxv = lower_bound(a, a+tot, maxv) - a + ;
M = maxv+;
for (int i = ; i <= maxv; i++)
{
p[i] = i;
}
for (int i = ; i < n; i++)
{
swap(p[A[i]], p[B[i]]);
}
long long ans = ;
long long cnt = ;
for (int i = ; i <= maxv; i++)
{
ans += (long long)(i-+cnt - sum(p[i]))*max(,kk[i]);
modify(p[i], max(,kk[i]));
cnt += max(,kk[i]) - ;
}
printf("%I64d\n", ans);
}
return ;
}
Codeforces Round #301 (Div. 2) E . Infinite Inversions 树状数组求逆序数的更多相关文章
- Codeforces Round #381 (Div. 2) D dfs序+树状数组
D. Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- HDU 6318 - Swaps and Inversions - [离散化+树状数组求逆序数][杭电2018多校赛2]
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=6318 Problem Description Long long ago, there was an ...
- Dynamic Inversions II 逆序数的性质 树状数组求逆序数
Dynamic Inversions II Time Limit: 6000/3000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Other ...
- Codeforces Round #261 (Div. 2) D. Pashmak and Parmida's problem (树状数组求逆序数 变形)
题目链接 题意:给出数组A,定义f(l,r,x)为A[]的下标l到r之间,等于x的元素数.i和j符合f(1,i,a[i])>f(j,n,a[j]),求i和j的种类数. 我们可以用map预处理出 ...
- 【Codeforces Round #433 (Div. 1) C】Boredom(树状数组)
[链接]h在这里写链接 [题意] 给你一个n*n的矩阵. 其中每一列都有一个点. 任意两个点构成了矩形的两个对角点 ->即任意两个点确定了一个矩形. ->总共能确定n*(n-1)/2个矩形 ...
- SGU180 Inversions(树状数组求逆序数)
题目: 思路:先离散化数据然后树状数组搞一下求逆序数. 离散化的方法:https://blog.csdn.net/gokou_ruri/article/details/7723378 自己对用树状数组 ...
- Codeforces Beta Round #79 (Div. 1 Only) B. Buses 树状数组
http://codeforces.com/contest/101/problem/B 给定一个数n,起点是0 终点是n,有m两车,每辆车是从s开去t的,我们只能从[s,s+1,s+2....t-1 ...
- codeforces 540E 离散化技巧+线段树/树状数组求逆序对
传送门:https://codeforces.com/contest/540/problem/E 题意: 有一段无限长的序列,有n次交换,每次将u位置的元素和v位置的元素交换,问n次交换后这个序列的逆 ...
- Codeforces Beta Round #12 (Div 2 Only) D. Ball 树状数组查询后缀、最值
http://codeforces.com/problemset/problem/12/D 这里的BIT查询,指的是查询[1, R]或者[R, maxn]之间的最值,这样就够用了. 设三个权值分别是b ...
随机推荐
- Dubbo[一个分布式服务框架
http://alibaba.github.io/dubbo-doc-static/User+Guide-zh.htm#UserGuide-zh-API%E9%85%8D%E7%BD%AE http: ...
- Android中如何像 360 一样优雅的杀死后台服务而不启动
Android中,虽然有很多方法(API或者shell命令)杀死后台`service`,但是仍然有很多程序几秒内再次启动,导致无法真正的杀死.这里主要着重介绍如何像 360 一样杀死Android后台 ...
- Linux GRUB 2及修改默认启动项
The GRUB 2 boot loader makes sure that you can boot Linux. GRUB 2 is installed in the boot sector of ...
- SpringMVC06以对象的方式获取前台的数据
========创建需要的两个实体类================ public class School { private String sName; private String addres ...
- el和jstl
<%@page import="cn.bdqn.bean.News"%> <%@ page language="java" import=&q ...
- svg转换工具
package com.rubekid.springmvc.utils; import java.io.ByteArrayInputStream; import java.io.ByteArrayOu ...
- Windbg简单介绍
1.1 使用帮助 Windbg中的命令分为三种:基本命令.元命令和扩展命令.基本命令和元命令都是调试器自带的,元命令以" ."开头. 扩展命令是外部加入的,以"!&quo ...
- Namespace declaration statement has to be the very first
Namespace declaration statement has to be the very first statement in the script 我新建了一个Homea模块,并把Hom ...
- C#数组的使用
//计算数组中最大值,最小值,平均值和总和 //类中main最先执行 static void Main(string[] args) { //声明一个数组,数组长度一定固定就不能更改了 , , , , ...
- JAVA-2-GetDay
import java.util.*; public class Ch0310 { public static void main(String[] args) { // TODO 自动生成的方法存根 ...