算法复习——莫队算法(bzoj1878)
题目:
Description
Input
Output
M行,每行一个整数,依次表示询问对应的答案。
Sample Input
1 2 3 4 3 5
3
1 2
3 5
2 6
Sample Output
2
4
HINT
Source
题解:
莫队算法模板题···也可以用分块做···
先说说针对数字序列的莫队吧···核心思想就是在题目允许询问离线的情况下,先将序列分块,然后将询问排序····第一关键字是left的所在块(越小越靠前)···第二关键字是right的大小(越小越靠前),然后创建tail和head指针,根据询问移动tail和head的同时更改所维护的答案·····
代码:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<cctype>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
const int N=5e4+;
const int M=2e5+;
struct node
{
int l,r,id;
}q[M];
int head,tail,id[N],num[N],cnt[],n,m,s,tots,ans,anss[M];
inline int R()
{
char c;int f=;
for(c=getchar();c<''||c>'';c=getchar());
for(;c<=''&&c>='';c=getchar())
f=(f<<)+(f<<)+c-'';
return f;
}
bool cmp(node x,node y)
{
return (id[x.l]<id[y.l])||(id[x.l]==id[y.l]&&x.r<y.r);
}
int main()
{
//freopen("a.in","r",stdin);
n=R();s=(int)sqrt(n);
for(int i=;i<=n;i++) num[i]=R();
m=R();
for(int i=;i<=m;i++)
q[i].l=R(),q[i].r=R(),q[i].id=i;
for(int i=;i<=n;i++)
{
if(i%s==) id[i]=++tots;
else id[i]=tots;
}
sort(q+,q+m+,cmp);
for(int i=;i<=m;i++)
{
while(head<q[i].l)
{
cnt[num[head]]--;
if(!cnt[num[head]]) ans--;
head++;
}
while(head>q[i].l)
{
head--;
if(!cnt[num[head]]) ans++;
cnt[num[head]]++;
}
while(tail>q[i].r)
{
cnt[num[tail]]--;
if(!cnt[num[tail]]) ans--;
tail--;
}
while(tail<q[i].r)
{
tail++;
if(!cnt[num[tail]]) ans++;
cnt[num[tail]]++;
}
anss[q[i].id]=ans;
}
for(int i=;i<=m;i++)
printf("%d\n",anss[i]);
return ;
}
算法复习——莫队算法(bzoj1878)的更多相关文章
- 信心题--FUOJ2226(莫队算法)
http://acm.fzu.edu.cn/problem.php?pid=2226 信心题,还说是信心题,题目给的真好.但是一点都不像信心题. 又是一个新的算法,莫队算法 莫队算法是一个用数组就可以 ...
- D. Powerful array 莫队算法或者说块状数组 其实都是有点优化的暴力
莫队算法就是优化的暴力算法.莫队算法是要把询问先按左端点属于的块排序,再按右端点排序.只是预先知道了所有的询问.可以合理的组织计算每个询问的顺序以此来降低复杂度. D. Powerful array ...
- XOR and Favorite Number(莫队算法+分块)
E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes input s ...
- 【国家集训队2010】小Z的袜子[莫队算法]
[莫队算法][国家集训队2010]小Z的袜子 Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼人的找袜子过程, ...
- 基于Manhattan最小生成树的莫队算法
点u,v的Manhattan距离:distance(u,v)= |x2-x1|+|y2-y1| Manhattan最小生成树:边权值为两个点Manhattan距离的最小生成树. 普通算法:prim复杂 ...
- 莫队算法详解和c实现
解析和实现 摘要: 莫队算法是一个对于区间.树或其他结构离线(在线)维护的算法,此算法基于一些基本算法,例如暴力维护,树状数组,分块,最小曼哈顿距离生成树,对其进行揉合从而产生的一个简单 ...
- 【BZOJ】4358: permu 莫队算法
[题意]给定长度为n的排列,m次询问区间[L,R]的最长连续值域.n<=50000. [算法]莫队算法 [题解]考虑莫队维护增加一个数的信息:设up[x]表示数值x往上延伸的最大长度,down[ ...
- hdu 5145(莫队算法+逆元)
NPY and girls Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- [BZOJ2038] [2009国家集训队]小Z的袜子(hose) 莫队算法练习
2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 10299 Solved: 4685[Sub ...
随机推荐
- 在使用线程池时应特别注意对ThreadLocal的使用
使用ThreadLocal并且有线程池时要特别注意,ThreadLocal是以线程为key的,而线程池里面的线程是会被重新利用的,所以如果有使用线程池并且使用ThreadLocal来保存状态信息时要特 ...
- 洛谷 P2922 [USACO08DEC]秘密消息Secret Message
题目描述 Bessie is leading the cows in an attempt to escape! To do this, the cows are sending secret bin ...
- 解决Starting to watch source with Jekyll and Compass. Starting Rack on port 4000
问题 Starting to watch source with Jekyll and Compass. Starting Rack on port 4000 rake aborted! Errno: ...
- SQLite连接
SQLite -连接 SQLite的联接子句用于从数据库中的两个或多个表合并的记录.JOIN是用于通过使用共同的每个值从两个表结合域的装置. SQL定义了三个主要类型的连接: CROSS JOIN I ...
- 移动端:active伪类无效的解决方法
:active伪类常用于设定点击状态下或其他被激活状态下一个链接的样式.最常用于锚点<a href="#">这种情况,一般主流浏览器下也支持其他元素,如button等. ...
- winhex与磁盘格式与 数据恢复
第一阶段: 熟悉WinHex的使用. n 熟悉磁盘工具的使用. n 利用WinHex查看物理磁盘和逻辑磁盘. n 了解WinHex中相关工具的用法. 以管理员身份运行winhex(以便之后修改) 上方 ...
- PAT (Basic Level) Practise (中文)-1038. 统计同成绩学生(20)
PAT (Basic Level) Practise (中文)-1038. 统计同成绩学生(20) http://www.patest.cn/contests/pat-b-practise/10 ...
- oracle row_number的使用
create table studentInfo( id number(8) primary key, name varchar2(20) not null, ObjectName varcha ...
- NOIP 成绩
这道题中点是在小数上,因为成绩可能是:“95.5 87.7……”所以我们就要用:printf和scanf这样就可以控制小数了!!! code: #include<bits/stdc++.h> ...
- mysql 与QT的连接
第一步:安装 qt开发环境 bi@bi-desktop:~$sudo apt-get install qt4-dev-tools qt4-doc qt4-qtconfig qt4-demos qt4- ...