[国家集训队]排队 [cdq分治]
题面
和动态逆序对那道题没有什么区别
把一个交换换成两个删除和两个插入
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;
const int N = 1e5 + 5;
const double eps = 1e-9;
const int inf = 0x3f3f3f3f;
typedef pair<int, int> PII;
typedef pair<double, int> PDI;
struct Node{
int x, y, z, d, cnt;
}node[N];
bool rule_yzx(Node x, Node y){
if(x.y != y.y) return x.y < y.y;
if(x.z != y.z) return x.z < y.z;
return x.x < y.x;
}
int n, m, nsize, a[N], b[N];
int tim, dfn[N];
long long ans[N];
struct BIT{
int w[N];
void ins(int x, int d){while(x<=n){w[x] += d; x += x & -x;}}
int qry(int x){int res = 0; while(x){res += w[x]; x -= x & -x;}return res;}
void print(){for(int i = 1; i <= n; ++i) printf("%d", w[i]); printf("\n");}
}bit;
inline void add(int x1, int x2, int x3, int x4){
++nsize;
node[nsize].x = x1, node[nsize].y = x2, node[nsize].z = x3, node[nsize].d = x4;
}
void cdq(int L, int R){
if(L == R) return ;
int mid = L + ((R - L) >> 1);
cdq(L, mid); cdq(mid + 1, R);
sort(node + L, node + mid + 1, rule_yzx);
sort(node + mid + 1, node + R + 1, rule_yzx);
int j = L;
for(int i = mid + 1; i <= R; ++i){
while(j <= mid && node[j].y <= node[i].y){
bit.ins(node[j].z, node[j].d); ++j;
}
node[i].cnt += node[i].d * (bit.qry(n) - bit.qry(node[i].z));
}
//printf("L %d R %d\n", L, R);
//bit.print();
while(j > L){--j; bit.ins(node[j].z, -node[j].d);}
j = mid;
for(int i = R; i >= mid + 1; --i){
while(j >= L && node[j].y >= node[i].y){
bit.ins(node[j].z, node[j].d); --j;
}
node[i].cnt += node[i].d * bit.qry(node[i].z - 1);
}
while(j < mid){++j; bit.ins(node[j].z, -node[j].d);}
}
int main() {
scanf("%d", &n);
for(int i = 1; i <= n; ++i){
scanf("%d", &a[i]); b[i] = a[i];
}
sort(b + 1, b + n + 1);
for(int i = 1; i <= n; ++i){
a[i] = (lower_bound(b + 1, b + n + 1, a[i]) - b);
add(0, i, a[i], 1);
//printf("%d %d %d\n", node[i].x, node[i].y, node[i].z);
}
scanf("%d", &m);
for(int i = 1, x, y; i <= m; ++i){
scanf("%d%d", &x, &y);
add(++tim, x, a[x], -1);
add(++tim, y, a[y], -1);
swap(a[x], a[y]);
add(++tim, x, a[x], 1);
add(++tim, y, a[y], 1);
dfn[i] = tim;
}
//for(int i = 1; i <= nsize; ++i) printf("%d %d %d\n", node[i].x, node[i].y, node[i].z);
//已经是升序了
cdq(1, nsize);
for(int i = 1; i <= nsize; ++i){
//printf("%d %d %d %d\n", node[i].x, node[i].y, node[i].z, node[i].cnt);
ans[node[i].x] += node[i].cnt;
}
for(int i = 1; i <= tim; ++i) ans[i] += ans[i - 1];
for(int i = 0; i <= m; ++i) printf("%d\n", ans[dfn[i]]);
system("PAUSE");
return 0;
}
[国家集训队]排队 [cdq分治]的更多相关文章
- 【LG1975】[国家集训队]排队
[LG1975][国家集训队]排队 题面 洛谷 题解 又是一个偏序问题 显然\(CDQ\) 交换操作不好弄怎么办? 可以看成两次删除两次插入 排序问题要注意一下 代码 #include <ios ...
- Luogu-1975 [国家集训队]排队
Luogu-1975 [国家集训队]排队 题面 Luogu-1975 题解 题意:给出一个长度为n的数列以及m个交换两个数的操作,问每次操作后逆序对数量 时间,下标和数的大小三维偏序,,,把交换操作看 ...
- BZOJ 2141: 排队 [CDQ分治]
题意: 交换序列中两个元素,求逆序对 做分块做到这道题...一看不是三维偏序嘛.... 作为不会树套树的蒟蒻就写CDQ分治吧.... 对时间分治...x排序...y树状数组... 交换拆成两个插入两个 ...
- bzoj 2141 : 排队 (cdq分治+bit)
链接: https://www.lydsy.com/JudgeOnline/problem.php?id=2141 思路: 其实就是求动态逆序对...cdq降维,用树状数组前后求两遍逆序对就好了 切水 ...
- luogu1975 [国家集训队]排队
思路 序列中 |i | 1| 2| 3| 4| 5| 6| 7| 8| 9| 10| |----|--|--|--|--|--|--|--|--|--|--| |a[i]| a| b| c| L| d ...
- P1975 [国家集训队]排队
题目链接 题意分析 我们考虑 交换两个数\([le,ri]\)的贡献 减少的逆序对数\([le,ri]\)中小于\(num[le]\)以及大于\(num[ri]\)的数 增加的\([le,ri]\)中 ...
- P1975 [国家集训队]排队 线段树套平衡树维护动态逆序对查询
$ \color{#0066ff}{ 题目描述 }$ 排排坐,吃果果,生果甜嗦嗦,大家笑呵呵.你一个,我一个,大的分给你,小的留给我,吃完果果唱支歌,大家乐和和. 红星幼儿园的小朋友们排起了长长地队伍 ...
- 洛谷 P1975 [国家集训队]排队 Lebal:块内排序+树状数组
题目描述 排排坐,吃果果,生果甜嗦嗦,大家笑呵呵.你一个,我一个,大的分给你,小的留给我,吃完果果唱支歌,大家乐和和. 红星幼儿园的小朋友们排起了长长地队伍,准备吃果果.不过因为小朋友们的身高有所区别 ...
- [luoguP1975] [国家集训队]排队(分块)
传送门 直接暴力分块,然后在每一个块内排序. 查询时可以在每一个块内二分. #include <cmath> #include <cstdio> #include <io ...
随机推荐
- 机器学习第一篇——最近邻kNN
机器学习监督学习中,根据解决问题的连续性和离散型,分为分类问题和回归问题.最邻近算法kNN是一种最为直接和简便的分类方法. kNN本质上,是计算目标到模型的欧式距离,从而判定目标所属的类别. 首先,在 ...
- Hadoop生态的配置
网盘下载地址 链接: https://pan.baidu.com/s/19qWnP6LQ-cHVrvT0o1jTMg 密码: 44hs Hadoop伪分布式配置 Hadoop 可以在单节点上以伪分布 ...
- hadoop:如何运行自带wordcount
1.在linux系统创建文件 vi aa.txt --------i 进行编辑 输入 内容(多个单词例如:aa bb cc aa) 2.在HDFS上面创建文件夹 hdfs dfs -mkdir ...
- JavaScript中防止重复提交
有这么一种情况: 页面有一个按钮,点击之后会触发Ajax请求,但是用户在点击之后,不知道是否点成功了,于是又点了一下,如果不加处理的话,就会进行两次Ajax请求,并且请求的数据都是一样的,对后端的程序 ...
- Es6数值拓展
Es6数值拓展 一,Number扩展 1,ES6 提供了二进制和八进制数值的新的写法,分别用前缀0b(或0B)和0o(或0O)表示. 将0b和0o前缀的字符串数值转为十进制,要使用Number方法 N ...
- laravel服务容器
laravel框架底层解析 本文参考陈昊<Laravel框架关键技术解析>,搭建一个属于自己的简化版服务容器.其中涉及到反射.自动加载,还是需要去了解一下. laravel服务容器 建立项 ...
- 理解根目录,classpath, getClass().getResourceAsStream和getClass().getClassLoader().getResourceAsStream的区别
一: 理解根目录 <value>classpath*:/application.properties</value> <value>classpath:/appli ...
- 《Effective C++》设计与声明:条款18-条款25
条款18:让接口容易被正确使用,不容易被误用 注意使用const,explicit,shared_ptr等来限制接口. 必要时可以创建一些新的类型,限制类型操作,束缚对象等. 注意保持接口的一致性,且 ...
- CLOUD SQL跟踪
CLOUD会自动在后台执行一些sql语句,所以追踪起来比较麻烦,需要加入一些过滤条件. 比如关键的CLIENTPROCESSID,加入后 ,就能过滤是哪个客户度执行的数据. 过滤数据.
- 进阶开发——文档,缓存,ip限速
一.文档自动化管理 1.django rest framework提供了一个接口: 可以将代码中注释转换为文档中内容(list,create等),以及help_text等等,且会生成JavaScrip ...