题目:

Description

对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对(i,j)的个数。给1到n的一个排列,按照某种顺序依次删除m个元素,你的任务是在每次删除一个元素之前统计整个序列的逆序对数。

Input

输入第一行包含两个整数nm,即初始元素的个数和删除的元素个数。以下n行每行包含一个1到n之间的正整数,即初始排列。以下m行每行一个正整数,依次为每次删除的元素。
 

Output

 
输出包含m行,依次为删除每个元素之前,逆序对的个数。

Sample Input

5 4
1
5
3
4
2
5
1
4
2

Sample Output

5
2
2
1

样例解释
(1,5,3,4,2)(1,3,4,2)(3,4,2)(3,2)(3)。

HINT

N<=100000 M<=50000

Source

题解:

同样的一道三维偏序题,将删除看成倒着插入,从而得出:<插入时间,位置,大小>(<t,a,b>),对于一个组数<t,a,b>,找寻(t>t1,a>a1且b<b1)的数量加到对应的ans[t]中,注意最后将ans叠加起来;

另外要注意在排完t后,a要正着排序求一遍ans然后倒着排序一遍ans,否则ans会少加(想想为什么单纯地求逆序对不用这样)

代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<cctype>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
const int N=1e5+;
struct node
{
int t,a,b;
}q[N],temp[N];
int n,m,tree[N],to[N],tag[N],tim;
long long ans[N];
long long Ans;
inline int R()
{
char c;int f=;
for(c=getchar();c<''||c>'';c=getchar());
for(;c<=''&&c>='';c=getchar())
f=(f<<)+(f<<)+c-'';
return f;
}
inline bool cmp(node a,node b)
{
return a.t<b.t;
}
inline bool comp(node a,node b)
{
return a.a<b.a;
}
inline void insert(int u,int v)
{
for(int i=u;i<=n;i+=(i&(-i)))
if(tag[i]!=tim) tag[i]=tim,tree[i]=v;
else tree[i]+=v;
}
inline bool comp2(node a,node b)
{
return a.a>b.a;
}
inline int query(int u)
{
int temp=;
for(int i=u;i;i-=(i&(-i)))
if(tag[i]!=tim) continue;
else temp+=tree[i];
return temp;
}
inline void solve1(int l,int r)
{
if(l==r) return;
int mid=(l+r)/;
solve1(l,mid),solve1(mid+,r);
int i=l,j=mid+,k=l;tim++;
while(i<=mid&&j<=r)
{
if(comp(q[i],q[j]))
{
insert(q[i].b,);
temp[k++]=q[i++];
}
else
{
ans[q[j].t]+=query(n)-query(q[j].b);
temp[k++]=q[j++];
}
}
while(i<=mid) temp[k++]=q[i++];
while(j<=r)
{
ans[q[j].t]+=query(n)-query(q[j].b);
temp[k++]=q[j++];
}
for(j=l;j<=r;j++) q[j]=temp[j]; }
inline void solve2(int l,int r)
{
if(l==r) return;
int mid=(l+r)/;
solve2(l,mid),solve2(mid+,r);
int i=l,j=mid+,k=l;tim++;
while(i<=mid&&j<=r)
{
if(comp(q[j],q[i]))
{
insert(q[i].b,);
temp[k++]=q[i++];
}
else
{
ans[q[j].t]+=query(q[j].b);
temp[k++]=q[j++];
}
}
while(i<=mid) temp[k++]=q[i++];
while(j<=r)
{
ans[q[j].t]+=query(q[j].b);
temp[k++]=q[j++];
}
for(j=l;j<=r;j++) q[j]=temp[j];
}
int main()
{
#ifndef ONLINE_JUDGE
//freopen("a.in","r",stdin);
#endif
n=R(),m=R();
for(int i=;i<=n;i++)
{
q[i].a=i,q[i].b=R();
to[q[i].b]=i;
}
int Time=n,a;
for(int i=;i<=m;i++)
{
a=R();q[to[a]].t=Time--;
}
for(int i=;i<=n;i++)
if(!q[i].t) q[i].t=Time--;
sort(q+,q+n+,cmp);
solve1(,n);
sort(q+,q+n+,cmp);
solve2(,n);
for(int i=;i<=n;i++)
Ans+=ans[i];
for(int i=n;i>n-m;i--)
printf("%lld\n",Ans),Ans-=ans[i];
return ;
}

