浅谈树状数组与线段树: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]动态逆序对的更多相关文章

  1. bzoj3295 [Cqoi2011]动态逆序对 cdq+树状数组

    [bzoj3295][Cqoi2011]动态逆序对 2014年6月17日4,7954 Description 对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对(i,j)的个数. ...

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

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

  3. 2018.07.01 BZOJ3295: [Cqoi2011]动态逆序对(带修主席树)

    3295: [Cqoi2011]动态逆序对 **Time Limit: 10 Sec Memory Limit: 128 MB Description 对于序列A,它的逆序对数定义为满足i<j& ...

  4. [BZOJ3295][Cqoi2011]动态逆序对 CDQ分治&树套树

    3295: [Cqoi2011]动态逆序对 Time Limit: 10 Sec  Memory Limit: 128 MB Description 对于序列A,它的逆序对数定义为满足i<j,且 ...

  5. bzoj千题计划146:bzoj3295: [Cqoi2011]动态逆序对

    http://www.lydsy.com/JudgeOnline/problem.php?id=3295 正着删除看做倒着添加 对答案有贡献的数对满足以下3个条件: 出现时间:i<=j 权值大小 ...

  6. BZOJ3295: [Cqoi2011]动态逆序对(树状数组套主席树)

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

  7. BZOJ3295 [Cqoi2011]动态逆序对 —— CDQ分治

    题目链接:https://vjudge.net/problem/HYSBZ-3295 3295: [Cqoi2011]动态逆序对 Time Limit: 10 Sec  Memory Limit: 1 ...

  8. bzoj3295: [Cqoi2011]动态逆序对(cdq分治+树状数组)

    3295: [Cqoi2011]动态逆序对 题目:传送门 题解: 刚学完cdq分治,想起来之前有一道是树套树的题目可以用cdq分治来做...尝试一波 还是太弱了...想到了要做两次cdq...然后伏地 ...

  9. [bzoj3295][Cqoi2011]动态逆序对_主席树

    动态逆序对 bzoj-3295 Cqoi-2011 题目大意:题目链接. 注释:略. 想法:直接建立主席树. 由于是一个一个删除,所以我们先拿建立好的root[n]的权值线段树先把总逆序对求出来,接着 ...

  10. bzoj3295: [Cqoi2011]动态逆序对(树套树)

    #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...

随机推荐

  1. 数据挖掘之Slope One

    计算偏差: card() 表示集合包含的元素数量. http://www.cnblogs.com/similarface/p/5385176.html 论文地址:http://lemire.me/fr ...

  2. session 的工作原理以及使用细节和url编码

    /**********************************************模拟页面************************************************* ...

  3. 给定一颗完全二叉树,给每一层添加上next的指针,从左边指向右边

    给你机会发出声音,但是不给你机会证明高层的决定是错的 RT: 时间复杂度O(n) 空间复杂度O(1)  原理就是有指针指向父节点和当前的节点,左孩子必指向右孩子,右孩子必指向父节点的下一个节点的左孩子 ...

  4. 基于Apache POI 从xlsx读出数据

    [0]写在前面 0.1) these codes are from 基于Apache POI 的从xlsx读出数据 0.2) this idea is from http://cwind.iteye. ...

  5. oracle角色(role)概念

    一个角色是一组特权,它可以授权给用户或其它角色. 特权有:create table,select on boss ,create session,insert on boss,update on bo ...

  6. 关于海康视频采集卡的简介---基于pci的插潮采集卡

    vga 640x480 qvga vga的1/4,宽高分别是vga的一半 (1)采集类型 海康威视 DS-2CE16A2P-IT3P 700TVL 1/3" DIS ICR 红外防水筒型摄像 ...

  7. KVC基本使用

    首先,创建两个类.person类和book类.如图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/ ...

  8. 在WePY中实现了小程序的组件化开发,组件的所有业务与功能在组件本身实现,组件与组件之间彼此隔离,上述例子在WePY的组件化开发过程中,A组件只会影响到A所绑定的myclick

    wepyjs - 小程序组件化开发框架 https://tencent.github.io/wepy/document.html#/?id=%e5%be%ae%e4%bf%a1%e5%b0%8f%e7 ...

  9. (转)source insight 窗口嵌入

    昨天用了一下source insight ,都说很强大,也有感觉,但是这个强大的东西往往不是那么容易弄清楚的,或者一下子就好上手的,工具强大,功能复杂多样,一开始不知道怎么入手,以后慢慢来吧,学习是要 ...

  10. X86汇编 BT

    位操作指令位操作指令包括位测试和位扫描指令,可以直接对一个二进制位进行测试,设置和扫描. 1位测试和设置指令 格式:BT DEST,SRC BTC DEST,SRC BTR DEST,SRC BTS ...