链接:https://www.nowcoder.com/acm/contest/139/J
来源:牛客网

时间限制:C/C++ 2秒,其他语言4秒
空间限制:C/C++ 524288K,其他语言1048576K
64bit IO Format: %lld

题目描述

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 a

1

, a

2

, ..., a

n

.
The i-th of the following q lines contains two integers l

i

 and r

i

.

输出描述:

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

输入

复制

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

输出

复制

2
1
3

备注:

* 1 ≤ n, q ≤ 10

5

* 1 ≤ a

i

 ≤ n
* 1 ≤ l

i

, r

i

 ≤ n
* The number of test cases does not exceed 10. 骚操作:直接把数组*2 然后求1-L,R-N 就变成 求R-L+N之间的不同数的个数了;注意,主席树会TLE;要用线段数组+离线
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <map> using namespace std;
const int maxn=+;
map<int,int> mp;
int data[maxn];
int a[maxn];
int ans[+];
struct node{
int l,r,id;
bool operator<(node t)const{
return r<t.r;
}
}q[+];
int sum(int i){
int ans=;
while(i>){
ans+=data[i];
i-=i&-i;
}
return ans;
}
void add(int i,int x){
while(i<maxn){
data[i]+=x;
i+=i&-i;
}
} int main()
{
int n,m;
while(~scanf("%d%d",&n,&m)){
fill(data,data+n*+,);
mp.clear();
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
a[i+n]=a[i];
}
n=n*; for(int i=;i<m;i++){
int x,y;
scanf("%d%d",&x,&y);
q[i].l=y;
q[i].r=x+n/;
q[i].id=i;
}
sort(q,q+m);
int pre=;
for(int i=;i<m;i++){
for(int j=pre;j<=q[i].r;j++){
if(mp[a[j]]!=){
add(mp[a[j]],-);
}
add(j,);
mp[a[j]]=j;
}
pre=q[i].r+;
ans[q[i].id]=sum(q[i].r)-sum(q[i].l-);
}
for(int i=;i<m;i++){
printf("%d\n",ans[i]);
}
}
return ;
}

也可以莫队加上读入挂

#include<bits/stdc++.h>

using namespace std;
int n,q,a[];
int L,R,ans;
struct node{
int l,r,id;
};
node temp[];
int sum[];
int anw[];
int block[];
int read()
{
char ch=' ';
int ans=;
while(ch<'' || ch>'')
ch=getchar();
while(ch<='' && ch>='')
{
ans=ans*+ch-'';
ch=getchar();
}
return ans;
}
int cmp(node a,node b){
if(block[a.l]==block[b.l])return block[a.r]<block[b.r];
return block[a.l]<block[b.l];
}
void add(int x){
if(sum[x]==)ans++;sum[x]++;
}
void del(int x){
sum[x]--;if(sum[x]==)ans--;
}
int main()
{ ios::sync_with_stdio(false);
while(~scanf("%d%d",&n,&q)){
memset(sum,,sizeof(sum));
for(int i=;i<=n;i++){
a[i]=read();
block[i]=i/sqrt(n);
}
for(int i=;i<=q;i++){
temp[i].l=read();
temp[i].r=read();
temp[i].id=i;
}
sort(temp+,temp++q,cmp);
L=;R=n+;ans=;
for(int i=;i<=q;i++){
while(L<temp[i].l)L++,add(a[L]);
while(R>temp[i].r)R--,add(a[R]);
while(L>temp[i].l)del(a[L]),L--;
while(R<temp[i].r)del(a[R]),R++;
anw[temp[i].id]=ans;
}
for(int i=;i<=q;i++)printf("%d\n",anw[i]); }
return ;
}

