题目

P1393 动态逆序对

做题前写篇博客是个好方法

做法

题目规定仅有删除,给每个位置标个号,逆序对+时间轴,显然这是个三维偏序

很久没做过\(cdq\)了,就当模板题讲一下:

按删除的先后顺序为关键字排序分治,然后在\(cdq\)中按位置排序,同时前部分删除的时间<=后部分删除时间

此时前部分删除的一个点会影响到的贡献在后部分处理:1.位置大值小查询-值大数量;2.位置小值大查询-值小数量

My complete code

#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long LL;
const LL maxn=1e5;
inline LL Read(){
LL x(0),f(1); char c=getchar();
while(c<'0'||c>'9'){
if(c=='-')f=-1; c=getchar();
}
while(c>='0'&&c<='9')
x=(x<<3)+(x<<1)+c-'0',c=getchar();
return x*f;
}
LL n,m,ans;
LL tree[maxn],a[maxn],tmp[maxn],D[maxn],low[maxn];
inline LL Lowbit(LL x){
return x&(-x);
}
inline void Add(LL x,LL val){
for(;x<=n;x+=Lowbit(x))
tree[x]+=val;
}
inline LL Query(LL x){
LL ret(0);
for(;x;x-=Lowbit(x))
ret+=tree[x];
return ret;
}
struct node{
LL del,val,id,ans;
}q[maxn];
inline bool cmp1(node x,node y){
return x.del<y.del;
}
inline bool cmp2(node x,node y){
return x.id<y.id;
}
void Cdq(LL l,LL r){
if(l==r)
return;
LL mid(l+r>>1);
Cdq(l,mid),Cdq(mid+1,r);
sort(q+l,q+mid+1,cmp2),sort(q+mid+1,q+r+1,cmp2);
LL j=mid;
for(LL i=l;i<=mid;++i){//big
while(j<r&&q[j+1].id<q[i].id)
Add(q[++j].val,1);
q[i].ans+=Query(n)-Query(q[i].val);
}
for(LL i=mid+1;i<=j;++i)
Add(q[i].val,-1);
j=r+1;
for(LL i=mid;i>=l;--i){
while(j>mid+1&&q[j-1].id>q[i].id)
Add(q[--j].val,1);
q[i].ans+=Query(q[i].val-1);
}
for(LL i=j;i<=r;++i)
Add(q[i].val,-1);
}
int main(){
n=Read(),m=Read();
for(LL i=1;i<=n;++i)
tmp[i]=a[i]=Read();
sort(tmp+1,tmp+1+n);
for(LL i=1;i<=n;++i)
a[i]=lower_bound(tmp+1,tmp+1+n,a[i])-tmp;
for(LL i=n;i>=1;--i){
ans+=Query(a[i]-1);
Add(a[i],1);
} for(LL i=1;i<=n;++i)
q[i]=(node){n+1,a[i],i,0};
for(LL i=1;i<=m;++i){
D[i]=Read();
q[D[i]].del=i;
}
memset(tree,0,sizeof(tree));
sort(q+1,q+1+n,cmp1);
Cdq(1,n);
for(LL i=1;i<=n;++i)
low[q[i].id]=q[i].ans;
printf("%lld ",ans);
for(LL i=1;i<=m;++i){
ans-=low[D[i]];
printf("%lld ",ans);
}
return 0;
}/*
6 3
5 4 2 6 3 1
2 1 4 11 7 4 2
*/

