博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
牛客网暑期ACM多校训练营(第一场) - J Different Integers(线段数组or莫队)
阅读量:4323 次
发布时间:2019-06-06

本文共 1955 字,大约阅读时间需要 6 分钟。

链接:

来源:牛客网

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

题目描述

Given a sequence of integers a
1, a
2, ..., a
n and q pairs of integers (l
1, r
1), (l
2, r
2), ..., (l
q, r
q), find count(l
1, r
1), count(l
2, r
2), ..., count(l
q, r
q) where count(i, j) is the number of different integers among a
1, a
2, ..., a
i, a
j, a
j + 1, ..., a
n.

输入描述:

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.
示例1

输入

3 21 2 11 21 34 11 2 3 41 3

输出

213

备注:

* 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 
#include
#include
#include
#include
#include
using namespace std;const int maxn=300000+5;map
mp;int data[maxn];int a[maxn];int ans[200000+5];struct node{ int l,r,id; bool operator<(node t)const{ return r
0){ ans+=data[i]; i-=i&-i; } return ans;}void add(int i,int x){ while(i

 

也可以莫队加上读入挂

#include
using namespace std;int n,q,a[100005];int L,R,ans;struct node{ int l,r,id;};node temp[100005];int sum[1000005];int anw[1000005];int block[100005];int read(){ char ch=' '; int ans=0; while(ch<'0' || ch>'9') ch=getchar(); while(ch<='9' && ch>='0') { ans=ans*10+ch-'0'; ch=getchar(); } return ans;}int cmp(node a,node b){ if(block[a.l]==block[b.l])return block[a.r]
temp[i].r)R--,add(a[R]); while(L>temp[i].l)del(a[L]),L--; while(R

 

转载于:https://www.cnblogs.com/luowentao/p/9338237.html

你可能感兴趣的文章
[转载]AAF灵便应用框架简介系列(6):休息一下,泛谈面向对象 Why OO+多层结构?...
查看>>
android EditView ime
查看>>
关于OpenXml SpreadSheet列宽根据内容的Auto-suitability
查看>>
javascript 学习随笔7
查看>>
<P>标签小细节
查看>>
Linux 命令 - netstat
查看>>
安卓模拟器genymotion安装
查看>>
C 语言中包含的标准头文件(24个)
查看>>
移动硬盘启动盘制作
查看>>
mac 关闭&&显示隐藏文件命令
查看>>
Altium Designer 10 导出文件(PDF,gerber,BOM)
查看>>
&spi1 , spi1 = &spi1; status = "okay"
查看>>
mysql备份与还原 数据库的常用命令。
查看>>
完成登录与注册页面的前端
查看>>
NIO学习之Channel
查看>>
两分布间距离的度量:MMD、KL散度、Wasserstein 对比
查看>>
HDU 1300 Pearls (DP)
查看>>
2014年军训总结
查看>>
扩展 -------jQuery
查看>>
Winform跨线程操作最简单的办法
查看>>