牛客网暑期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 ...
随机推荐
- ASP.NET Core API ---状态码
摘录自:https://www.cnblogs.com/cgzl/p/9047626.html 状态码是非常重要的,因为只有状态码会告诉API的消费者: 请求是否如预期的成功,或者失败 如果出现了错误 ...
- 易语言.开源(绝地求生多功能盒子)类似LOL盒子
下载地址:https://pan.baidu.com/s/1OXwCjGJODkcZVrCwVixu3Q 成品地址:https://pan.lanzou.com/i0rmdwj
- Opencv3.2.0安装包
这个资源是Opencv3.2.0安装包,包括Windows软件包,Android软件包,IOS软件包,还有opencv的源代码:需要的下载吧. 点击下载
- 在Struts2 Action中快速简便的访问Request、Session等变量
前言——正常情况下如何在Action中获取到这些变量 全部方法(共四种)可以参考:http://blog.csdn.net/itmyhome1990/article/details/7019476 这 ...
- 使用ADO.NET 实体数据模型连接MySql
原文:使用ADO.NET 实体数据模型连接MySql 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/a123_z/article/details/8 ...
- 【Android】实验5 数独游戏界面设计-报告提交时间:2016.4.15
- jQuery选择器之元素选择器
元素选择器:根据给定(html)标记名称选择所有的元素. 描述: $('element') 搜索指定元素标签名的所有节点,这是一个合集的操作.同样的也有原生方法getElementsByTagName ...
- table不让td中文字溢出操作方法
table不让td中文字溢出操作方法 table{ width:100px; table-layout:fixed;/* 只有定义了表格的布局算法为fixed,下面td的定义才能起作用. */ } t ...
- 为啥shmem不回收 | drop_caches
内核在哪里禁止对tmpfs中内存页的回收 mem.limit_in_bytes同样会触发shrink_zones过程! shrink_zones是代码中的直接内存回收路径 1.try_to_free_ ...
- [CF632E]Thief in a Shop
题目大意:有一个小偷,拿$k$个东西,有$n$种产品,每种产品都有无限多个.对于每个第$i$ 种产品,它的价值是$A_i$.可能偷走的物品价值之和. 题解:对于所有的物品构造生成函数$F(x)=\su ...