牛客网暑期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 ...
随机推荐
- 《Cracking the Coding Interview》——第3章:栈和队列——题目6
2014-03-19 03:01 题目:给定一个栈,设计一个算法,在只使用栈操作的情况下将其排序.你可以额外用一个栈.排序完成后,最大元素在栈顶. 解法:我在草稿纸上试了试{1,4,2,3}之类的小例 ...
- 《Cracking the Coding Interview》——第1章:数组和字符串——题目3
2014-03-18 01:32 题目:对于两个字符串,判断它们是否是Anagrams. 解法:统计俩单词字母构成是否相同即可. 代码: // 1.3 Given two strings, write ...
- 一个初学者的辛酸路程-前端cs
一.主要内容 继续CSS 二.CSS 第一个: postion 网页有一类就是返回顶部,一直在右下角,还有打开一个网页顶部有个菜单,滚动滑轮,顶部永远在上面. position: fiexd == ...
- 求 n的阶乘
def chengji(n): if n == 0: return 1 return chengji(n-1)*nprint(chengji(n))
- 调整CodeIgniter错误报告级别
修改位置:CI根目录 index.php 为开发环境与生产环境定义错误报告级别 if (defined('ENVIRONMENT')) { switch (ENVIRONMENT) { case 'd ...
- 电信学院第一届新生程序设计竞赛题解及std
首先非常感谢各位同学的参加,还有出题验题同学的辛勤付出 昨天想偷懒就是不想再把我C++11的style改没了,大家看不懂的可以百度一下哦,懒得再写gcc了,毕竟代码是通的 //代表的是行注释,所以那个 ...
- Password [分块]
题面 $n,m,x \leq 10^5$ 思路 首先$n=2$做法很多,不讲了 $n=3$的时候,分块维护两个东西:每一个数出现次数的前缀和,和出现次数的出现次数的前缀和(说的有点绕,但是应该挺好理解 ...
- gdb调试的艺术——Debug技巧
调试的艺术——Debug技巧总结 (本文从写好的wiki里粘出来的,格式稍乱不影响阅读) 用Q+编号代表问题,A+编号代表答案.用这种方式组织.如无特别说明,这些技巧都是针对Visual Studio ...
- MAC使用IDA PRO远程调试LINUX程序
1 背景 在学习Linux系统上的一些漏洞知识的时候,往往需要进行“实地测试”,但是在Linux系统上进行调试并不太方便,因为LINUX自带的GDB调试工具真的不太人性化,即使有GDBTUI之类的“伪 ...
- [1]区分event对象中的[clientX,offsetX,screenX,pageX]
前言 在平时的开发中,非常讨厌的就是兼容性了,兼容性的问题总会让我们记忆混淆,所以这次来区分一下event对象中的常用获取鼠标位置. clientX clientY event.clientXeven ...