【链接】 我是链接,点我呀:)

【题意】

给你一个无限长的序列1,2,3,4...
然后给你n个操作.
每个操作ai,bi;
表示调换位置为ai和位置为bi的数的位置。
(ai,bi

【题解】

肯定和数据结构相关的。
那么大,首先离散化一下。
然后用离散化后的数字来模拟这个swap的过程。
这样,就能够获取出有操作过的数之间的大小关系了。
然后用树状数组来处理这些"有操作过的数字"的逆序对。
但是还不够。
因为可能还有一些数字虽然没有经过处理。但仍然对逆序对有贡献。
也即位置为i的被操作过的数字,它原来的位置pos.
那么这里对逆序对有贡献的数字,即i..pos这一段中没有操作过的数字。(这里假设ipos也成立。
所以再处理出两个处理过的数字之间没有处理过的数字的前缀和就好了。
这样的话就能够快速的获取任意两个位置之间没有操作过的数字的和了。

写题的时候,一直在想着要怎么在线处理。

但实际上这一题并不需要在线处理,只要操作之后知道答案就可以了。

不用什么非常高级的数据结构。

【代码】

#include <bits/stdc++.h>
using namespace std;
#define ll long long const int N = 1e5; int n,num;
pair <int,int> a[N+10];
int X[N*2+10],val[N*2+10],pre[N*2+10]; struct BI {
int a[2*N + 10]; int lowbit(int x) {
return x&(-x);
} void add(int x,int y) {
while (x <= 2*N) {
a[x] += y;
x += lowbit(x);
}
} int sum(int x) {
int now = 0;
while (x > 0) {
now += a[x];
x -= lowbit(x);
}
return now;
} int get_sum(int l, int r) {
return sum(r) - sum(l - 1);
} }b; int main(){
#ifdef LOCAL_DEFINE
freopen("rush_in.txt", "rt", stdin);
#endif scanf("%d",&n);
for (int i = 1;i <= n;i++){
scanf("%d%d",&a[i].first,&a[i].second);
X[++num] = a[i].first;
X[++num] = a[i].second;
} sort(X+1,X+1+num);
num = unique(X+1,X+1+num) - X - 1;
// printf("%d\n",num); for (int i = 1;i <= num;i++) val[i] = X[i];
for (int i = 1;i <= num;i++) pre[i] = pre[i-1] + X[i] - X[i-1] - 1; for (int i = 1;i <= n;i++){
int x = lower_bound(X+1,X+1+num,a[i].first) - X;
int y = lower_bound(X+1,X+1+num,a[i].second) - X;
swap(val[x],val[y]);
} ll ans = 0;
for (int i = 1;i <= num;i++){
int pos = lower_bound(X+1,X+1+num,val[i])-X;
/*
printf("now=%d pos=%d temp2 = %d\n",i,pos,abs(pre[pos]-pre[i]));
puts("");
*/ ans += b.get_sum(pos,num);
ans += abs(pre[pos]-pre[i]);
b.add(pos,1);
} printf("%lld\n",ans);
return 0;
}

【Codeforces Round #301 (Div. 2) E】Infinite Inversions的更多相关文章

  1. 【Codeforces Round #301 (Div. 2) D】 Bad Luck Island

    [链接] 我是链接,点我呀:) [题意] 剪刀.石头.布各有r,s,p个生活在同一个村子里. 它们两两之间相遇的几率都相同(相遇后就会按照划拳的规则判断输赢,输的人就死掉了). 问你最后只剩下剪刀,只 ...

  2. 【Codeforces Round #301 (Div. 2) C】 Ice Cave

    [链接] 我是链接,点我呀:) [题意] 给你一个n*m的地图. 每个地图为0的时候可以安全走过,且走过后变成1. (一定要离开之后才会变成1) 而为1的则走过之后会掉入下一层. 你一开始在初始位置( ...

  3. 【Codeforces Round #301 (Div. 2) B】 School Marks

    [链接] 我是链接,点我呀:) [题意] 已知k门成绩. 总共有n门成绩. 让你构造剩下的n-k门成绩,使得这n门成绩的中位数>=y,并且这n门成绩的和要小于等于x. n为奇数 [题解] 首先判 ...

  4. 【Codeforces Round #301 (Div. 2) A】 Combination Lock

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 模拟水题 [代码] #include <bits/stdc++.h> using namespace std; cons ...

  5. 【Codeforces Round #432 (Div. 1) B】Arpa and a list of numbers

    [链接]h在这里写链接 [题意] 定义bad list是一个非空的.最大公约数为1的序列.给定一个序列,有两种操作:花费x将一个元素删除.花费y将一个元素加1,问你将这个序列变为good list所需 ...

  6. 【Codeforces Round #420 (Div. 2) C】Okabe and Boxes

    [题目链接]:http://codeforces.com/contest/821/problem/C [题意] 给你2*n个操作; 包括把1..n中的某一个数压入栈顶,以及把栈顶元素弹出; 保证压入和 ...

  7. 【Codeforces Round #420 (Div. 2) B】Okabe and Banana Trees

    [题目链接]:http://codeforces.com/contest/821/problem/B [题意] 当(x,y)这个坐标中,x和y都为整数的时候; 这个坐标上会有x+y根香蕉; 然后给你一 ...

  8. 【Codeforces Round #420 (Div. 2) A】Okabe and Future Gadget Laboratory

    [题目链接]:http://codeforces.com/contest/821/problem/A [题意] 给你一个n*n的数组; 然后问你,是不是每个位置(x,y); 都能找到一个同一行的元素q ...

  9. 【Codeforces Round #423 (Div. 2) C】String Reconstruction

    [Link]:http://codeforces.com/contest/828/problem/C [Description] 让你猜一个字符串原来是什么; 你知道这个字符串的n个子串; 且知道第i ...

