动态逆序对[CDQ分治]
题面
luogu
cdq分治入门
注意删的是值不是位置!
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;
const int N = 1e5 + 5;
const int inf = 0x3f3f3f3f;
struct Node{
int x, y, z;
long long cnt;
}node[N];
inline bool rule_xyz(Node x, Node y){
if(x.x != y.x) return x.x < y.x;
if(x.y != y.y) return x.y < y.y;
return x.z < y.z;
}
inline 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, a[N], b[N];
inline void debug(int L, int R){
for(int i = L; i <= R; ++i){
if(node[i].x == 1){
printf("debug %lld\n", node[i].cnt);
break;
}
}
}
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;
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 = mid + 1;
for(int i = L; i <= mid; ++i){
while(j <= R && node[j].y < node[i].y){
bit.ins(node[j].z, 1); ++j;
}
node[i].cnt += bit.qry(n) - bit.qry(node[i].z);
}
while(--j >= mid + 1){bit.ins(node[j].z, -1);}
j = R;
for(int i = mid; i >= L; --i){
while(j >= mid + 1 && node[j].y > node[i].y){
bit.ins(node[j].z, 1); --j;
}
node[i].cnt += bit.qry(node[i].z - 1);
}
while(++j <= R) {bit.ins(node[j].z, -1);}
//printf("%d %d\n", L, R);
//bit.print();
//if(L <= 1) debug(L, R);
}
int main(){
//freopen();
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; ++i){
scanf("%d", &a[i]);
b[a[i]] = i;
}
for(int i = 1; i <= n; ++i)
node[i] = (Node){inf, i, a[i], 0};
for(int i = 1, x; i <= m; ++i){
scanf("%d", &x);
node[b[x]].x = i;//注意审题哦 这里是删掉为x的值
}
sort(node + 1, node + n + 1, rule_xyz);
cdq(1, n);
sort(node + 1, node + n + 1, rule_xyz);
long long ans = 0;
for(int i = n; i >= 1; --i){
ans += bit.qry(a[i]);
bit.ins(a[i], 1);
}
for(int i = 1; i <= m; ++i){
printf("%lld\n", ans);
ans -= node[i].cnt;
}
return 0;
}
动态逆序对[CDQ分治]的更多相关文章
- P3157 动态逆序对 CDQ分治
动态逆序对 CDQ分治 传送门:https://www.luogu.org/problemnew/show/P3157 题意: 对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对 ...
- [BZOJ3295][Cqoi2011]动态逆序对 CDQ分治&树套树
3295: [Cqoi2011]动态逆序对 Time Limit: 10 Sec Memory Limit: 128 MB Description 对于序列A,它的逆序对数定义为满足i<j,且 ...
- BZOJ 3295 动态逆序对 | CDQ分治
BZOJ 3295 动态逆序对 这道题和三维偏序很类似.某个元素加入后产生的贡献 = time更小.pos更小.val更大的元素个数 + time更小.pos更大.val更小的元素个数. 分别用类似C ...
- 【BZOJ3295】[Cqoi2011]动态逆序对 cdq分治
[BZOJ3295][Cqoi2011]动态逆序对 Description 对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对(i,j)的个数.给1到n的一个排列,按照某种顺序依 ...
- bzoj3295: [Cqoi2011]动态逆序对(cdq分治+树状数组)
3295: [Cqoi2011]动态逆序对 题目:传送门 题解: 刚学完cdq分治,想起来之前有一道是树套树的题目可以用cdq分治来做...尝试一波 还是太弱了...想到了要做两次cdq...然后伏地 ...
- BZOJ3295 [Cqoi2011]动态逆序对 —— CDQ分治
题目链接:https://vjudge.net/problem/HYSBZ-3295 3295: [Cqoi2011]动态逆序对 Time Limit: 10 Sec Memory Limit: 1 ...
- [CQOI2011]动态逆序对 CDQ分治
洛谷上有2道相同的题目(基本是完全相同的,输入输出格式略有不同) ---题面--- ---题面--- CDQ分治 首先由于删除是很不好处理的,所以我们把删除改为插入,然后输出的时候倒着输出即可 首先这 ...
- 洛谷 P3157 [CQOI2011]动态逆序对 | CDQ分治
题目:https://www.luogu.org/problemnew/show/3157 题解: 1.对于静态的逆序对可以用树状数组做 2.我们为了方便可以把删除当成增加,可以化动为静 3.找到三维 ...
- BZOJ 3295: [Cqoi2011]动态逆序对 [CDQ分治]
RT 传送门 首先可以看成倒着插入,求逆序对数 每个数分配时间(注意每个数都要一个时间)$t$,$x$位置,$y$数值 $CDQ(l,r)$时归并排序$x$ 然后用$[l,mid]$的加入更新$[mi ...
- BZOJ3295:[CQOI2011]动态逆序对(CDQ分治)
Description 对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对(i,j)的个数.给1到n的一个排列,按照某种顺序依次删除m个元素,你的任务是在每次删除一个元素之前统计 ...
随机推荐
- 这款APP太像微信 腾讯起诉索赔1000万
去年8月,“币应”(inChat)APP上线,号称是一款原创的区块链加密通讯工具,而界面与微信极为相似,图标是白配绿色调,内部界面几乎一模一样,通讯录.朋友圈的界面完全相同.里面的小游戏,也从微信拿来 ...
- vue echarts 动态数据
安装echarts依赖 npm install echarts -S 或者使用国内的淘宝镜像: 安装 npm install -g cnpm --registry=https://registry.n ...
- JS典记
var href = ""; //遍历a标签 $ ( "a"). each (function () { href = ...
- 【转】RCP中org.eclipse.core.runtime.CoreException
org.eclipse.core.runtime.CoreException: Plug-in TRAIN was unable to load class train.Application. 利用 ...
- Python_数据类型的补充、集合set、深浅copy
1.数据类型的补充 1.1 元组 当元组里面只有一个元素且没有逗号时,则该数据的数据类型与括号里面的元素相同. tu1 = ('laonanhai') tu2 = ('laonanhai') prin ...
- AJAX返回值问题
ajax同步方式获取返回值,必须以同步请求的的方式获取. //主函数部分 function confirm(id,...)//省略部分参数 { //...省略部分代码 //任务涉及专业 var Maj ...
- java.lang包【Object类】
基本描述: (1)Object类位于java.lang包中,java.lang包包含着Java最基础和核心的类,在编译时会自动导入: (2)Object类是所有Java类的祖先.每个类都使用 Obje ...
- 关于windows注册表
Windows 注册表 应该是一个 数据库.里面包含操作系统以及在其上的软件配置信息和旗下的硬件配置信息,有点就是整体和全面,控制面包和gpedit.msc 组策略应该是抽象过后的注册表配置信息, W ...
- Day2 列表,元组,字典,集合
一,列表 定义:[]内以逗号分隔,按照索引,存放各种数据类型,每个位置代表一个元素. list=['alex', 'jack', 'chen', 'shaoye'] #创建一个列表. 特性: 1.可存 ...
- Vue 鼠标移入移出事件
Vue 中鼠标移入移出事件 @mouseover和@mouseleave 然后绑定style 现在开始代码示例 <template> <div class="pc&qu ...