题目链接:https://www.nowcoder.com/acm/contest/139/J

题目描述 

Given a sequence of integers a1, a2, ..., an and q pairs of integers (l1, r1), (l2, r2), ..., (lq, rq), find count(l1, r1), count(l2, r2), ..., count(lq, rq) where count(i, j) is the number of different integers among a1, a2, ..., ai, aj, aj + 1, ..., an.

输入描述:

The input consists of several test cases and is terminated by end-of-file.
The first line of each test cases contains two integers n and q.
The second line contains n integers a1, a2, ..., an.
The i-th of the following q lines contains two integers li and ri.

输出描述:

For each test case, print q integers which denote the result.

输入

3 2
1 2 1
1 2
1 3
4 1
1 2 3 4
1 3

输出

2
1
3

备注:

* 1 ≤ n, q ≤ 105
* 1 ≤ ai ≤ n
* 1 ≤ li, ri ≤ n
* The number of test cases does not exceed 10.

题意:

给出序列a[1:n],给出q个查询,每个查询包含(l,r),要求回答a[1:l]和a[r:n]合起来有多少不同的整数。

题解:

莫队算法。

统计a[1:n]有多少不同的整数,记为sum,所有 r - l ≤ 1 的查询答案为sum,

cnt[k]表示整数k出现了几次,sum的变动根据cnt[k]的0→1以及1→0变化判断。

AC代码:

#include<bits/stdc++.h>
using namespace std;
const int MAXN=1e5+;
const int MAXQ=1e5+; struct Query
{
int block;
int id;
int l,r;
bool operator <(const Query &oth) const
{
return (this->block == oth.block) ? (this->r < oth.r) : (this->block < oth.block);
}
}query[MAXQ]; int n,q;
int sum;
int num[MAXN],cnt[MAXN];
int ans[MAXQ]; int main()
{
while(scanf("%d%d",&n,&q)!=EOF)
{
memset(cnt,,sizeof(cnt));
sum=;
for(int i=;i<=n;i++)
{
scanf("%d",&num[i]);
if(cnt[num[i]]==) sum++;
cnt[num[i]]++;
} int len=sqrt(n);
for(int i=;i<=q;i++)
{
scanf("%d%d",&query[i].l,&query[i].r);
if(query[i].r-query[i].l<=) ans[i]=sum;
query[i].block=query[i].l/len;
query[i].id=i;
}
sort(query+,query+q+); int pl=,pr=;
for(int i=;i<=q;i++)
{
if(query[i].r-query[i].l<=) continue; if(pr<query[i].r)
{
for(int j=pr;j<query[i].r;j++)
{
cnt[num[j]]--;
if(cnt[num[j]]==) sum--;
}
}
if(pr>query[i].r)
{
for(int j=pr-;j>=query[i].r;j--)
{
if(cnt[num[j]]==) sum++;
cnt[num[j]]++;
}
}
if(pl<query[i].l)
{
for(int j=pl+;j<=query[i].l;j++)
{
if(cnt[num[j]]==) sum++;
cnt[num[j]]++;
}
}
if(pl>query[i].l)
{
for(int j=pl;j>query[i].l;j--)
{
cnt[num[j]]--;
if(cnt[num[j]]==) sum--;
}
} ans[query[i].id]=sum; pl=query[i].l;
pr=query[i].r;
} for(int i=;i<=q;i++)
{
printf("%d\n",ans[i]);
}
}
}

