【Codeforces Round #301 (Div. 2) E】Infinite Inversions
【链接】 我是链接,点我呀:)
【题意】
给你一个无限长的序列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的更多相关文章
- 【Codeforces Round #301 (Div. 2) D】 Bad Luck Island
[链接] 我是链接,点我呀:) [题意] 剪刀.石头.布各有r,s,p个生活在同一个村子里. 它们两两之间相遇的几率都相同(相遇后就会按照划拳的规则判断输赢,输的人就死掉了). 问你最后只剩下剪刀,只 ...
- 【Codeforces Round #301 (Div. 2) C】 Ice Cave
[链接] 我是链接,点我呀:) [题意] 给你一个n*m的地图. 每个地图为0的时候可以安全走过,且走过后变成1. (一定要离开之后才会变成1) 而为1的则走过之后会掉入下一层. 你一开始在初始位置( ...
- 【Codeforces Round #301 (Div. 2) B】 School Marks
[链接] 我是链接,点我呀:) [题意] 已知k门成绩. 总共有n门成绩. 让你构造剩下的n-k门成绩,使得这n门成绩的中位数>=y,并且这n门成绩的和要小于等于x. n为奇数 [题解] 首先判 ...
- 【Codeforces Round #301 (Div. 2) A】 Combination Lock
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 模拟水题 [代码] #include <bits/stdc++.h> using namespace std; cons ...
- 【Codeforces Round #432 (Div. 1) B】Arpa and a list of numbers
[链接]h在这里写链接 [题意] 定义bad list是一个非空的.最大公约数为1的序列.给定一个序列,有两种操作:花费x将一个元素删除.花费y将一个元素加1,问你将这个序列变为good list所需 ...
- 【Codeforces Round #420 (Div. 2) C】Okabe and Boxes
[题目链接]:http://codeforces.com/contest/821/problem/C [题意] 给你2*n个操作; 包括把1..n中的某一个数压入栈顶,以及把栈顶元素弹出; 保证压入和 ...
- 【Codeforces Round #420 (Div. 2) B】Okabe and Banana Trees
[题目链接]:http://codeforces.com/contest/821/problem/B [题意] 当(x,y)这个坐标中,x和y都为整数的时候; 这个坐标上会有x+y根香蕉; 然后给你一 ...
- 【Codeforces Round #420 (Div. 2) A】Okabe and Future Gadget Laboratory
[题目链接]:http://codeforces.com/contest/821/problem/A [题意] 给你一个n*n的数组; 然后问你,是不是每个位置(x,y); 都能找到一个同一行的元素q ...
- 【Codeforces Round #423 (Div. 2) C】String Reconstruction
[Link]:http://codeforces.com/contest/828/problem/C [Description] 让你猜一个字符串原来是什么; 你知道这个字符串的n个子串; 且知道第i ...
随机推荐
- 实战c++中的string系列--string的替换、查找(一些与路径相关的操作)
今天继续写一些string操作. string给我们提供了非常多的方法,可是每在使用的时候,就要费些周折. 场景1: 得到一个std::string full_path = "D:\prog ...
- [Android]新建项目继承Activity不继承ActionBarActivity
在SDK更新后,在eclipse新建Android项目时.我们常常会碰到这样一种事情:新建的MainActivity不再继承Activity而是继承ActionBarActivity,因为一些人的开发 ...
- 新辰:十种外链终极方法 让SEOer外链之路不再孤独!
大家都知道,外链就是指从别的站点导入到自己站点的链接.导入链接对于新辰站点优化来说是很重要的一个过程.因此,新辰觉得.对于中小型站点来说.外链但是优化的重中之重! 由于也有了"外链专员&qu ...
- jquery05 继承
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- C#变量的作用域
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- [论文笔记] CUDA Cuts: Fast Graph Cuts on the GPU
Paper:V. Vineet, P. J. Narayanan. CUDA cuts: Fast graph cuts on the GPU. In Proc. CVPR Workshop, 200 ...
- C# 爬虫总结
static void Main(string[] args) { //WebRequest request = WebRequest.Create("http://www.cnblogs. ...
- cat---查看文件内容
- 【Hello 2018 C】Party Lemonade
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 处理出凑够2^j最少需要花费多少钱. 即试着把第i种物品买2^(j-i)个,看看会不会更便宜 记录在huafei[0..31]中 然 ...
- [Python's] Python's list comprehensions a
# Python's list comprehensions are awesome. vals = [expression for value in collection if condition] ...