BZOJ3289 Mato的文件管理(莫队算法+树状数组)
题目是区间逆序数查询。
莫队算法。。左或右区间向左或右延伸时加或减这个区间小于或大于新数的数的个数,这个个数用树状数组来统计,我用线段树超时了。询问个数和数字个数都记为n,数字范围不确定所以离散化,这样时间复杂度就是$O(n^{\frac32}\log{n})$
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define MAXN 55555
int block;
struct Query{
int i,l,r;
bool operator<(const Query &q)const{
if(l/block==q.l/block) return r<q.r;
return l/block<q.l/block;
}
}que[MAXN]; int bit[MAXN],N;
inline int lowbit(int x){
return x&(-x);
}
void update(int x,int k){
while(x<=N){
bit[x]+=k;
x+=lowbit(x);
}
}
int query(int x){
if(x<=) return ;
int res=;
while(x){
res+=bit[x];
x-=lowbit(x);
}
return res;
} int b[MAXN],bn,res,tot;
void insertR(int i){
i=lower_bound(b+,b++bn,i)-b;
res+=tot-query(i);
update(i,);
++tot;
}
void removeR(int i){
i=lower_bound(b+,b++bn,i)-b;
res-=tot-query(i);
update(i,-);
--tot;
}
void insertL(int i){
i=lower_bound(b+,b++bn,i)-b;
res+=query(i-);
update(i,);
++tot;
}
void removeL(int i){
i=lower_bound(b+,b++bn,i)-b;
res-=query(i-);
update(i,-);
--tot;
}
int a[MAXN],ans[MAXN];
int main(){
int n,m;
scanf("%d",&n);
block=(int)sqrt(n);
for(int i=; i<=n; ++i){
scanf("%d",&a[i]);
b[i]=a[i];
}
sort(b+,b++n);
bn=unique(b+,b++n)-b-;
N=bn;
scanf("%d",&m);
for(int i=; i<m; ++i){
que[i].i=i;
scanf("%d%d",&que[i].l,&que[i].r);
}
sort(que,que+m);
int l=,r=; insertR(a[]);
for(int i=; i<m; ++i){
while(l<que[i].l) removeL(a[l++]);
while(l>que[i].l) insertL(a[--l]);
while(r>que[i].r) removeR(a[r--]);
while(r<que[i].r) insertR(a[++r]);
ans[que[i].i]=res;
}
for(int i=; i<m; ++i){
printf("%d\n",ans[i]);
}
return ;
}
BZOJ3289 Mato的文件管理(莫队算法+树状数组)的更多相关文章
- 【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 ...
- 【bzoj3289】Mato的文件管理 离散化+莫队算法+树状数组
原文地址:http://www.cnblogs.com/GXZlegend/p/6805224.html 题目描述 Mato同学从各路神犇以各种方式(你们懂的)收集了许多资料,这些资料一共有n份,每份 ...
- BZOJ 3289:Mato的文件管理(莫队算法+树状数组)
http://www.lydsy.com/JudgeOnline/problem.php?id=3289 题意:…… 思路:求交换次数即求逆序对数.确定了这个之后,先离散化数组.然后在后面插入元素的话 ...
- BZOJ3289【莫队算法+树状数组+离散化】
思路: 区间逆序数即是交换次数. 逆序数,可以用树状数组吧. 怎么处理区间变换的时候求逆序数啊.. 这里分成左边的增/删,右边的增/删 因为是按时序插入, 所以左边增,增一个数,计算:ans+=sun ...
- HDU 6278 - Just h-index - [莫队算法+树状数组+二分][2018JSCPC江苏省赛C题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6278 Time Limit: 6000/3000 MS (Java/Others) Memory Li ...
- HDU-6534-Chika and Friendly Pairs (莫队算法,树状数组,离散化)
链接: https://vjudge.net/contest/308446#problem/C 题意: Chika gives you an integer sequence a1,a2,-,an a ...
- 【BZOJ】3289: Mato的文件管理(莫队算法+树状数组)
http://www.lydsy.com/JudgeOnline/problem.php?id=3289 很裸的莫队... 离线了区间然后分块排序后,询问时搞搞就行了. 本题中,如果知道$[l, r] ...
- BZOJ 2120: 数颜色 带修改的莫队算法 树状数组套主席树
https://www.lydsy.com/JudgeOnline/problem.php?id=2120 标题里是两种不同的解法. 带修改的莫队和普通莫队比多了个修改操作,影响不大,但是注意一下细节 ...
随机推荐
- php面试题之五——MySQL数据库(基础部分)
五.MySQL数据库 mysql_num_rows() mysql_affected_rows() 这两个函数都作用于 mysql_query($query)操作的结果,mysql_num_rows( ...
- git参考资料
个人博客 http://www.iwangzheng.com/ $git log --graph $git reset --hard 67889898... $ssh-add $git pull -- ...
- [Educational Codeforces Round 16]E. Generate a String
[Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...
- window 安装 sass compass 记录
1.安装Ruby 安装sass 和compass 需要 Ruby 的环境,还区分xp 和win7-8 下版本区别 ruby 官网中文 ruby 官网英文 注意: xp: 下载 [xp不能下载包含64位 ...
- python 之验证码
验证码原理在于后台自动创建一张带有随机内容的图片,然后将内容通过img标签输出到页面. 安装图像处理模块: pip3 install pillow
- linux dd命令实用详解
linux dd命令刻录启动U盘详解 dd命令做usb启动盘十分方便,只须:sudo dd if=xxx.iso of=/dev/sdb bs=1M 用以上命令前必须卸载u盘,sdb是你的u盘,bs= ...
- Redis和Memcache的区别分析
1. Redis中,并不是所有的数据都一直存储在内存中的,这是和Memcached相比一个最大的区别. 2. Redis不仅仅支持简单的k/v类型的数据,同时还提供list,set,hash等数据结构 ...
- Redis使用介绍
Redis 是一个高性能的key-value数据库. redis的出现,很大程度补偿了memcached这类keyvalue存储的不足,在部 分场合可以对关系数据库起到很好的补充作用.它提供了Pyth ...
- MongoDB 学习笔记(python操作)
转自: http://blog.csdn.net/daillo/article/details/7030910
- 《ASP.NET MVC4 WEB编程》学习笔记------.net mvc实现原理ActionResult/View
ActionResult ActionResult是Action的返回结果.ActionResult 有多个派生类,每个子类功能均不同,并不是所有的子类都需要返回视图View,有些直接返回流,有些返回 ...