随机推荐

  1. 错误:created a ThreadLocal with key of type ……but failed to remove it when the web application was stopped. This is very likely to create a memory leak.

    tomcat reload显示错误:SEVERE: The web application [/Interceptor] created a ThreadLocal with key of type ...

  2. UITableViewCell的prepareForReuse方法

    cell被重用怎样提前知道? 重写cell的prepareForReuse官方头文件里有说明.当前已经被分配的cell假设被重用了(一般是滚动出屏幕外了),会调用cell的prepareForReus ...

  3. SSM(spring,springMVC,Mybatis)框架的整合

    这几天想做一个小项目,所以搭建了一个SSM框架. 1.基本概念 1.1.Spring   Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Joh ...

  4. 小白算法之路-非确定性多项式(non-deterministic polynomial,缩写NP)

    前端小白的算法之路   时隔多日终于解决了埋在心头的一道难题,霎时云开雾散,今天把一路而来碰到的疑惑和心得都记录下来,也算是开启了自己探索算法的大门. 问题背景 曾经有一个年少轻狂的职场小白,在前端圈 ...

  5. js call 和 apply

    前言 call 和 apply 都是为了改变某个函数运行时的 context 即上下文而存在的,换句话说,就是为了改变函数体内部 this 的指向. call 和 apply二者的作用完全一样,只是接 ...

  6. pipPython运维日记

    一 Python 工作环境管理 1.1 使用 pyenv 管理不同的Python 版本 克隆项目安装 git clone https://github.com/yyuu/pyenv.git ~/.py ...

  7. Android Gson解析json工具类封装

    package com.springSecurity.gson; import java.util.ArrayList; import java.util.List; import java.util ...

  8. array01.js

    //1.获取指定范围内的随机数 function getRadomNum(min,max){ return Math.floor(Math.random() * (max - min + 1)) + ...

  9. Vue给元素添加样式

    Vue中使用样式 绑定css 数组 <style> .red{ color:red } .thin{ font-size:18px } </style> <h1 :cla ...

  10. C#引用c++DLL结构体数组注意事项(数据发送与接收时)

    本文转载自:http://blog.csdn.net/lhs198541/article/details/7593045 最近做的项目,需要在C# 中调用C++ 写的DLL,因为C# 默认的编码方式是 ...