这个用莫队做会被卡,但是我还是......

收获在于树状数组维护后缀和以及双维排序......

莫队的时间复杂度比想象中的要好一些....

然而我还是被卡了......

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define MAXN 100005
using namespace std;
int pos[MAXN],len,a[MAXN],front[MAXN],back[MAXN],n,m,t[MAXN],p,now,b[MAXN],live[MAXN];
struct Q
{
int pos,time,ans;
}q[MAXN];
int comp(const Q x,const Q y)
{
return pos[x.pos]<pos[y.pos]||(pos[x.pos]==pos[y.pos]&&x.time<y.time);
}
int end_comp(const Q x,const Q y)
{
return x.time<y.time;
}
inline int lowbit(int x)
{
return x&(-x);
}
inline void update_front(int x,int i)
{
if(x==)return;
while(x>)
{
front[x]+=i;
x-=lowbit(x);
}
}
inline void update_back(int x,int i)
{
if(x==)return;
while(x<=n)
{
back[x]+=i;
x+=lowbit(x);
}
}
inline int sum_front(int x)
{
int ret=;
while(x<=n)
{
ret+=front[x];
x+=lowbit(x);
}
return ret;
}
inline int sum_back(int x)
{
int ret=;
while(x>)
{
ret+=back[x];
x-=lowbit(x);
}
return ret;
}
long long ans;
void pre()
{
scanf("%d%d",&n,&m);
len=(int)(sqrt(n+0.5));
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
live[a[i]]=i;
pos[i]=(i-)/len+;
}
for(int i=n;i>;i--)
{
ans+=sum_back(a[i]-);
update_back(a[i],);
}
update_back(a[],-);
p=;
now=;
for(int i=;i<=m;i++)
{
scanf("%d",&b[i]);
q[i].pos=live[b[i]];
q[i].time=i;
}
sort(q+,q+m+,comp);
}
inline void via(int pl,int i)
{
a[live[pl]]=(i==)?pl:;
if(live[pl]<p)update_front(pl,i);
if(live[pl]>p)update_back(pl,i);
}
inline void do_front(int pl)
{
update_front(a[pl],);
update_back(a[pl+],-);
}
inline void do_back(int pl)
{
update_back(a[pl],);
update_front(a[pl-],-);
}
void work()
{
for(int i=;i<=m;i++)
{
while(now<q[i].time)via(b[now++],-);
while(now>q[i].time)via(b[--now],);
while(p<q[i].pos)do_front(p++);
while(p>q[i].pos)do_back(p--);
q[i].ans=sum_front(a[q[i].pos]+)+sum_back(a[q[i].pos]-);
}
}
void print()
{
sort(q+,q+m+,end_comp);
for(int i=;i<=m;i++)
{
printf("%lld\n",ans);
ans-=q[i].ans;
}
}
int main()
{
//freopen("inverse.in","r",stdin);
//freopen("inverse.out","w",stdout);
pre();
work();
print();
return ;
}

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]的权值线段树先把总逆序对求出来,接着 ...

随机推荐

  1. html 截图粘粘图片JS

    web前端socket聊天室功能和在线编辑器上传编辑内容的时候经常会需要上传一些图文信息,但是很多编辑器不支持截图粘粘的功能,这里参考了网友分享的可用方法做一个记录. <html> < ...

  2. Java应用:经纬度匹配(geohash加密)

    本文采用http://gc.ditu.aliyun.com地址进行经纬度匹配,无数量限制 如果给定经纬度进行geohash加密操作,先解密得到相应gps坐标,具体程序如下所示: import java ...

  3. python七类之列表元组

    列表 一.关键字:  list  lst = [ , , , , , , ,] lst = [1,2,3,4] 二.方法: 1.增加:​ . append( ) #追加​​​,添加元素进列表最后 ls ...

  4. STL——vector和list

    vector和list为STL中的顺序容器,顺序容器会依次维护第一个到最后一个元素,在顺序容器上,我们主要的操作就是迭代. 头文件: #include<vector> #include&l ...

  5. ABAP CDS ON HANA-(8)算術式

    Arithmetic expression in CDS View Allowed Arithmetic operators in CDS view. CDS View- @AbapCatalog.s ...

  6. ArrayMap java.lang.ArrayIndexOutOfBoundsException

    错误堆栈: java.lang.ArrayIndexOutOfBoundsException: length=0; index=1 at android.support.v4.util.SimpleA ...

  7. java doc 编写

    总而言之,我觉得有用的是: @see 只要敲了@see 然后会自动写你的类名的,很方便.# 去连接字段 {@link } 只要敲了{@link } 然后会自动写你的类名的,很方便.# 去连接字段 如果 ...

  8. crontab时间规则

    sudo crontab -e 5 * * * *每小时第5分钟执行*/5 * * * *每5分钟执行0 2 * * * 每天凌晨2点执行 cron是一个linux下的定时执行工具,可以在无需人工干预 ...

  9. Python连接符的种类和使用区别

    python的连接符主要有 加号(+).逗号(,).空格(   ) .反斜线(\).join()的方式. 加号(+),demo如下: #注意,+只能连接字符串,如果一个是字符串一个是数字就会报错 pr ...

  10. 问题:调用 ASP.Net Core WebAPI的HTTP POST方法时,从 [FromBody] 中读取的 MongoDB GeoJsonObjectModel成员总是null

    问题描述: POST/PUT to ASP.Net Core with [FromBody] to a MongoDB GeoJsonObjectModel member is always null ...