同题:

    Codevs 2307 HH的项链

    BZOJ    1878 HH的项链

    洛谷      1972 HH的项链

2009年省队选拔赛山东

 时间限制: 1 s
 空间限制: 256000 KB
 题目等级 : 大师 Master
 查看运行结果
 
 
题目描述 Description

HH有一串由各种漂亮的贝壳组成的项链。HH相信不同的贝壳会带来好运,所以每次散步完后,他都会随意取出一段贝壳,思考它们所表达的含义。HH不断地收集新的贝壳,因此,他的项链变得越来越长。有一天,他突然提出了一个问题:某一段贝壳中,包含了多少种不同的贝壳?这个问题很难回答……因为项链实在是太长了。于是,他只好求助睿智的你,来解决这个问题。

输入描述 Input Description

第一行:一个整数N,表示项链的长度。

第二行:N个整数,表示依次表示项链中贝壳的编号(编号为0到1000000之间的整数)。

第三行:一个整数M,表示HH询问的个数。

接下来M行:每行两个整数,L和R(1 ≤ L ≤ R ≤ N),表示询问的区间。

输出描述 Output Description

M行,每行一个整数,依次表示询问对应的答案。

样例输入 Sample Input

6

1 2 3 4 3 5

3

1 2

3 5

2 6

样例输出 Sample Output

2

2

4

数据范围及提示 Data Size & Hint

对于20%的数据,N ≤ 100,M ≤ 1000;

对于40%的数据,N ≤ 3000,M ≤ 200000;

对于100%的数据,N ≤ 50000,M ≤ 200000。

分类标签 Tags 点此展开

 

题解:

这题首先在线是没法做的,所以我们可以考虑离线算法

首先记录下每种颜色的下一种颜色所在的位置

将所有询问按照左端点进行排序

将所有颜色的第一个点x a[x]++

然后从左往右扫

扫到一个点x将a[next[x]]++

碰到一个询问l,r输出sum[r]-sum[l-1]

其中sum是a数组的前缀和

求前缀和可以用树状数组

AC代码:

