链接: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. 用gulp清除、移动、压缩、合并、替换代码

    之前前端代码部署时用的是grunt,后来又出了个gulp工具,最近试用了一下,很方便,感觉比grunt简单好用,下面把一些常见的任务列一下,备用. var gulp = require('gulp') ...

  2. 【Java数据结构学习笔记之三】Java数据结构与算法之队列(Queue)实现

      本篇是数据结构与算法的第三篇,本篇我们将来了解一下知识点: 队列的抽象数据类型 顺序队列的设计与实现 链式队列的设计与实现 队列应用的简单举例 优先队列的设置与实现双链表实现 队列的抽象数据类型 ...

  3. eclipse里导入maven项目有红叉的解决办法

    导入maven的项目上有红叉,说明eclipse里maven的插件该更新了 1.help里选择install new software 2.点击add,输入name:MavenArchiver, lo ...

  4. gradle构建

    https://blog.csdn.net/baidu_30809315/article/details/77865414

  5. 孤荷凌寒自学python第四十二天python线程控制之Condition对象

     孤荷凌寒自学python第四十二天python的线程同步之Condition对象 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 今天学习了Condition对象,发现它综合了Event对象 ...

  6. sql 游标使用

    declare @PASSDate datetime,@VLPN varchar(50),@VLPNColor varchar(10),@nambers int set @VLPN='';set @V ...

  7. 编译Code::Blocks源码 with MinGW on Win

    Build Code::Blocks源码 ---By 狂徒归来 CodeBlocks是一款非常优秀的IDE !可惜的是没有64位的版本,而且本来是轻量级别的IDE就应该够轻,能够像记事本工具一样,迅速 ...

  8. python与MySQL数据库

    python与MySQL数据库 慕课网连接 我使用的软件:python2.7 + MySQL+ Navicat for MySQL + Atom 注意:你的数据库表格类型的引擎为:InnoDB :字符 ...

  9. weex & web app & vue

    weex & web app & vue https://weex-project.io/tools/playground.html https://weex.apache.org/ ...

  10. 了解Spark源码的概况

    本文旨在帮助那些想要对Spark有更深入了解的工程师们,了解Spark源码的概况,搭建Spark源码阅读环境,编译.调试Spark源码,为将来更深入地学习打下基础. 一.项目结构 在大型项目中,往往涉 ...