牛客网暑期ACM多校训练营(第一场) - J Different Integers(线段数组or莫队)
链接:https://www.nowcoder.com/acm/contest/139/J
来源:牛客网
空间限制:C/C++ 524288K,其他语言1048576K
64bit IO Format: %lld
题目描述
输入描述:
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 ≤ 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莫队)的更多相关文章
- 牛客网暑期ACM多校训练营 第九场
HPrefix Sum study from : https://blog.csdn.net/mitsuha_/article/details/81774727 k较小.分离x和k. 另外的可能:求a ...
- 牛客网暑期ACM多校训练营(第四场):A Ternary String(欧拉降幂)
链接:牛客网暑期ACM多校训练营(第四场):A Ternary String 题意:给出一段数列 s,只包含 0.1.2 三种数.每秒在每个 2 后面会插入一个 1 ,每个 1 后面会插入一个 0,之 ...
- 牛客网暑期ACM多校训练营(第五场):F - take
链接:牛客网暑期ACM多校训练营(第五场):F - take 题意: Kanade有n个盒子,第i个盒子有p [i]概率有一个d [i]大小的钻石. 起初,Kanade有一颗0号钻石.她将从第1到第n ...
- 牛客网 暑期ACM多校训练营(第二场)A.run-动态规划 or 递推?
牛客网暑期ACM多校训练营(第二场) 水博客. A.run 题意就是一个人一秒可以走1步或者跑K步,不能连续跑2秒,他从0开始移动,移动到[L,R]的某一点就可以结束.问一共有多少种移动的方式. 个人 ...
- 牛客网 暑期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 ,问你 ...
- 牛客网暑期ACM多校训练营(第三场)H Diff-prime Pairs (贡献)
牛客网暑期ACM多校训练营(第三场)H Diff-prime Pairs (贡献) 链接:https://ac.nowcoder.com/acm/contest/141/H来源:牛客网 Eddy ha ...
- 2018牛客网暑期ACM多校训练营(第二场)I- car ( 思维)
2018牛客网暑期ACM多校训练营(第二场)I- car 链接:https://ac.nowcoder.com/acm/contest/140/I来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 ...
- 牛客网暑期ACM多校训练营(第七场)Bit Compression
链接:https://www.nowcoder.com/acm/contest/145/C 来源:牛客网 题目描述 A binary string s of length N = 2n is give ...
- 牛客网暑期ACM多校训练营(第九场) A题 FWT
链接:https://www.nowcoder.com/acm/contest/147/A来源:牛客网 Niuniu has recently learned how to use Gaussian ...
随机推荐
- Python 3基础教程2-打印语句和字符串
本文介绍Python 3中的打印语句和字符串使用,具体练习请看下面的demo.py print ('Hello Python 3!') """文本讲打印语句和字符串打印语 ...
- python之while/for循环
一.while循环 (一)循环语句 while 后面接判断语句,在返回结果时有以下几种语句: 1.break 仅适用于循环语句,意思是结束最近的循环 2.continue 仅适用于循环语句,意思是跳到 ...
- 事件Qevent的接受和忽略 和重定义 事件过滤器(转)
转载来源:http://blog.csdn.net/seanyxie/article/details/5821970 事件处理流程:某个事件发生------>exec()循环会接收到这个事件-- ...
- pandas.read_csv to_csv参数详解
pandas.read_csv参数整理 读取CSV(逗号分割)文件到DataFrame 也支持文件的部分导入和选择迭代 更多帮助参见:http://pandas.pydata.org/pandas ...
- Linux网络运维相关
删除特殊的用户和用户组 userdel games group games 关闭不需要的服务 chkconfig chkconfig --level 345 bluetooth off 删减系 ...
- php设计模式 工厂模式和单例
1.单例模式//让该类在外界无法造对象//让外界可以造一个对象,做一个静态方法返回对象//在类里面通过让静态变量控制返回对象只能是一个. class cat{ public $name; privat ...
- 第二章 Internet 地址结构
注意: 这个系列的博客只是为了巩固我学习的知识,参考的价值不是很大,如果需要,请转到http://www.cnblogs.com/ZCplayground/p/7764436.html Interne ...
- 【bzoj3530】[Sdoi2014]数数 AC自动机+数位dp
题目描述 我们称一个正整数N是幸运数,当且仅当它的十进制表示中不包含数字串集合S中任意一个元素作为其子串.例如当S=(22,333,0233)时,233是幸运数,2333.20233.3223不是幸运 ...
- 提取URL字符串的搜索字符串中的参数
function urlArgs() { var args = []; var query = location.search.substring(1); var paris = query.spli ...
- highchart柱状图 series中data的数据构造
先可以看一下data的数据结构 网站http://jsfiddle.net/gh/get/jquery/1.9.1/highslide-software/highcharts.com/tree/mas ...