#include<bits/stdc++.h>
using namespace std;
#define N 50010
int n,m,maxn,a[N],next[N],c[N],p[N*];
struct node{
int l,r,id,ans;
}q[N<<];
inline int read(){
register int x=,f=;
register char ch=getchar();
while(ch>''||ch<''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
inline bool cmp1(const node &a,const node &b){
return a.l==b.l?a.r<b.r:a.l<b.l;
}
inline bool cmp2(const node &a,const node &b){
return a.id<b.id;
}
inline int lowbit(int x){
return x&-x;
}
inline void updata(int p){
for(int i=p;i<=n;i+=lowbit(i)) c[i]+=;
}
inline int query(int p){
int ans=;
for(int i=p;i>=;i-=lowbit(i)) ans+=c[i];
return ans;
}
int main(){
n=read();
for(int i=;i<=n;i++) a[i]=read(),maxn=max(maxn,a[i]);
for(int i=n;i;i--) next[i]=p[a[i]],p[a[i]]=i;
for(int i=;i<=maxn;i++) if(p[i]) updata(p[i]);
m=read();
for(int i=;i<=m;i++) q[i].l=read(),q[i].r=read(),q[i].id=i;
stable_sort(q+,q+m+,cmp1);
int l=;
for(int i=;i<=m;i++){
for(;l<q[i].l;l++) if(next[l]) updata(next[l]);
q[i].ans=query(q[i].r)-query(q[i].l-);
}
stable_sort(q+,q+m+,cmp2);
for(int i=;i<=m;i++) printf("%d\n",q[i].ans);
return ;
}

看完前面还不会,就用莫队水过吧。

莫队不会的戳这里

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
int read(){
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
const int M=1e6+,N=5e5+;
struct node{
int l,r,t,pos;
bool operator < (const node &a)const{
return pos==a.pos?r<a.r:pos<a.pos;
}
}b[N];
int n,m,l,r,sum,a[N],f[M],ans[N];
void add(int x){
if(f[x]==) sum++;
f[x]++;
}
void del(int x){
if(f[x]==) sum--;
f[x]--;
}
int main(){
freopen("diff.in","r",stdin);
freopen("diff.out","w",stdout);
n=read();
for(int i=;i<=n;i++) a[i]=read();
int k=sqrt(n*1.0)+0.5;
m=read();
for(int i=;i<=m;i++){
b[i].l=read();b[i].r=read();
b[i].t=i;b[i].pos=b[i].l/k;
}
sort(b+,b+m+);
memset(f,,sizeof f);
l=;r=;sum=;
for(int i=;i<=m;i++){
while(r>b[i].r){
del(a[r]);
r--;
}
while(r<b[i].r){
r++;
add(a[r]);
}
while(l>b[i].l){
l--;
add(a[l]);
}
while(l<b[i].l){
del(a[l]);
l++;
}
ans[b[i].t]=sum;
}
for(int i=;i<=m;i++) printf("%d\n",ans[i]);
return ;
}

Codevs 2307[SDOI2009]HH的项链的更多相关文章

  1. BZOJ 1878: [SDOI2009]HH的项链

    1878: [SDOI2009]HH的项链 Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 3548  Solved: 1757[Submit][Statu ...

  2. 【BZOJ1878】[SDOI2009]HH的项链 离线BIT

    1878: [SDOI2009]HH的项链 Description HH有一串由各种漂亮的贝壳组成的项链.HH相信不同的贝壳会带来好运,所以每次散步 完后,他都会随意取出一段贝壳,思考它们所表达的含义 ...

  3. BZOJ 1878: [SDOI2009]HH的项链 离线树状数组

    1878: [SDOI2009]HH的项链 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/p ...

  4. Bzoj 1878: [SDOI2009]HH的项链 莫队

    1878: [SDOI2009]HH的项链 Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 2717  Solved: 1363[Submit][Statu ...

  5. BZOJ 1878: [SDOI2009]HH的项链( BIT )

    离线处理 , 记下询问的左右端点并排序 , 然后可以利用树状数组 , 保证查询区间时每种颜色只计算一次 ------------------------------------------------ ...

  6. 洛谷 P1972 [SDOI2009]HH的项链【莫队算法学习】

    P1972 [SDOI2009]HH的项链 题目背景 无 题目描述 HH 有一串由各种漂亮的贝壳组成的项链.HH 相信不同的贝壳会带来好运,所以每次散步完后,他都会随意取出一段贝壳,思考它们所表达的含 ...

  7. BZOJ_1878_[SDOI2009]HH的项链_莫队

    BZOJ_1878_[SDOI2009]HH的项链_莫队 Description HH有一串由各种漂亮的贝壳组成的项链.HH相信不同的贝壳会带来好运,所以每次散步 完后,他都会随意取出一 段贝壳,思考 ...

  8. bzoj千题计划181:bzoj1878: [SDOI2009]HH的项链

    http://www.lydsy.com/JudgeOnline/problem.php?id=1878 之前用莫队做的,现在用树状数组 把每种数的第一个出现位置在树状数组中+1 nxt[i] 记录i ...

  9. 洛谷P1972 [SDOI2009]HH的项链 题解

    [SDOI2009]HH的项链 题目背景 无 题目描述 HH 有一串由各种漂亮的贝壳组成的项链.HH 相信不同的贝壳会带来好运,所以每次散步完后,他都会随意取出一段贝壳,思考它们所表达的含义.HH 不 ...

随机推荐

  1. Spring AOP Example – Advice

    Spring AOP + AspectJ Using AspectJ is more flexible and powerful. Spring AOP (Aspect-oriented progra ...

  2. codeforces 617BChocolate

    B. Chocolate time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  3. String 和 byte[]

    使用默认字符集合 Encodes this String into a sequence of bytes using the platform's default charset, storing ...

  4. mac 下对 iterm 终端 设置代理

    vi .profile export http_prox="http://xxxx:port" export https_proxy="http://xxxx:port& ...

  5. EasyUI datagrid添加右键菜单项

    js代码 //动态加载数据表格 function InitData() { $('#grid').datagrid({ url: '/Home/Query?r=' + Math.random(), / ...

  6. 通过yum安装Nagios

    通过yum安装Nagios 2012年04月05日 ⁄ Nagios ⁄ 暂无评论   QQ空间新浪微博腾讯微博人人网更多3   前提先自行安装好Apache+php 测试环境主监控机:CentOS ...

  7. Hadoop 2.4.0全然分布式平台搭建、配置、安装

    一:系统安装与配置 虚拟机软件:Virtualbox 4.3.10(已安装对应扩展包) 虚拟机:Ubuntu 13.04 LTS 32位(至于为什么选择13.04,是由于最新的版本号装上后开机会出现错 ...

  8. NEUOJ 1117: Ready to declare(单调队列)

    1117: Ready to declare 时间限制: 1 Sec  内存限制: 128 MB 提交: 358  解决: 41 [提交][状态][pid=1117" style=" ...

  9. C++ AfxBeginThread

    计算从1+2+3...+100000=? 关键点 CWinThread* AfxBeginThread( AFX_THREADPROC pfnThreadProc, LPVOID pParam, in ...

  10. android152 笔记 4

    42. Android中Task任务栈的分配. 首先我们来看下Task的定义,Google是这样定义Task的:a task is what the user experiences as an &q ...