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 权值大小 ...
随机推荐
- bzoj 4821 [Sdoi2017]相关分析
题面 https://www.lydsy.com/JudgeOnline/problem.php?id=4821 题解 做法显然 就是维护一颗线段树 里面装4个东西 区间x的和 区间y的和 区间$x^ ...
- bzoj 4860 [BeiJing2017]树的难题
题面 https://www.lydsy.com/JudgeOnline/problem.php?id=4860 题解 点分治 设当前重心为v 假设已经把所有边按照出发点第一关键字, 颜色第二关键字排 ...
- Codeforces Round #321 (Div. 2) A, B, C, D, E
580A. Kefa and First Steps 题目链接: A. Kefa and First Steps 题意描述: 给出一个序列,求最长不降连续子序列多长? 解题思路: 水题,签到 代码: ...
- [WOJ1318]和最大
题目链接: WOJ1318 题目分析: 首先我们要知道当这是一个线性的序列的时候应该怎么做:最大子序和 这里是线性的,就把数组复制两遍即可 好像有些细节要处理(也可能是我代码写丑了),具体的都在代码里 ...
- Codeforces Round #408 (Div. 2) A
Description Zane the wizard had never loved anyone before, until he fell in love with a girl, whose ...
- 2017zstu新生赛
1.b^3 - a^3 = c(zy) zy说要卡nlogn的,然而他实际给的组数只有100组,然后因为在windows下随机的,所以给出的 c <= 100000.然后只要胆子大.... 通过 ...
- 创建表规范 lob 字段
ORAClce 11g 提供如下特性: BasicfileOracle10g 及之前版本被称为basicfile Securefile11g中新增securefile 优点:集中写入缓存(WGC),4 ...
- 485 Max Consecutive Ones 最大连续1的个数
给定一个二进制数组, 计算其中最大连续1的个数.示例 1:输入: [1,1,0,1,1,1]输出: 3解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3.注意: 输入的数组只包 ...
- Windows下降权MYSQL和apche的运行级别(普通用户权限运行)
1.MYSQL的降权运行 新建立一个用户比如mysql net user mysql microsoft /add net localgroup users mysql /del 不属于任何组 ...
- AJPFX总结方法的特点
它可以实现独立的功能; 必须定义在类里面; 它只有被调用才会执行; 它可以被重复使用; 方法结束后方法里的对象失去引用; 如何定义一个功能,并通过方法体现出来: ① 明确该功能运算后的结果.明确返 ...