[国家集训队]排队 [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 ...
随机推荐
- 牛客练习赛B题 筱玛的排列(找递推规律)
链接:https://ac.nowcoder.com/acm/contest/342/B来源:牛客网 筱玛的排列 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K,其他语 ...
- PAT L3-020 至多删三个字符
https://pintia.cn/problem-sets/994805046380707840/problems/994805046946938880 给定一个全部由小写英文字母组成的字符串,允许 ...
- Failure to transfer org.apache.maven:maven-archiver:pom:2.5 from https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval o
pom.xml报错: Failure to transfer org.apache.maven:maven-archiver:pom:2.5 from https://repo.maven.apach ...
- JMeter学习non-gui模式运行
-h, --help print usage information and exit #打印帮助信息 -v, --version print the version information and ...
- react render
实际上react render方法返回一个虚拟dom 并没有去执行渲染dom 渲染的过程是交给react 去完成的 这就说明了为什么要在所有数据请求完成后才去实现render 这样做也提高了性能.只调 ...
- react中如何使用动画效果
在react中想要加入动画效果 需要引入 import {CSSTransitionGroup} from 'react-transition-group' //加入react 动画包 import ...
- C调用C++, C++调用C方法
1. C 调用 C++封装好后的函数: -> 在C++中有一个函数 int main_cpp(): -> 首先构建头文件, #ifndef CPP_FILE_H #define CPP ...
- laravel belongsTo使用
前提:订单表(order)和用户表(user) 表结构: order CREATE TABLE `order` ( `id` char(16) COLLATE utf8mb4_unicode_ci N ...
- 12 Connections
1 and 出现在两个及以上的词,词组,或者句子之间,将它们连接起来.在正式的书面英语中,and不能出现在句首.在非正式的英语中可以. We should expand our product lin ...
- 创建虚拟目录失败,必须为服务器名称指定“localhost”
关于微信开发过程,远程调试后,再次打开vs出现项目加载失败的解决办法! 第一步: 第二步:打开编辑的页面,把下图这部分直接注释掉 ok了,再加载一次,就好了!