刷题总结——动态逆序对(bzoj3295)的更多相关文章

  1. 【CQOI2011】动态逆序对 BZOJ3295

    Description 对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对(i,j)的个数.给1到n的一个排列,按照某种顺序依次删除m个元素,你的任务是在每次删除一个元素之前统计 ...

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

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

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

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

  4. 【BZOJ3295】动态逆序对(线段树,树状数组)

    [BZOJ3295]动态逆序对(线段树,树状数组) 题面 Description 对于序列A,它的逆序对数定义为满足iAj的数对(i,j)的个数.给1到n的一个排列,按照某种顺序依次删除m个元素,你的 ...

  5. 【BZOJ3295】[Cqoi2011]动态逆序对 cdq分治

    [BZOJ3295][Cqoi2011]动态逆序对 Description 对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对(i,j)的个数.给1到n的一个排列,按照某种顺序依 ...

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

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

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

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

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

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

  9. bzoj3295 洛谷P3157、1393 动态逆序对——树套树

    题目:bzoj3295 https://www.lydsy.com/JudgeOnline/problem.php?id=3295 洛谷 P3157(同一道题) https://www.luogu.o ...

随机推荐

  1. jmeter常量吞吐量定时器

    jmeter常量吞吐量定时器

  2. 简单shell执行脚本

    #!/bin/bash source /etc/profile APPLICATIONS_HOME="/opt/cpic_analy" APPLICATION_NAME=" ...

  3. iOS面试题 第一天

    今天上午,下午分别面试了两家公司.上午是一家互联网公司,气氛还比较好,是我比较喜欢的.技术这块是直接机试,主要是给了些BUG让我修复,整个过程还算顺利.下午去了一家大型的证券公司.整理技术问题如下: ...

  4. CentOS 6.7安装(一)

    CentOS 6.7安装 1.将光盘放入服务器,选择从光盘启动,选择“Install or upgrade an existing system”,并跳过光盘测试. 2.选择安装过程中使用的语言,默认 ...

  5. apache 报413

    http://www.hostlift.com/apache/modsecurity-request-body-content-length-is-larger-than-the-configured ...

  6. git 不完全教程

    概念 工作目录:当前所见,Working directory 暂存区域:以后要提交到仓库的文件,称为Index或者staging area Git 仓库:持久化存储快照的地方,HEAD指针所指向的地方 ...

  7. [LUOGU] P2196 挖地雷

    题目背景 NOIp1996提高组第三题 题目描述 在一个地图上有N个地窖(N<=20),每个地窖中埋有一定数量的地雷.同时,给出地窖之间的连接路径.当地窖及其连接的数据给出之后,某人可以从任一处 ...

  8. Mysql的慢日志

    一.开启慢查询日志,可以让MySQL记录下查询超过指定时间的语句,通过定位分析性能的瓶颈,才能更好的优化数据库系统的性能. 二.慢日志参数: slow_query_log 慢查询开启状态slow_qu ...

  9. centos7 parted 扩容

    (系统:vmware上的centos7.4 ,使用工具:parted分区命令.) 最近发现磁盘不够用了,需要加点.## WARNING ! 下面是实验过程,不代表生产环境.若有重要数据请操作前备份. ...

  10. Immutable 特性

    https://io-meter.com/2016/09/03/Functional-Go-persist-datastructure-intro/ 持久化的数据结构(Persistent Data ...