BZOJ3295:[CQOI2011]动态逆序对
浅谈树状数组与线段树:https://www.cnblogs.com/AKMer/p/9946944.html
题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=3295
树状数组套线段树,如题目所言,动态维护答案即可。
写了树套树之后我才发现递归是个多么傻逼的玩意儿……
时间复杂度:\(O(nlog^2n)\)
空间复杂度:\(O(nlogn)\)
代码如下:
#include <cstdio>
using namespace std;
typedef long long ll;
#define low(i) ((i)&(-(i)))
const int maxn=1e5+5;
ll ans;
int n,m;
int pos[maxn];
inline int read() {
int x=0,f=1;char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
return x*f;
}
struct segment_tree {
int tot;
int ls[maxn*85],rs[maxn*85],sum[maxn*85];
inline int query(int p,int l,int r,int pos,int opt) {
int res=0;
while(l!=r) {
int mid=(l+r)>>1;
if(pos<=mid) {
if(opt)res+=sum[rs[p]];
p=ls[p],r=mid;
}
else {
if(!opt)res+=sum[ls[p]];
p=rs[p],l=mid+1;
}
}
return res;
}
inline void change(int p,int l,int r,int pos,int v) {
if(!p)p=++tot;
while(1) {
sum[p]+=v;if(l==r)break;
int mid=(l+r)>>1;
if(pos<=mid) {
if(!ls[p])ls[p]=++tot;
p=ls[p],r=mid;
}
else {
if(!rs[p])rs[p]=++tot;
p=rs[p],l=mid+1;
}
}
}
}T;
struct TreeArray {
int rt[maxn];
inline int query(int pos,int v,int opt) {
int res=0;
for(int i=pos;i;i-=low(i))
res+=T.query(rt[i],1,n,v,opt);
return res;
}
inline void change(int pos,int x,int v) {
for(int i=pos;i<=n;i+=low(i)) {
if(!rt[i])rt[i]=++T.tot;
T.change(rt[i],1,n,x,v);
}
}
}bit;
int main() {
n=read(),m=read();
for(int i=1;i<=n;i++) {
int x=read();pos[x]=i;
ans+=bit.query(i-1,x,1);
bit.change(i,x,1);
}
for(int i=1;i<=m;i++) {
printf("%lld\n",ans);
int x=read();
ans-=bit.query(pos[x]-1,x,1);
ans-=bit.query(n,x,0)-bit.query(pos[x],x,0);
bit.change(pos[x],x,-1);
}
return 0;
}
BZOJ3295:[CQOI2011]动态逆序对的更多相关文章
- bzoj3295 [Cqoi2011]动态逆序对 cdq+树状数组
[bzoj3295][Cqoi2011]动态逆序对 2014年6月17日4,7954 Description 对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对(i,j)的个数. ...
- bzoj3295[Cqoi2011]动态逆序对 树套树
3295: [Cqoi2011]动态逆序对 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 5987 Solved: 2080[Submit][Sta ...
- 2018.07.01 BZOJ3295: [Cqoi2011]动态逆序对(带修主席树)
3295: [Cqoi2011]动态逆序对 **Time Limit: 10 Sec Memory Limit: 128 MB Description 对于序列A,它的逆序对数定义为满足i<j& ...
- [BZOJ3295][Cqoi2011]动态逆序对 CDQ分治&树套树
3295: [Cqoi2011]动态逆序对 Time Limit: 10 Sec Memory Limit: 128 MB Description 对于序列A,它的逆序对数定义为满足i<j,且 ...
- bzoj千题计划146:bzoj3295: [Cqoi2011]动态逆序对
http://www.lydsy.com/JudgeOnline/problem.php?id=3295 正着删除看做倒着添加 对答案有贡献的数对满足以下3个条件: 出现时间:i<=j 权值大小 ...
- BZOJ3295: [Cqoi2011]动态逆序对(树状数组套主席树)
3295: [Cqoi2011]动态逆序对 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 7465 Solved: 2662[Submit][Sta ...
- BZOJ3295 [Cqoi2011]动态逆序对 —— CDQ分治
题目链接:https://vjudge.net/problem/HYSBZ-3295 3295: [Cqoi2011]动态逆序对 Time Limit: 10 Sec Memory Limit: 1 ...
- bzoj3295: [Cqoi2011]动态逆序对(cdq分治+树状数组)
3295: [Cqoi2011]动态逆序对 题目:传送门 题解: 刚学完cdq分治,想起来之前有一道是树套树的题目可以用cdq分治来做...尝试一波 还是太弱了...想到了要做两次cdq...然后伏地 ...
- [bzoj3295][Cqoi2011]动态逆序对_主席树
动态逆序对 bzoj-3295 Cqoi-2011 题目大意:题目链接. 注释:略. 想法:直接建立主席树. 由于是一个一个删除,所以我们先拿建立好的root[n]的权值线段树先把总逆序对求出来,接着 ...
- bzoj3295: [Cqoi2011]动态逆序对(树套树)
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...
随机推荐
- redis配置认证密码(转)
redis配置密码 1.通过配置文件进行配置yum方式安装的redis配置文件通常在/etc/redis.conf中,打开配置文件找到 ? 1 #requirepass foobared 去掉行前的注 ...
- 目标检测之行人检测(Pedestrian Detection)---行人检测之简介0
一.论文 综述类的文章 [1]P.Dollar, C. Wojek,B. Schiele, et al. Pedestrian detection: an evaluation of the stat ...
- programming review (c++): (3)graph, binary search
I.graph #include <iostream> #include <vector> using namespace std; vector<vector<, ...
- 苹果开发之COCOA编程(第三版)下半部分
第十八章:Image和鼠标事件 1.NSResponderNSView继承自NSResponder类.所有的事件处理方法都定义在NSResponder类中.NSResponder申明了如下方法:- ( ...
- 获得手机的ip
本文转载至 http://blog.csdn.net/showhilllee/article/details/8746114 iosip手机 貌似ASI里获取ip地址的链接不可以了.也曾试过 ...
- Mysql代码建外键问题
用下面代码建外键 运行之后 没有提示错误 但是打开建好的表格 外键并没有建立上 打开外键栏 里面并没有外键 在从表设置了外键列里面输入东西没有任何限制 成功建立应该是下面这样 什么情况???????? ...
- 2017-01-20_dp测试
题目:http://files.cnblogs.com/files/shenben/2017-01-20problems.pdf 数据包(含解题报告):http://files.cnblogs.com ...
- 实现RTSP摄像机进行网页直播和微信直播的技术方案:EasyNVR自动更新方法
问题背景: 1.EasyNVR的用户越来越多,技术人员一一对应解答效率不高: 2.随着EasyNVR应用场景的不断增加,以及EasyNVR自身在技术上的不断优化,版本更新比较快: 3.由于开发人力有限 ...
- ArcGIS api for javascript 离线部署
一.说明: 1.本篇文章以及之后的相关的文章所使用的ArcGIS API for javascript 的版本是3.3.初学者为了学习简单,可以引用在线的 Javascript.但是如果只能连接内网, ...
- maven工作的过程
1 建立各个module之间的依赖关系 2 越底层的依赖的module先生成 3 下载远程库中的依赖 4 先生成本地被依赖的module 问题是,如何保证本次module和远程库中的包不重名?