题目大意:给定一个长度为 N 的序列,M 个询问,每次询问区间逆序对的个数。

题解:用树状数组加速答案转移。

代码如下

#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) x.begin(),x.end()
#define cls(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int dx[]={0,1,0,-1};
const int dy[]={1,0,-1,0};
const int mod=1e9+7;
const int inf=0x3f3f3f3f;
const int maxn=5e4+10;
const double eps=1e-6;
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll sqr(ll x){return x*x;}
inline ll fpow(ll a,ll b,ll c){ll ret=1%c;for(;b;b>>=1,a=a*a%c)if(b&1)ret=ret*a%c;return ret;}
inline ll read(){
ll x=0,f=1;char ch;
do{ch=getchar();if(ch=='-')f=-1;}while(!isdigit(ch));
do{x=x*10+ch-'0';ch=getchar();}while(isdigit(ch));
return f*x;
}
/*------------------------------------------------------------*/ int n,m,bsize,a[maxn],d[maxn],tot;
struct query{int id,bl,l,r;}q[maxn];
bool cmp(const query &x,const query &y){return x.bl!=y.bl?x.bl<y.bl:(x.bl&1)?x.r<y.r:x.r>y.r;}
inline int get(int x){return (x-1)/bsize+1;} ll bit[maxn],now,l=1,r=0,ans[maxn];
void modify(int pos,ll val){
for(int i=pos;i<=n;i+=i&-i)bit[i]+=val;
}
ll query(int pos){
ll ret=0;
for(int i=pos;i;i-=i&-i)ret+=bit[i];
return ret;
} void read_and_parse(){
n=read(),bsize=sqrt(n);
for(int i=1;i<=n;i++)a[i]=d[i]=read();
sort(d+1,d+n+1);
tot=unique(d+1,d+n+1)-d-1;
for(int i=1;i<=n;i++)a[i]=lower_bound(d+1,d+tot+1,a[i])-d;
m=read();
for(int i=1;i<=m;i++){
q[i].id=i,q[i].l=read(),q[i].r=read();
q[i].bl=get(q[i].l);
}
sort(q+1,q+m+1,cmp);
} inline void update(int pos,int f){
if(f==0){// r +
now+=query(n)-query(a[pos]);
modify(a[pos],1);
}
else if(f==1){// r -
now-=query(n)-query(a[pos]);
modify(a[pos],-1);
}
else if(f==2){// l -
now+=query(a[pos]-1);
modify(a[pos],1);
}
else{
now-=query(a[pos]-1);
modify(a[pos],-1);
}
} void solve(){
for(int i=1;i<=m;i++){
while(r>q[i].r)update(r--,1);
while(r<q[i].r)update(++r,0);
while(l>q[i].l)update(--l,2);
while(l<q[i].l)update(l++,3);
ans[q[i].id]=now;
}
for(int i=1;i<=m;i++)printf("%lld\n",ans[i]);
}
int main(){
read_and_parse();
solve();
return 0;
}

【BZOJ3289】Mato的文件管理 莫队+树状数组的更多相关文章

  1. bzoj3289 Mato的文件管理 莫队+树状数组

    求逆序对个数,莫队套树状数组 #include<cstdio> #include<iostream> #include<cstring> #include<c ...

  2. bzoj 3289: Mato的文件管理 莫队+树状数组

    3289: Mato的文件管理 Time Limit: 40 Sec  Memory Limit: 128 MB[Submit][Status][Discuss] Description Mato同学 ...

  3. BZOJ3289[JZYZOJP2018]: Mato的文件管理 莫队+树状数组+离散化

            描述 Description     Mato同学从各路神犇以各种方式(你们懂的)收集了许多资料,这些资料一共有n份,每份有一个大小和一个编号.为了防止他人偷拷,这些资料都是加密过的, ...

  4. Bzoj 3289: Mato的文件管理 莫队,树状数组,逆序对,离散化,分块

    3289: Mato的文件管理 Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 1539  Solved: 665[Submit][Status][Di ...

  5. bzoj 3289 : Mato的文件管理 (莫队+树状数组)

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3289 思路: 求区间最小交换的次数将区间变成一个不降序列其实就是求区间逆序对的数量,这 ...

  6. BZOJ_3289_Mato的文件管理_莫队+树状数组

    BZOJ_3289_Mato的文件管理_莫队+树状数组 Description Mato同学从各路神犇以各种方式(你们懂的)收集了许多资料,这些资料一共有n份,每份有一个大小和一个编号 .为了防止他人 ...

  7. bzoj3236 作业 莫队+树状数组

    莫队+树状数组 #include<cstdio> #include<cstring> #include<iostream> #include<algorith ...

  8. BZOJ3236[Ahoi2013]作业——莫队+树状数组/莫队+分块

    题目描述 输入 输出 样例输入 3 4 1 2 2 1 2 1 3 1 2 1 1 1 3 1 3 2 3 2 3 样例输出 2 2 1 1 3 2 2 1 提示 N=100000,M=1000000 ...

  9. COGS.1822.[AHOI2013]作业(莫队 树状数组/分块)

    题目链接: COGS.BZOJ3236 Upd: 树状数组实现的是单点加 区间求和,采用值域分块可以\(O(1)\)修改\(O(sqrt(n))\)查询.同BZOJ3809. 莫队为\(O(n^{1. ...

随机推荐

  1. getMessage(),getFile,getLine获取异常用法

    try { $param = $request->all(); $param['building_id'] = 0; $param['sync'] = 2; // 1小程序2App $param ...

  2. Linux上的一些基本常用命令

    上传下载文件:// 首先安装lrzsz # yum -y install lrzsz // 上传文件,执行命令rz,会跳出文件选择窗口,选择好文件,点击确认即可.# rz // 下载文件,执行命令sz ...

  3. mac下php开发环境的搭建

    1.phpstorm 在官网:https://www.jetbrains.com/phpstorm/,下载最新版:phpstorm-2016.2.1 在http://15.idea.lanyus.co ...

  4. python爬虫scrapy之downloader_middleware设置proxy代理

    一.背景: 小编在爬虫的时候肯定会遇到被封杀的情况,昨天爬了一个网站,刚开始是可以了,在settings的设置DEFAULT_REQUEST_HEADERS伪装自己是chrome浏览器,刚开始是可以的 ...

  5. Golang的interface实践

    这是第二个我在别的语言里面没有见过的实现,go的interface可以说是独树一帜,让我们仔细来实践一下. interface类型是什么?interface类型定义了一组方法,如果某个对象实现了某个接 ...

  6. iperf网络测试

    iperf网络测试文档 地址: https://www.jianshu.com/p/942a9d9bc704

  7. Laravel从入门到精通

    1. Laravel框架的下载安装 例如: 在D:\test\laravel目录下新建一个目录为test_laravel 第一步,下载laravel框架 在D:\test\laravel\test_l ...

  8. 原型链上的call方法集合

    1. Object.prototype.toString.call(value) // 返回数据的类型 // "[object Object]" 等 2. Array.protot ...

  9. ReadWriteLock读写锁(八)

    前言:在JUC ReentrantReadWriteLock是基于AQS实现的读写锁实现. ReadWriteLock中定义了读写锁需要实现的接口,具体定义如下: public interface R ...

  10. [离散时间信号处理学习笔记] 9. z变换性质

    z变换描述 $x[n] \stackrel{\mathcal{Z}}{\longleftrightarrow}X(z) ,\quad ROC=R_x$ 序列$x[n]$经过z变换后得到复变函数$X(z ...