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. (转)c# Linq及Lamda表达式应用经验之 GroupBy 分组

    本文转载自:http://www.cnblogs.com/han1982/p/4138163.html 示例1: GroupBy 分组在List<>泛型中的应用 原表: 按姓名Nam 分组 ...

  2. 机器学习:模型泛化(岭回归:Ridge Regression)

    一.基础理解 模型正则化(Regularization) # 有多种操作方差,岭回归只是其中一种方式: 功能:通过限制超参数大小,解决过拟合或者模型含有的巨大的方差误差的问题: 影响拟合曲线的两个因子 ...

  3. laravel4 composer报错 d11wtq/boris v1.0.10 requires ext-pcntl

    laravel4 composer报错 d11wtq/boris v1.0.10 requires ext-pcntl laravel 4.2 用composer 安装任何包都会报这个错,通过谷歌找到 ...

  4. H.264学习笔记

    1.帧和场的概念 视频的一场或一帧可用来产生一个编码图像.通常,视频帧可以分成两种类型:连续或隔行视频帧.我们平常看的电视是每秒25帧,即每秒更换25个图像,由于视觉暂留效应,所以人眼不会感到闪烁.每 ...

  5. Android CTS(frome google)

    Compatibility Test Suite How does the CTS work? The Compatibility Test Suite (CTS) is a free, commer ...

  6. DAY19-Pillow制作验证码

    PIL:Python Imaging Library,已经是Python平台事实上的图像处理标准库了.PIL功能非常强大,但API却非常简单易用. 由于PIL仅支持到Python 2.7,加上年久失修 ...

  7. linux中安装sqlmap

    wget https://codeload.github.com/sqlmapproject/sqlmap/legacy.tar.gz/master //下载sqlmap tar zxvf maste ...

  8. 一段PHP异常

    这是我写的一段代码,里面通过PHP异常功能,实现报错时显示出错代码所在行.当使用者操作出错时,截图给我,我可以很快得去追踪和排查错误! public function added_business_s ...

  9. 基于cookie实现用户验证

    #!/usr/bin/env python import tornado.ioloop import tornado.web class IndexHander(tornado.web.Request ...

  10. ROS Learning-027 (提高篇-005 A Mobile Base-03) 控制移动平台 --- Twist 消息

    ROS 提高篇 之 A Mobile Base-03 - 控制移动平台 - Twist 消息 我使用的虚拟机软件:VMware Workstation 11 使用的Ubuntu系统:Ubuntu 14 ...