bzoj3289
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3289
题目大意:Mato同学从各路神犇以各种方式(你们懂的)收集了许多资料,这些资料一共有n份,每份有一个大小和一个编号。为了防止他人偷拷,这些资料都是加密过的,只能用Mato自己写的程序才能访问。Mato每天随机选 一个区间[l,r],他今天就看编号在此区间内的这些资料。Mato有一个习惯,他总是从文件大小从小到大看资料。他先把要看的文件按编号顺序依次拷贝出来,再用他写的排序程序给文件大小排序。排序程序可以在1单 位时间内交换2个相邻的文件(因为加密需要,不能随机访问)。Mato想要使文件交换次数最小,你能告诉他每天需要交换多少次吗?
题解:莫队+树状数组+逆序对
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#define maxn 100000
using namespace std;
int n,m;
int pos[maxn],val[maxn],c[maxn],bo[maxn],ans[maxn*];
struct data{
int l,r,a,b,id;
}a[maxn*];
int read()
{
int x=; char ch; bool bo=;
while (ch=getchar(),ch<''||ch>'') if (ch=='-') bo=;
while (x=x*+ch-'',ch=getchar(),ch>=''&&ch<='') ;
if (bo) return -x; return x;
}
int lowbit(int x){ return x&-x;
}
void add(int x,int v)
{
for (int i=x; i<=n; i+=lowbit(i)) c[i]+=v;
}
int query(int x)
{
int res=;
for (int i=x; i; i-=lowbit(i)) res+=c[i];
return res;
}
bool cmp(data a,data b)
{
if (pos[a.l]==pos[b.l])
if (pos[a.l]&) return a.r<b.r;
else return a.r>b.r;
return pos[a.l]<pos[b.l];
}
void init()
{
n=read(),m=read();
for (int i=; i<=n; i++) val[i]=read();
for (int i=; i<=m; i++)
{
a[i].l=read(),a[i].r=read(),a[i].a=read(),a[i].b=read(); a[i].id=i;
}
int kk=int (sqrt(n));
for (int i=; i<=n; i++) pos[i]=(i-)/kk+;
sort(a+,a++m,cmp);
}
void updata(int k,int vval)
{
if (vval==-)
{
if (bo[val[k]]==) add(val[k],-);
bo[val[k]]--;
}
else
{
if (!bo[val[k]]) add(val[k],);
bo[val[k]]++;
}
}
void work()
{
int r=,l=;
for (int i=; i<=m; i++)
{
for (; r<a[i].r; r++) updata(r+,);
for (; r>a[i].r; r--) updata(r,-);
for (; l<a[i].l; l++) updata(l,-);
for (; l>a[i].l; l--) updata(l-,);
ans[a[i].id]=query(a[i].b)-query(a[i].a-);
}
for (int i=;i<=m; i++)
printf("%d\n",ans[i]);
}
int main()
{
init();
work();
}
bzoj3289的更多相关文章
- 【bzoj3289】 Mato的文件管理
http://www.lydsy.com/JudgeOnline/problem.php?id=3289 (题目链接) 题意 求区间逆序对 Solution 离线无修改查询,莫队转移:树状数组维护区间 ...
- BZOJ3289 Mato的文件管理(莫队+树状数组)
这个做法非常显然. #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib& ...
- 数据离散化 ( 以及 stl 中的 unique( ) 的用法 )+ bzoj3289:Mato的文件管理
http://blog.csdn.net/gokou_ruri/article/details/7723378 ↑惯例Mark大神的博客 bzoj3289:Mato的文件管理 线段树求逆序对+莫队 ...
- 【BZOJ3289】Mato的文件管理 莫队算法+树状数组
[BZOJ3289]Mato的文件管理 Description Mato同学从各路神犇以各种方式(你们懂的)收集了许多资料,这些资料一共有n份,每份有一个大小和一个编号.为了防止他人偷拷,这些资料都是 ...
- [bzoj3289]Mato的文件管理_莫队_树状数组
Mato的文件管理 bzoj-3289 题目大意:给定一个n个数的序列.m次询问:一段区间中的逆序对个数. 注释:$1\le n\,mle 5\cdot 10^4$. 想法: 开始想这个题的大佬们,给 ...
- [bzoj3289]Mato的文件管理
Description Mato同学从各路神犇以各种方式(你们懂的)收集了许多资料,这些资料一共有n份,每份有一个大小和一个编号.为了防止他人偷拷,这些资料都是加密过的,只能用Mato自己写的程序才能 ...
- BZOJ3289 Mato的文件管理(莫队算法+树状数组)
题目是区间逆序数查询. 莫队算法..左或右区间向左或右延伸时加或减这个区间小于或大于新数的数的个数,这个个数用树状数组来统计,我用线段树超时了.询问个数和数字个数都记为n,数字范围不确定所以离散化,这 ...
- bzoj3289 Mato的文件管理 莫队+树状数组
求逆序对个数,莫队套树状数组 #include<cstdio> #include<iostream> #include<cstring> #include<c ...
- 【BZOJ3289】Mato的文件管理 莫队+树状数组
题目大意:给定一个长度为 N 的序列,M 个询问,每次询问区间逆序对的个数. 题解:用树状数组加速答案转移. 代码如下 #include <bits/stdc++.h> #define f ...
随机推荐
- PAT1016
A long-distance telephone company charges its customers by the following rules: 一个长途电话公司费用告诉它的顾客需要遵循 ...
- java OPENCV 连通域, Imgproc.findContours 例子,参数说明
http://stackoverflow.com/questions/29491669/real-time-paper-sheet-detection-using-opencv-in-android/ ...
- hdu_5724_Chess(组合博弈)
题目链接:hdu_5724_Chess 题意: 给你一个n行20列的棋盘,棋盘里面有些棋子,每个棋子每次只能往右走一步,如果右边有棋子,可以跳过去,前提是最右边有格子,如果当前选手走到没有棋子可以走了 ...
- 使用cisco SDM管理路由器
实验拓扑: 实验目的:掌握如何通过SDM对路由器进行管理 实验需求:设置R1使其能够通过SDM进行管理 实验步骤: 步骤一:配置基本IP地址 步骤二:在R1上进行设置,使其能够通过SDM连接 R1(c ...
- xmppserver
http://highscalability.com/blog/2014/1/6/how-hipchat-stores-and-indexes-billions-of-messages-using-e ...
- json optString getString
optString 和 getString 区别. optString 当接收到的为空时候 不会报错
- fragment 数据传递,传值,通信
[原][Fragment精深系列4]Fragment与Activity之间的数据交互 2015-5-26阅读389 评论0 以下内容来自于自己的实践和网络资料的整理,希望对你有帮助. 一.Acti ...
- smarty的ASSIGN()函数
http://blog.sina.com.cn/s/blog_6721f25c01011qdj.html 主要是把程序里面的值付给模板,因为使用smarty时,模板里面是没有PHP代码的,无法显示在操 ...
- 神经网络joone_engin模式识别示范,eclipse
链接: http://pan.baidu.com/s/1kVRducv 密码: junw
- 用apache配置多个tomcat webapp
The Apache HTTP Server Project is an effort to develop and maintain an open-source HTTP server for m ...