P1393 动态逆序对的更多相关文章

  1. 洛谷P1393 动态逆序对(CDQ分治)

    传送门 题解 听别人说这是洛谷用户的双倍经验啊……然而根本没有感觉到……因为另外的那题我是用树状数组套主席树做的……而且莫名其妙感觉那种方法思路更清晰(虽然码量稍稍大了那么一点点)……感谢Candy大 ...

  2. 【Luogu】P1393动态逆序对(树套树)

    题目链接 树套树. 每次删掉x的时候会减去1到x-1里比x位置的数大的数和它构成的逆序对,以及x+1到n里比x位置的数小的数和它构成的逆序对. 顺带一提我发现平衡树insert的时候不是要splay一 ...

  3. bzoj3295 洛谷P3157、1393 动态逆序对——树套树

    题目:bzoj3295 https://www.lydsy.com/JudgeOnline/problem.php?id=3295 洛谷 P3157(同一道题) https://www.luogu.o ...

  4. 【题解】动态逆序对 [CQOI2011] [P3157] [BZOJ3295] [P1393]

    [题解]动态逆序对 [CQOI2011] [P3157] [BZOJ3295] [P1393] 水一水QAQ 题目链接: \([P3157]\) \([BZOJ3295]\) [题目描述] 对于一个序 ...

  5. BZOJ 3295: [Cqoi2011]动态逆序对

    3295: [Cqoi2011]动态逆序对 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3865  Solved: 1298[Submit][Sta ...

  6. Bzoj 3295: [Cqoi2011]动态逆序对 分块,树状数组,逆序对

    3295: [Cqoi2011]动态逆序对 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2886  Solved: 924[Submit][Stat ...

  7. 【Luogu1393】动态逆序对(CDQ分治)

    [Luogu1393]动态逆序对(CDQ分治) 题面 题目描述 对于给定的一段正整数序列,我们定义它的逆序对的个数为序列中ai>aj且i < j的有序对(i,j)的个数.你需要计算出一个序 ...

  8. 【BZOJ3295】动态逆序对(线段树,树状数组)

    [BZOJ3295]动态逆序对(线段树,树状数组) 题面 Description 对于序列A,它的逆序对数定义为满足iAj的数对(i,j)的个数.给1到n的一个排列,按照某种顺序依次删除m个元素,你的 ...

  9. bzoj3295[Cqoi2011]动态逆序对 树套树

    3295: [Cqoi2011]动态逆序对 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 5987  Solved: 2080[Submit][Sta ...

随机推荐

  1. 解决eclipse中overlaps the location of another project: 'xxxx'

    找遍网络发现各种解释,最常见的一种是: new -> android project -> create project from exist source出现如下错误信息:Invalid ...

  2. 转: Appium ---移动自动化测试

    转自:http://www.cnblogs.com/nbkhic/p/3803830.html 什么是appium? 下面这段介绍来自于appium的官网. Appium is an open-sou ...

  3. Hadoop datanode无法启动

    原因是多次 hadoop namenode -format  导致 datanode 的version 里的Cluster-Id 不一致 这个Cluster-Id的位置在:  自己设置的数据临时文件/ ...

  4. static 修饰的变量在程序中容易出现的问题

    package lianxi; public class StaticTest {    int a = 0;    static int b =0;    StaticTest(){         ...

  5. hive beeline 的server启动与连接

    启动hiveServer2 启动beeline之后 连接 !connect jdbc:hive2://localhost:10000/default 启动的时候连接 /beeline -u jdbc: ...

  6. 机器学习5—logistic回归学习笔记

    机器学习实战之logistic回归 test5.py #-*- coding:utf-8 import sys sys.path.append("logRegres.py") fr ...

  7. vmware workstation导出ovf

    ovf tool路径 /Applications/VMware Fusion.app/Contents/Library/VMware OVF Tool 上面红色的部分,需要右键点击应用程序中的VMwa ...

  8. zabbix 自定义脚本监控activemq

    1. 编写获取activemq队列积压消息(check-amq.sh) #!/bin/bash QUEUENAME=$ MQ_IP='172.16.1.56' curl -uadmin:admin h ...

  9. 【JMeter4.0学习(十一)】之JMeter对(Mysql、Oracle)数据库性能测试脚本开发

    一.MySQL数据库链接: 注:下面所产生的问题一律参考详见:<[JMeter4.0]之遇到的问题总结(持续更新)>(包括Mysql.Orcale) 准备:引包,包路径一定要放对位置,参考 ...

  10. rebound是facebook的开源动画库

    网址:http://www.jcodecraeer.com/a/opensource/2015/0121/2338.html 介绍: rebound是facebook的开源动画库.可以认为这个动画库是 ...