A - D-query

Given a sequence of n numbers a1, a2, ..., an and a number of d-queries. A d-query is a pair (i, j) (1 ≤ i ≤ j ≤ n). For each d-query (i, j), you have to return the number of distinct elements in the subsequence ai, ai+1, ..., aj.

Input

  • Line 1: n (1 ≤ n ≤ 30000).
  • Line 2: n numbers a1, a2, ..., an (1 ≤ ai ≤ 106).
  • Line 3: q (1 ≤ q ≤ 200000), the number of d-queries.
  • In the next q lines, each line contains 2 numbers i, j representing a d-query (1 ≤ i ≤ j ≤ n).

Output

  • For each d-query (i, j), print the number of distinct elements in the subsequence ai, ai+1, ..., aj in a single line.

Example

Input
5
1 1 2 1 3
3
1 5
2 4
3 5 Output
3
2
3
解题思路:这道题就是给你n个数,q次查询,查询l到r区间有多少个不同的数字;此题用莫队算法;
代码如下:
 #include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cmath>
using namespace std; const int maxn = ;
int n ;
int m ;
int a[maxn];
int ans[maxn];
int vis[];
int block[maxn];
int blocksize ;
int count1 = ;
struct query{
int l ;
int r ;
int id ;
}q[maxn];
bool cmp(query a ,query b)
{
if(block[a.l]==block[b.l])
return a.r<b.r;
return block[a.l]<block[b.l];
}
void add(int num)
{
if(vis[a[num]]==)
count1++;
vis[a[num]]++;
}
void remove(int num){
if(vis[a[num]]==)
count1--;
vis[a[num]]--;
}
void solve()
{
int r = ;
int l = ;
for(int i = ; i < m;i++)
{
while(q[i].r > r)
{
r++;
add(r);
}
while(q[i].r<r)
{
remove(r);
r--;
}
while(q[i].l>l)
{
remove(l);
l++;
}
while(q[i].l<l)
{
l--;
add(l);
}
ans[q[i].id] = count1;
}
}
int main()
{
scanf("%d",&n);
blocksize = sqrt(n);
for(int i = ; i <= n ; i++)
{
scanf("%d",&a[i]);
block[i] = (i-)/blocksize + ;
}
scanf("%d",&m);
for(int i = ; i < m;i++)
{
scanf("%d%d",&q[i].l,&q[i].r);
q[i].id = i;
}
sort(q,q+m,cmp);
solve();
for(int i = ; i < m ;i++ )
{
printf("%d\n",ans[i]);
}
return ;
}

(原创)D-query SPOJ - DQUERY(莫队)统计不同数的数量的更多相关文章

  1. SPOJ - DQUERY 莫队

    题意:给定\(a[1...n]\),\(Q\)次询问,每次统计\([L,R]\)范围内有多少个不同的数字 xjb乱写就A了,莫队真好玩 #include<iostream> #includ ...

  2. SPOJ DQUERY - D-query (莫队算法|主席树|离线树状数组)

    DQUERY - D-query Given a sequence of n numbers a1, a2, ..., an and a number of d-queries. A d-query ...

  3. SPOJ - FREQ2 莫队 / n^1.5logn爆炸

    题意:给定\(a[1...n]\)和\(Q\)次询问,每次统计\([L,R]\)范围内出现频率最高的数的次数 想法没啥好说的,分别统计该数出现的次数和次数出现的次数,然后莫队暴力 注意本题时间卡的很紧 ...

  4. SP3267 DQUERY - D-query 莫队板子题

    题意可见:https://www.luogu.com.cn/problem/SP3267 可在vj上提交:https://vjudge.net/problem/SPOJ-DQUERY 题意翻译 给出一 ...

  5. D-query SPOJ - DQUERY(莫队)统计不同数的数量

    Given a sequence of n numbers a1, a2, ..., an and a number of d-queries. A d-query is a pair (i, j) ...

  6. SPOJ DQUERY 求区间内不同数的个数 主席树

    这题跟HDU3333差不多吧. 离线的做法很简单,不再说了 以前做过. 主席树的做法就比较暴力了.. 什么是主席树呢.. 其实是某种称号. 在该题中的体现是可持久化的线段树. 对于一个数 如果以前没出 ...

  7. 【带修莫队】bzoj2120 数颜色

    块大小为n2/3. 把询问和修改分开. 每次两个询问之间的修改进行暴力转移,如果修改在上一次询问的区间里,就会对当前状态形成影响. 好慢. #include<cstdio> #includ ...

  8. Luogu2336 SCOI2012 喵星球上的点名 SA、莫队

    传送门 一道很套路的题目 先将所有串拼在一起,两个不同的串之间放一个没有出现在任何串中的字符做分隔,然后SA 那么对于所有点名串能够点到的名字串在SA中对应一段区间 把这些区间拿出来然后莫队统计每一个 ...

  9. P1903 [国家集训队]数颜色 / 维护队列 带修改莫队

    题目描述 墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会向你发布如下指令: 1. Q L R代表询问你从第L支画笔到第R支画笔中共有几种不同颜色的画笔. 2 ...

随机推荐

  1. maven用途、核心概念、用法、常用参数和命令、扩展

    设置问题解决. http://trinea.iteye.com/blog/1290898 本文由浅入深,主要介绍maven的用途.核心概念(Pom.Repositories.Artifact.Buil ...

  2. POJ3258(最大化最小值)

    River Hopscotch Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11155   Accepted: 4785 ...

  3. U盘操作

    一.8G的U盘被设置为2G,如何恢复 1.下载usboot 1.7 打开以后,选中U盘,点击[点击此处选择工作模式],选择[用0重置参数] ,然后点[开始].2.完成后它会提示拔下U盘,此时拔下U盘, ...

  4. Java基础--虚拟机JVM

    JVM内存结构 Heap Space: 堆内存(Heap Space)是由Young Generation和Old Generation组成,而Young Generation又被分成三部分,Eden ...

  5. 我的MyGeneration

    话不多说,直接上代码 Interface Code: public class GeneratedGui : DotNetScriptGui { public GeneratedGui(ZeusCon ...

  6. nignx 重启

    sudo /opt/nginx/sbin/nginx -s stop sudo /opt/nginx/sbin/nginx

  7. C Primer Plus学习笔记(四)- 运算符、表达式和语句

    基本运算符 赋值运算符:= 在C语言中,=不是“相等”,而是赋值运算符,把左边的值赋给右边的变量 a = 2018; //把值2018赋给变量a 赋值表达式语句的目的是把值储存到内存位置上,用于储存值 ...

  8. virtualbox下安装的纯净版centOS7,无法访问外网

    virtualbox下安装的纯净版centOS7,网络设置如下: 需要在/etc/sysconfig/network-scripts/下编辑ifcfg-enp0s3,其中,NOBOOT设置成也是,就可 ...

  9. Changing Controller Numbers in Solaris

    If you need to change the controller numbers (c#) that a disk has assigned to it, whether it is for ...

  10. MySQL 删除字段数据某关键字后的所有数据

    ),'开发商') WHERE Compay LIKE '%开发商%'; sql附上