luogu3157 [CQOI2011]动态逆序对
先算出一个点前头比它大和后头比它小的数量。
每次删点就扔进一个主席树里头,防止造成重复删答案。
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int n, m, a[100005], p[100005], c[100005], uu, pre[100005], suf[100005];
int lson[5000005], rson[5000005], sum[5000005], orz, rot[100005];
long long ans;
int lowbit(int x){
return x&-x;
}
int getSum(int x){
int tmp=0;
for(int i=x; i; i-=lowbit(i)) tmp += c[i];
return tmp;
}
int query(int o, int l, int r, int x, int y){
if(!o) return 0;
if(l>=x && r<=y) return sum[o];
else{
int mid=(l+r)>>1;
int ans=0;
if(x<=mid) ans += query(lson[o], l, mid, x, y);
if(mid<y) ans += query(rson[o], mid+1, r, x, y);
return ans;
}
}
int queryRange(int uu, int vv, int xx, int yy){//在uu到vv之间有多少数在xx和yy之间
int tmp=0;
for(int i=uu-1; i; i-=lowbit(i))
tmp -= query(rot[i], 1, n, xx, yy);
for(int i=vv; i; i-=lowbit(i))
tmp += query(rot[i], 1, n, xx, yy);
return tmp;
}
void update(int &rt, int l, int r, int x){
if(!rt) rt = ++orz;
sum[rt]++;
if(l==r) return ;
int mid=(l+r)>>1;
if(x<=mid) update(lson[rt], l, mid, x);
if(mid<x) update(rson[rt], mid+1, r, x);
}
int main(){
cin>>n>>m;
for(int i=1; i<=n; i++){
scanf("%d", &a[i]);
p[a[i]] = i;
pre[i] = getSum(n) - getSum(a[i]);
ans += pre[i];
for(int j=a[i]; j<=n; j+=lowbit(j)) c[j]++;
}
memset(c, 0, sizeof(c));
for(int i=n; i; i--){
suf[i] = getSum(a[i]-1);
for(int j=a[i]; j<=n; j+=lowbit(j)) c[j]++;
}
while(m--){
printf("%lld\n", ans);
scanf("%d", &uu);
uu = p[uu];
ans -= pre[uu] + suf[uu];
ans += queryRange(1, uu-1, a[uu]+1, n);
ans += queryRange(uu+1, n, 1, a[uu]-1);
for(int i=uu; i<=n; i+=lowbit(i))
update(rot[i], 1, n, a[uu]);
}
return 0;
}
luogu3157 [CQOI2011]动态逆序对的更多相关文章
- BZOJ3295/Luogu3157 [CQOI2011]动态逆序对 (CDQ or 树套树 )
/* Dear friend, wanna learn CDQ? As a surprice, this code is totally wrong. You may ask, then why yo ...
- BZOJ 3295: [Cqoi2011]动态逆序对
3295: [Cqoi2011]动态逆序对 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3865 Solved: 1298[Submit][Sta ...
- Bzoj 3295: [Cqoi2011]动态逆序对 分块,树状数组,逆序对
3295: [Cqoi2011]动态逆序对 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2886 Solved: 924[Submit][Stat ...
- bzoj3295[Cqoi2011]动态逆序对 树套树
3295: [Cqoi2011]动态逆序对 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 5987 Solved: 2080[Submit][Sta ...
- P3157 [CQOI2011]动态逆序对(树状数组套线段树)
P3157 [CQOI2011]动态逆序对 树状数组套线段树 静态逆序对咋做?树状数组(别管归并QWQ) 然鹅动态的咋做? 我们考虑每次删除一个元素. 减去的就是与这个元素有关的逆序对数,介个可以预处 ...
- P3157 [CQOI2011]动态逆序对
P3157 [CQOI2011]动态逆序对 https://www.luogu.org/problemnew/show/P3157 题目描述 对于序列A,它的逆序对数定义为满足i<j,且Ai&g ...
- 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 权值大小 ...
随机推荐
- Codeforces Round #395 (Div. 2) A
Description Comrade Dujikov is busy choosing artists for Timofey's birthday and is recieving calls f ...
- 洛谷 P2714 四元组统计
错误记录:42行N写成n:事实上除了读入以外,此题的小n没有任何用途 #include<cstdio> #include<algorithm> #include<cstr ...
- Adding New Machine ZOJ - 3540
https://vjudge.net/problem/ZOJ-3540 错误记录: 扫描线没有考虑到同一行的要删除在前,加入在后:由于用了特殊的方式所以想当然以为不需要考虑这个问题 #include& ...
- 搭建一个高可用的redis环境
一.环境准备 我的环境: Fedora 25 server 64位版 6台: 192.168.10.204 192.168.10.205 192.168.10.206 192.168.10.203 ...
- React.js 的 context
这一节我们来介绍一个你可能永远用不上的 React.js 特性 —— context.但是了解它对于了解接下来要讲解的 React-redux 很有好处,所以大家可以简单了解一下它的概念和作用. 在过 ...
- 在input标签里只能输入数字
<input type='text' onkeyup="(this.v=function(){this.value=this.value.replace(/[^0-9-]+/,''); ...
- Backbone.js之Todo源码浅析
相信每个接触了解过backbone的人都知道todo,网上的关于它的分析教程也都分析乱了.但是,知识只有自己学习领悟才是自己的,话不多说,正文开始. 在分析todo的源码之前,首先我们要知道todo具 ...
- HYSBZ 1588 营业额统计 (Splay树)
题意:给出一个公司每一天的营业额,求每天的最小波动值之和.该天的最小波动值= min { 绝对值| 该天以前某一天的营业额-该天的营业额 | }.第一天的最小波动值就是其自己. 思路:Splay伸展树 ...
- 微信小程序开发系列教程三:微信小程序的调试方法
微信小程序开发系列教程 微信小程序开发系列一:微信小程序的申请和开发环境的搭建 微信小程序开发系列二:微信小程序的视图设计 这个教程的前两篇文章,介绍了如何用下图所示的微信开发者工具自动生成一个Hel ...
- Linux OpenGL 实践篇-10-framebuffer
在之前的实践中我们都是在当前的窗口中渲染,即使用的缓存都是由glutCreateWindow时创建的缓存,我们可称之为默认缓存.它是唯一一个可以被图形服务器的显示系统识别的帧缓存,我们在屏幕上看到的只 ...