2018牛客网暑期ACM多校训练营(第一场) J - Different Integers - [莫队算法]的更多相关文章

  1. 2018牛客网暑期ACM多校训练营(第二场)I- car ( 思维)

    2018牛客网暑期ACM多校训练营(第二场)I- car 链接:https://ac.nowcoder.com/acm/contest/140/I来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 ...

  2. 2018牛客网暑期ACM多校训练营(第一场)D图同构,J

    链接:https://www.nowcoder.com/acm/contest/139/D来源:牛客网 同构图:假设G=(V,E)和G1=(V1,E1)是两个图,如果存在一个双射m:V→V1,使得对所 ...

  3. 2018 牛客网暑期ACM多校训练营(第一场) E Removal (DP)

    Removal 链接:https://ac.nowcoder.com/acm/contest/139/E来源:牛客网 题目描述 Bobo has a sequence of integers s1, ...

  4. 2018牛客网暑期ACM多校训练营(第十场)A Rikka with Lowbit (树状数组)

    链接:https://ac.nowcoder.com/acm/contest/148/A 来源:牛客网 Rikka with Lowbit 时间限制:C/C++ 5秒,其他语言10秒 空间限制:C/C ...

  5. 2018牛客网暑期ACM多校训练营(第十场)J Rikka with Nickname(二分,字符串)

    链接:https://ac.nowcoder.com/acm/contest/148/J?&headNav=acm 来源:牛客网 Rikka with Nickname 时间限制:C/C++ ...

  6. 2018牛客网暑期ACM多校训练营(第二场)J Farm(树状数组)

    题意 n*m的农场有若干种不同种类作物,如果作物接受了不同种类的肥料就会枯萎.现在进行t次施肥,每次对一个矩形区域施某种类的肥料.问最后枯萎的作物是多少. 分析 作者:xseventh链接:https ...

  7. 2018牛客网暑期ACM多校训练营(第一场)B Symmetric Matrix(思维+数列递推)

    题意 给出一个矩阵,矩阵每行的和必须为2,且是一个主对称矩阵.问你大小为n的这样的合法矩阵有多少个. 分析 作者:美食不可负064链接:https://www.nowcoder.com/discuss ...

  8. 2018牛客网暑期ACM多校训练营(第三场) A - PACM Team - [四维01背包][四约束01背包]

    题目链接:https://www.nowcoder.com/acm/contest/141/A 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K ...

  9. 2018牛客网暑期ACM多校训练营(第五场) F - take - [数学期望][树状数组]

    题目链接:https://www.nowcoder.com/acm/contest/143/F 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K ...

随机推荐

  1. Nexus5 电信3G保留数据和Root升级Android 6.0

    前提: A 备份手机重要数据,安全第一 B 进入twrp recovery 备份EFS,建议最好拷贝到电脑上(如果没有twrp,则需要先刷twrp,具体指令请看下面步骤第10条) C 因为Androi ...

  2. 大杂烩 -- Java中Iterator的fast-fail分析

    基础大杂烩 -- 目录 Java中的Iterator非常方便地为所有的数据源提供了一个统一的数据读取(删除)的接口,但是新手通常在使用的时候容易报如下错误ConcurrentModificationE ...

  3. iOS NSError

    写在前面 在iOS开发中,NSError的使用非常常见,使用也比较简单,也正因为简单,所以对这一部分知识不甚注重.但是近期在做app底层网络封装时发现了一些问题.我使用的网络框架是AFNetworki ...

  4. Robot Framework配置发送邮件功能

    请参考:http://www.robotframework.net/?/article/118

  5. 【RF库Built-In测试】Catenate

    Name:CatenateSource:BuiltIn <test library>Arguments:[ *items ]Catenates the given items togeth ...

  6. Jar命令

    JAR包是Java中所特有一种压缩文档,其实大家就可以把它理解为.zip包;当然也是有区别的,JAR包中有一个META-INF\MANIFEST.MF文件,当你打成JAR包时,它会自动生成. 一.ja ...

  7. [Ubuntu] geoip-bin 程序包 - 查询 IP 归属地

    简述:在Linux命令行下查询IP归属地. 对Ubuntu/Debian系统,使用APT命令进行安装: $ sudo apt-get install geoip-bin 该包由MaxMind提供,它同 ...

  8. Kafka 0.11版本新功能介绍 —— 空消费组延时rebalance

    在0.11之前的版本中,多个consumer实例加入到一个空消费组将导致多次的rebalance,这是由于每个consumer instance启动的时间不可控,很有可能超出coordinator确定 ...

  9. RewriteCond和13个mod_rewrite应用举例Apache伪静态

    1.给子域名加www标记 RewriteCond %{HTTP_HOST} ^([a-z.]+)?example\.com$ [NC] RewriteCond %{HTTP_HOST} !^www\. ...

  10. 小北微信小程序之小白教程系列之 -- 样式(WXSS)

    为了适应广大的前端开发者,WXSS 具有 CSS 大部分 特性.同时为了更适合开发微信小程序,WXSS 对 CSS 进行了扩充以及修改.与 CSS 相比,WXSS 扩展的特性有:尺寸单位和样式导入. ...