bzoj3295 动态逆序对
Description
Input
Output
将原数列建立归并树,归并树每个节点记录区间排序后结果、被删除的数的个数、树状数组记录前i个数被删了几个,每删除一个数前查找左边比它大的数和右边比它小的数的个数并更新答案。
#include<cstdio>
#include<algorithm>
inline int read(){
int x=,c=getchar();
while(c>||c<)c=getchar();
while(c>&&c<)x=x*+c-,c=getchar();
return x;
}
inline void mins(int&a,int b){if(a>b)a=b;}
inline void maxs(int&a,int b){if(a<b)a=b;}
long long ans=;
int t[],v[],id[];
int vs[][],bit[][],dt[][];
int X,v0;
void build(int L,int R,int h=){
int*x=vs[h];
if(L<R){
int M=L+R>>;
build(L,M,h+);
build(M+,R,h+);
int p1=L,p2=M+,*p3=x+L,*y=vs[h+];
while(p1<=M&&p2<=R)*(p3++)=y[(y[p1]<y[p2]?p1:p2)++];
while(p1<=M)*(p3++)=y[p1++];
while(p2<=R)*(p3++)=y[p2++];
}else x[L]=v[L];
}
void s1(int L,int R,int h=){
int*x=vs[h];
if(R<X){
int p=std::upper_bound(x+L,x+R+,v0)-(x+L);
ans-=R+-L-p;
for(int*a=bit[h]+L-;p;p-=p&-p)ans-=a[p];
ans+=dt[h][L];
return;
}
if(L!=R){
int M=L+R>>;
s1(L,M,h+);
if(X->M)s1(M+,R,h+);
}
}
void s2(int L,int R,int h=){
int*x=vs[h];
if(X<L){
int p=std::lower_bound(x+L,x+R+,v0)-(x+L);
ans-=p;
for(int*a=bit[h]+L-;p;p-=p&-p)ans+=a[p];
return;
}
if(L!=R){
int M=L+R>>;
if(X<M)s2(L,M,h+);
s2(M+,R,h+);
}
}
void del(int L,int R,int h=){
int*x=vs[h];
++dt[h][L];
int p=std::lower_bound(x+L,x+R+,v0)-(x+L)+;
for(int*a=bit[h]+L-,N=R-L+;p<=N;p+=p&-p)++a[p];
if(L!=R){
int M=L+R>>;
if(X<=M)del(L,M,h+);
else del(M+,R,h+);
}
}
int n,m;
int main(){
n=read();m=read();
for(int i=,a;i<=n;i++){
v[i]=a=read();
id[a]=i;
ans+=i-;
for(int w=a;w;w-=w&-w)ans-=t[w];
while(a<=n)++t[a],a+=a&-a;
}
build(,n);
for(int i=;i<=m;i++){
printf("%lld\n",ans);
v0=read();
X=id[v0];
s1(,n);
s2(,n);
del(,n);
}
return ;
}
bzoj3295 动态逆序对的更多相关文章
- BZOJ3295 动态逆序对(树状数组套线段树)
[Cqoi2011]动态逆序对 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 6058 Solved: 2117[Submit][Status][D ...
- BZOJ3295动态逆序对
一道比较傻的CDQ分治 CDQ: 主要用于解决三位偏序的问题 #include<cstdio> #include<cctype> #include<algorithm&g ...
- 【BZOJ3295】动态逆序对(线段树,树状数组)
[BZOJ3295]动态逆序对(线段树,树状数组) 题面 Description 对于序列A,它的逆序对数定义为满足iAj的数对(i,j)的个数.给1到n的一个排列,按照某种顺序依次删除m个元素,你的 ...
- 【BZOJ3295】[Cqoi2011]动态逆序对 cdq分治
[BZOJ3295][Cqoi2011]动态逆序对 Description 对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对(i,j)的个数.给1到n的一个排列,按照某种顺序依 ...
- bzoj3295 [Cqoi2011]动态逆序对 cdq+树状数组
[bzoj3295][Cqoi2011]动态逆序对 2014年6月17日4,7954 Description 对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对(i,j)的个数. ...
- bzoj3295 洛谷P3157、1393 动态逆序对——树套树
题目:bzoj3295 https://www.lydsy.com/JudgeOnline/problem.php?id=3295 洛谷 P3157(同一道题) https://www.luogu.o ...
- 【题解】动态逆序对 [CQOI2011] [P3157] [BZOJ3295] [P1393]
[题解]动态逆序对 [CQOI2011] [P3157] [BZOJ3295] [P1393] 水一水QAQ 题目链接: \([P3157]\) \([BZOJ3295]\) [题目描述] 对于一个序 ...
- [bzoj3295][Cqoi2011]动态逆序对_主席树
动态逆序对 bzoj-3295 Cqoi-2011 题目大意:题目链接. 注释:略. 想法:直接建立主席树. 由于是一个一个删除,所以我们先拿建立好的root[n]的权值线段树先把总逆序对求出来,接着 ...
- bzoj3295[Cqoi2011]动态逆序对 树套树
3295: [Cqoi2011]动态逆序对 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 5987 Solved: 2080[Submit][Sta ...
随机推荐
- 最小生成树之克鲁斯卡尔(Kruskal)算法
学习最小生成树算法之前我们先来了解下 下面这些概念: 树(Tree):如果一个无向连通图中不存在回路,则这种图称为树. 生成树 (Spanning Tree):无向连通图G的一个子图如果是一颗包含G的 ...
- sublime text3的快捷键
Ctrl + Shift + P 调出命令板(Command Palette) Ctrl + ` 调出控制台 Ctrl + D 选择当前光标所在的词并高亮该词所有出现的位置,再次 C ...
- beta阶段贡献分配实施
作业要求[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2281] 要求1 每位组员的贡献分值 刘莹莹 王玉潘 潘世维 周昊 赵美增 ...
- L237
The British parliament on Tuesday rejected overwhelmingly the Brexit deal, further complicating the ...
- Bad update sites
Bad update sites com.genuitec.pulse2.client.common.launcher.BadUpdateSiteException Software being in ...
- pygame经典sprite精灵类
import cStringIO, base64 import pygame from pygame.locals import * class Ball(pygame.sprite.Sprite): ...
- cache和buffer区别
Cache: 一般用于读缓存,用于将频繁读取的内容放入缓存,下次在读取相同的内容,直接从缓存冲读取,提高读取性能,缓存可以有多级. Buffer:一般用于写缓存,用于解决不同介质直接存储速度的不同, ...
- 一步步入门log4cpp
前言 项目实现过程中,需要检查.查找或者调试程序bug等,此时程序日志则较为清晰地展现代码的运行过程.目前接触到的方法有打印消息到控制台,将重要信息输出到某个文件比如txt文件,或者直接使用日志库. ...
- Nginx+Keepalive实现高可用负载均衡
1.准备2台服务器 服务器名LB1,假设IP为192.168.1.100 服务器名LB2,假设IP为192.168.1.101 2.在LB1,LB2上分别安装Nginx 步骤参照:http://blo ...
- 2018-2019-2 《网络对抗技术》Kali安装 Week1 20165212
2018-2019-2 <网络对抗技术>Kali安装 Week1 20165212 1.完成安装linux kali和vm tools 装的第三遍成功安装.前面两次镜像文件不行,没驱动(网 ...