E. Infinite Inversions
                                                                                         time limit per test

2 seconds

                                                                                      memory limit per test

256 megabytes

                                                                                     input standard input

                              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.

Input

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.

Output

Print a single integer — the number of inversions in the resulting sequence.

Sample test(s)
input
2
4 2
1 4
output
4
input
3
1 6
3 4
2 5
output
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 树状数组求逆序数的更多相关文章

  1. 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 ...

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

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

  3. Dynamic Inversions II 逆序数的性质 树状数组求逆序数

    Dynamic Inversions II Time Limit: 6000/3000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Other ...

  4. 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预处理出  ...

  5. 【Codeforces Round #433 (Div. 1) C】Boredom(树状数组)

    [链接]h在这里写链接 [题意] 给你一个n*n的矩阵. 其中每一列都有一个点. 任意两个点构成了矩形的两个对角点 ->即任意两个点确定了一个矩形. ->总共能确定n*(n-1)/2个矩形 ...

  6. SGU180 Inversions(树状数组求逆序数)

    题目: 思路:先离散化数据然后树状数组搞一下求逆序数. 离散化的方法:https://blog.csdn.net/gokou_ruri/article/details/7723378 自己对用树状数组 ...

  7. 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 ...

  8. codeforces 540E 离散化技巧+线段树/树状数组求逆序对

    传送门:https://codeforces.com/contest/540/problem/E 题意: 有一段无限长的序列,有n次交换,每次将u位置的元素和v位置的元素交换,问n次交换后这个序列的逆 ...

  9. Codeforces Beta Round #12 (Div 2 Only) D. Ball 树状数组查询后缀、最值

    http://codeforces.com/problemset/problem/12/D 这里的BIT查询,指的是查询[1, R]或者[R, maxn]之间的最值,这样就够用了. 设三个权值分别是b ...

随机推荐

  1. 零基础学习IOS开发(二)- 使用cocos2d-x3.0 执行Hello world

    关于开发框架,依据网上检索来的信息,感觉cocos2d-x的ios游戏开发框架非常不错,并且有非常强的可移植性,因此打算尝试一下. 截止写下此文章,最新的cocos2d-x的版本号为v3.0稳定版(几 ...

  2. Android 单指触控拖拽,两指触控缩放

    import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view. ...

  3. cocos2d 高仿doodle jump 无源代码

    1. 游戏视频 主角眼熟吗?没错,上次跑酷游戏中的"30"来Jump了,有三种道具.主角光环,竹蜻蜓.翅膀: 有两种怪物,螃蟹和鸟: 有5种板子.点击屏幕,30会把它的嘴巴3给发射 ...

  4. Difference between Tomcat's extraResourcePaths and aliases to access an external directory--转

    Question: Simple question: In Tomcat7, what's the difference between using extraResourcePaths and al ...

  5. JVM内存回收对象及引用分析

    自动垃圾回收是Java相较于C++的一个重要的特点,想了解JVM的垃圾回收机制,首先我们要知道垃圾回收是回收什么地方的垃圾,我在我的上一篇博客<JVM内存区域划分>里面有写到JVM里面的内 ...

  6. 人工智能2:智能Agent

    一.Agent基本定义 基于理性行为的Agent是本书人工智能方法的核心.Agent由传感器.执行器两个重要元件组成,具有与环境交互的能力,其能力是通过分析感知序列,经过Agent函数映射到相应的行动 ...

  7. css02基本选择器

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  8. 查询数据库返回List<Entity>问题

    如果判断所返回的List<Entity>是否为空不能用 list!=null,因为如果查询数据为空则会返回[],当与null判断的时候会判断为有数据,此时判断条件应该写成list.size ...

  9. oracle sql语句中使用if逻辑

    l在 SQL 语句中使用IF-THEN-ELSE 逻辑 l l使用两种方法: •CASE 表达式:SQL99的语法,类似Basic,比较繁琐 •DECODE 函数:Oracle自己的语法,类似Java ...

  10. 武汉科技大学ACM :1006: A+B for Input-Output Practice (VI)

    Problem Description Your task is to calculate the sum of some integers. Input Input contains multipl ...