牛客网暑期ACM多校训练营(第一场) - J Different Integers(线段数组or莫队)的更多相关文章

  1. 牛客网暑期ACM多校训练营 第九场

    HPrefix Sum study from : https://blog.csdn.net/mitsuha_/article/details/81774727 k较小.分离x和k. 另外的可能:求a ...

  2. 牛客网暑期ACM多校训练营(第四场):A Ternary String(欧拉降幂)

    链接:牛客网暑期ACM多校训练营(第四场):A Ternary String 题意:给出一段数列 s,只包含 0.1.2 三种数.每秒在每个 2 后面会插入一个 1 ,每个 1 后面会插入一个 0,之 ...

  3. 牛客网暑期ACM多校训练营(第五场):F - take

    链接:牛客网暑期ACM多校训练营(第五场):F - take 题意: Kanade有n个盒子,第i个盒子有p [i]概率有一个d [i]大小的钻石. 起初,Kanade有一颗0号钻石.她将从第1到第n ...

  4. 牛客网 暑期ACM多校训练营(第二场)A.run-动态规划 or 递推?

    牛客网暑期ACM多校训练营(第二场) 水博客. A.run 题意就是一个人一秒可以走1步或者跑K步,不能连续跑2秒,他从0开始移动,移动到[L,R]的某一点就可以结束.问一共有多少种移动的方式. 个人 ...

  5. 牛客网 暑期ACM多校训练营(第一场)A.Monotonic Matrix-矩阵转化为格子路径的非降路径计数,Lindström-Gessel-Viennot引理-组合数学

    牛客网暑期ACM多校训练营(第一场) A.Monotonic Matrix 这个题就是给你一个n*m的矩阵,往里面填{0,1,2}这三种数,要求是Ai,j⩽Ai+1,j,Ai,j⩽Ai,j+1 ,问你 ...

  6. 牛客网暑期ACM多校训练营(第三场)H Diff-prime Pairs (贡献)

    牛客网暑期ACM多校训练营(第三场)H Diff-prime Pairs (贡献) 链接:https://ac.nowcoder.com/acm/contest/141/H来源:牛客网 Eddy ha ...

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

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

  8. 牛客网暑期ACM多校训练营(第七场)Bit Compression

    链接:https://www.nowcoder.com/acm/contest/145/C 来源:牛客网 题目描述 A binary string s of length N = 2n is give ...

  9. 牛客网暑期ACM多校训练营(第九场) A题 FWT

    链接:https://www.nowcoder.com/acm/contest/147/A来源:牛客网 Niuniu has recently learned how to use Gaussian ...

随机推荐

  1. 《Cracking the Coding Interview》——第6章:智力题——题目3

    2014-03-20 00:48 题目:有3升的瓶子和5升的瓶子,只允许倒满.倒到满为止.或是泼光三种操作,怎么搞出4升水呢? 解法:如果A和B是互质的两个正整数,且A<B,令X=B-A,则(X ...

  2. 嵌入式(Embedded System)笔记 —— Cortex-M3 Introduction and Basics(上)

    随着课内的学习,我想把每节课所学记录下来,以作查阅.以饲读者.由于我所上的是英文班课程,因此我将把关键术语的英文给出,甚至有些内容直接使用英文. 本次所介绍内容是关于Cortex-M3的基础内容. - ...

  3. 玩转Node.js(二)

    玩转Node.js(二) 先来回顾上次的内容,上一次我们使用介绍了Node.js并写了第一个服务器端的Hello World程序,在这个Hello World程序中,请求自带的http模块并将其赋给h ...

  4. Linux/Unix中系统级IO

    Linux/unix I/O:将设备映射为文件的方式,允许Unix内核引出一个简单.低级的应用接口. Linux/unix IO的系统调用函数很简单,它只有5个函数:open(打开).close(关闭 ...

  5. python中subprocess.Popen执行命令并持续获取返回值

    先举一个Android查询连接设备的命令来看看Python中subprocess.Popen怎么样的写法.用到的命令为 adb devices. import subprocess order='ad ...

  6. Python 3基础教程1-环境安装和运行环境

    本系列开始介绍Python3的基础教程,为什么要选中Python 3呢?之前呢,学Python 2,看过笨方法学Python,学了不到一个礼拜,就开始用Python写Selenium脚本.最近看到一些 ...

  7. css 之 border-radius属性

    css中给盒子设置圆角可以通过 border-radius 属性来实现(具体原理就不深入探讨了); 在开发过程中都会遇到浏览器兼容问题,这问题其实也不难解决,无非就是加上私有前缀,在这里先忽略掉.   ...

  8. 每个套接字地址error

    套接字问题 1 netstat -aon|findstr 5037   2 根据pid,查询占用端口的应用,这里的pid为 8672,查询命令如图 3 杀死对应的PID,taskkill /pid 8 ...

  9. 爬虫:Scrapy9 - Feed exports

    实现爬虫时最经常提到的需求就是能合适的保存爬取到的数据,或者说,生成一个带有爬取数据的“输出文件”(通常叫“输出 feed”),来供其它系统使用. Scrapy 自带了 Feed 输出,并且支持多种序 ...

  10. linux kernal oom killer 学习

    背景 我有2个定时任务,一个任务A是00:00开跑,另一个B是04:00开跑.正常情况下A会在2点多时候跑完,但是某一天因为某一步骤用的时间过久,导致4点还没跑完,这时候A内存占用大约在12g左右.4 ...