BZOJ3289:Mato的文件管理
浅谈莫队:https://www.cnblogs.com/AKMer/p/10374756.html
题目传送门:https://lydsy.com/JudgeOnline/problem.php?id=3289
用树状数组维护区间内元素的个数,动态删/加逆序对即可。
时间复杂度:\(O(n\sqrt{n}logn)\)
空间复杂度:$ O(n)$
代码如下:
#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
#define low(i) ((i)&(-(i)))
const int maxn=5e4+5;
ll ans;
int n,m,block,val,cnt;
int tmp[maxn],a[maxn],bel[maxn];
int read() {
int x=0,f=1;char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
return x*f;
}
struct query {
ll res;
int l,r,id;
bool operator<(const query &a)const {
if(bel[l]!=bel[a.l])return bel[l]<bel[a.l];
if(bel[l]&1)return r<a.r;return r>a.r;
}
}q[maxn];
struct TreeArray {
int c[maxn];
void add(int pos,int v) {
for(int i=pos;i<=n;i+=low(i))
c[i]+=v;
}
int query(int pos) {
int res=0;
for(int i=pos;i;i-=low(i))
res+=c[i];
return res;
}
}T;
bool cmp(query a,query b) {
return a.id<b.id;
}
void change(int siz,int v,bool big) {
if(!big)val=T.query(siz-1);
else val=T.query(n)-T.query(siz);
ans+=val*v;T.add(siz,v);
}
int main() {
n=read(),block=sqrt(n);
for(int i=1;i<=n;i++)
tmp[i]=a[i]=read(),bel[i]=(i-1)/block+1;
sort(tmp+1,tmp+n+1),m=read();
cnt=unique(tmp+1,tmp+n+1)-tmp-1;
for(int i=1;i<=m;i++)
q[i].l=read(),q[i].r=read(),q[i].id=i;
for(int i=1;i<=n;i++)
a[i]=lower_bound(tmp+1,tmp+cnt+1,a[i])-tmp;
sort(q+1,q+m+1);
int nowl=1,nowr=0;
for(int i=1;i<=m;i++) {
while(nowl<q[i].l)change(a[nowl++],-1,0);
while(nowl>q[i].l)change(a[--nowl],1,0);
while(nowr<q[i].r)change(a[++nowr],1,1);
while(nowr>q[i].r)change(a[nowr--],-1,1);
q[i].res=ans;
}
sort(q+1,q+m+1,cmp);
for(int i=1;i<=m;i++)
printf("%lld\n",q[i].res);
return 0;
}
BZOJ3289:Mato的文件管理的更多相关文章
- 数据离散化 ( 以及 stl 中的 unique( ) 的用法 )+ bzoj3289:Mato的文件管理
http://blog.csdn.net/gokou_ruri/article/details/7723378 ↑惯例Mark大神的博客 bzoj3289:Mato的文件管理 线段树求逆序对+莫队 ...
- [bzoj3289]Mato的文件管理_莫队_树状数组
Mato的文件管理 bzoj-3289 题目大意:给定一个n个数的序列.m次询问:一段区间中的逆序对个数. 注释:$1\le n\,mle 5\cdot 10^4$. 想法: 开始想这个题的大佬们,给 ...
- BZOJ3289 Mato的文件管理 【莫队 + 树状数组】
3289: Mato的文件管理 Time Limit: 40 Sec Memory Limit: 128 MB Submit: 3964 Solved: 1613 [Submit][Status] ...
- [bzoj3289]Mato的文件管理
Description Mato同学从各路神犇以各种方式(你们懂的)收集了许多资料,这些资料一共有n份,每份有一个大小和一个编号.为了防止他人偷拷,这些资料都是加密过的,只能用Mato自己写的程序才能 ...
- BZOJ3289 Mato的文件管理(莫队+树状数组)
这个做法非常显然. #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib& ...
- BZOJ3289 Mato的文件管理(莫队算法+树状数组)
题目是区间逆序数查询. 莫队算法..左或右区间向左或右延伸时加或减这个区间小于或大于新数的数的个数,这个个数用树状数组来统计,我用线段树超时了.询问个数和数字个数都记为n,数字范围不确定所以离散化,这 ...
- bzoj3289 Mato的文件管理 莫队+树状数组
求逆序对个数,莫队套树状数组 #include<cstdio> #include<iostream> #include<cstring> #include<c ...
- 【莫队算法】bzoj3289 Mato的文件管理
莫队算法,离线回答询问,按一定大小(sqrt(n*log(n))左右)将答案分块,按 ①左端点所在块②右端点 双关键字排序. 然后暴力转移. 转移的时候用树状数组. O(n*sqrt(n)*log(n ...
- 【BZOJ3289】Mato的文件管理 莫队算法+树状数组
[BZOJ3289]Mato的文件管理 Description Mato同学从各路神犇以各种方式(你们懂的)收集了许多资料,这些资料一共有n份,每份有一个大小和一个编号.为了防止他人偷拷,这些资料都是 ...
- BZOJ 3289: Mato的文件管理[莫队算法 树状数组]
3289: Mato的文件管理 Time Limit: 40 Sec Memory Limit: 128 MBSubmit: 2399 Solved: 988[Submit][Status][Di ...
随机推荐
- String和StringBufffer的区别
string的字符串操作都是废弃已有的对象,开辟一个新的内存空间创建一个新的对象 比如一个string str= "字符串"; str += "a"; 这样的操 ...
- Qt下TCP编程
一.服务器 1.声明一个QTcpServer对象 QTcpServer* serverListener; 2.new出对象 this->serverListener = new QTcpServ ...
- Virtual Container Hosts(VCHs) 介绍
In vSphere Integrated Containers, you deploy virtual container hosts (VCHs) that serve as Docker API ...
- python之命令行解析工具argparse
以前写python的时候都会自己在文件开头写一个usgae函数,用来加上各种注释,给用这个脚本的人提供帮助文档. 今天才知道原来python已经有一个自带的命令行解析工具argparse,用了一下,效 ...
- lockingModel in log4net 日志文件不能被其他进程写入
http://logging.apache.org/log4net/release/faq.html#single-file How do I get multiple process to log ...
- mysql删除重复记录
Solution 1: Add Unique Index on your table: ALTER IGNORE TABLE `TableA` ADD UNIQUE INDEX (`member_id ...
- 手机APP和微信小程序能否取代域名?
有人说现在App是主流,手机上装几个App就可以了,以后域名的重要性会越来越低,直至App完全取代域名的域名无用论.真的是这样吗? 关于这个话题已经有很多先人前辈探讨过,这次誉名网从另外一个角度给各位 ...
- Spring boot 更改端口的几种方式
web服务的默认端口是8080,但有时候我们需要更改成其他端口,这里有几种端口修改方式: 在application.properties文件添加配置(最简单) server.port= 在applic ...
- 安装Linux应该知道的十件事
硬件,软件及其之间的一切 Linux机器的硬件和软件之间一切都由内核主宰. 内核负责硬件和软件的连接,每隔几个礼拜就可以在Internet上获取 更新的内核: 如果你自己安装内核并非易事,这时,就轮到 ...
- lnmp安装exif扩展
lnmp安装exif扩展 1.找到位置 cd /usr/local/lnmp1.4-full/src 2.解压php sudo bzip2 -d php-5.6.31.tar.bz2 sudo tar ...