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 权值大小 ...
随机推荐
- AtCoder Grand Contest 012 B
B - Splatter Painting Time limit : 2sec / Memory limit : 256MB Score : 700 points Problem Statement ...
- 三色灯渐变DIY制作
小编前几天查资料,怎么使用12864屏幕的用法,突然发觉微博是个好东西,随着自己的成长,学习了很多的知识,没有做笔记的习惯,只是习惯把用到的硬件,传感器,资料写到程序的备注内,但感觉,用到时不是那么方 ...
- Oracle及其相关软件历史版本下载地址
https://edelivery.oracle.com/osdc/faces/Home.jspx 打开上面这个链接,输入自己或可用的帐号即可. 搜索到自己想要下载的软件后,点击,软件会添加到购物车中 ...
- Asp.net MVC + Vue.js
@{ Layout = null; } <!DOCTYPE html><html> <head> <meta charset="UTF-8" ...
- Clean Code 第十章 : 类
最近的CleanCode读到了第十章.这一张主要讲了如何去构造一个类,感觉的CleanCode至此已经不仅仅是单纯的讲如何'写'出漂亮的代码,而是从设计方向上去构造出好的代码了. 本章节主要讲了: * ...
- 【HEVC帧间预测论文】P1.2 An Efficient Inter Mode Decision Approach for H.264 Video Codin
参考:An Efficient Inter Mode Decision Approach for H.264 Video Coding <HEVC标准介绍.HEVC帧间预测论文笔记>系列博 ...
- 使用windows的fsutil命令创建指定大小及类型的测试文件
在软件测试中,对于上传.下载一类功能常常需要用不同大小的文件进行测试. 使用Windows命令fsutil可以生成任意大小.任意类型文件. C:\Users\axia\fsutil file crea ...
- 再遇BGP
第一次遇到BGP,是在大学的课堂上,现在再次看到它,有种深深的无奈,我只记得它的名字,忘记了它的样子. 那么什么是BGP呢? 翻译过来就是边界网关协议,一个用来网络数据进行选路的路由协议,使用TCP协 ...
- lodash中文说明文档
lodash中文说明文档 https://www.css88.com/doc/lodash/
- python常用模块之random
random模块 import random print(random.random())#(0,1)----float 大于0且小于1之间的小数 print(random.randint(1,3)) ...