题目链接: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. [MySQL] 01- Basic sql

    准备 一.配置 1. 登录:mysql -u root -p  2. phpMyAdmin创建数据库,并导入.sql文件. 3. 支持中文:set names utf8; 二.面试题 参考:面试宝典- ...

  2. Java通过复选框控件数组实现添加多个复选框控件

    编写程序,通过复选框控件数组事先选择用户爱好信息的复选框,在该程序中,要求界面中的复选框数量可以根据指定复选框名称的字符串数组的长度来自动调节. 思路如下: 创建JPanel面板对象: 使用JPane ...

  3. 5 -- Hibernate的基本用法 --4 8 外连接抓取属性

    外连接抓取能限制执行SQL语句的次数来提高效率,这种外连接抓取通过在单个select语句中使用outer join来一次抓取多个数据表的数据. 外连接抓取允许在单个select语句中,通过@ManyT ...

  4. iOS开发--时间戳问题

    什么是时间戳? 时间戳(timestamp),通常是一个字符序列,唯一地标识某一刻的时间.数字时间戳技术是数字签名技术一种变种的应用. 思考:简单来讲就是根据文件hash加密后生成的摘要和时间生成的时 ...

  5. O2O(online to offline)营销模式

    O2O营销模式又称离线商务模式,是指线上营销线上购买带动线下经营和线下消费.O2O通过打折.提供信息.服务预订等方式,把线下商店的消息推送给互联网用户,从而将他们转换为自己的线下客户,这就特别适合必须 ...

  6. mybatis 之resultType="HashMap" parameterType="list"

    <!-- 查询商品仓库信息 --> <select id="loadGoodsStock" resultType="HashMap" para ...

  7. linux下c语言获取当前时间

    和时间有关的函数定义在头文件”time.h”中 常用函数: time_t time(time_t *t); 函数说明:此函数会返回从公元 1970 年1 月1 日的UTC 时间从0 时0 分0 秒算起 ...

  8. LinQ的初步学习与总结

    嘿嘿,说起来ORM和LinQ,就感觉离我好遥远的,在学校是没有学习的,所以总感觉学习了LinQ就是大神,现在嘛,终于也体会一点,感觉LinQ只是初步学习,没有太难,当然以后使用在项目中就没有这样的简单 ...

  9. Git和GitHub入门基础

    -----------------------------------------//cd F:/learngit // 创建仓库git init  // 在当前目录下创建空的git仓库------- ...

  10. MySql数据库设计表添加字段

    当要添加的字段属于整型,需要设置默认值 或者: alter table fp_user_base add hasPwd tinyint(4) not null default 0;