题意:给你一个数组, q次询问, 每次询问都会有1个[l, r] 求 区间[1,l] 和 [r, n] 中 数字的种类是多少。

解法1, 莫队暴力:

代码:

 #include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL mod = (int)1e9+;
const int N = 2e5 + ;
int n, m, blo;
int a[N];
struct Node{
int l, r, id, ans;
}q[N];
bool cmp(Node x1, Node x2){
if(x1.l/blo != x2.l / blo) return x1.l < x2.l;
if((x1.l / blo) & ) return x1.r < x2.r;
else return x1.r > x2.r;
}
int cnt[N];
int tot;
int ans[N];
void add(int p){
//cout << p << endl;
cnt[a[p]]++;
if(cnt[a[p]] == ) tot++;
}
void Remove(int p){
cnt[a[p]]--;
if(cnt[a[p]] == ) tot--;
}
int main(){
while(~scanf("%d%d", &n, &m)){
memset(cnt, , sizeof(cnt));
tot = ;
blo = + ;
for(int i = ; i <= n; i++)
scanf("%d", &a[i]);
for(int i = ; i <= m; i++){
scanf("%d%d", &q[i].l, &q[i].r);
q[i].id = i;
}
sort(q+, q++m, cmp);
int L = , R = n+;
for(int i = ; i <= m; i++){
int nl = q[i].l;
int nr = q[i].r;
while(L < nl) add(++L);
while(L > nl) Remove(L--);
while(R > nr) add(--R);
while(R < nr) Remove(R++);
ans[q[i].id] = tot;
}
for(int i = ; i <= m; i++){
printf("%d\n", ans[i]);
}
}
return ;
}

解法2, 离线询问, 按r从小到打排序, 每次r往右边移动的时候, 如果r移除的时候移除了 x 最后一次出现的位置, 那么就在x第一次出现的位置标记一下。 每次询问的时候答案就为[1,l] 标记数字出现的次数 + 右边的值。我们用树状数组来优化[1,l]的和。

代码:

 #include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL mod = (int)1e9+;
const int N = 2e5 + ;
int cnt[N], last[N], first[N];
int a[N], ans[N];
int n, m, tot;
struct Node{
int l, r, id;
bool operator <(Node &x) const{
return r < x.r;
}
}q[N];
void Add(int x){
while(x <= n){
cnt[x]++;
x += x&(-x);
}
}
int Query(int x){
int ret = ;
while(x){
ret += cnt[x];
x -= x&(-x);
}
return ret;
}
int main(){
while(~scanf("%d%d", &n, &m)){
memset(cnt, , sizeof(cnt));
memset(last, , sizeof(last));
memset(first, , sizeof(first));
tot = ;
for(int i = ; i <= n; i++){
scanf("%d", &a[i]);
if(first[a[i]] == ){
first[a[i]] = i;
tot++;
}
last[a[i]] = i;
}
for(int i = ; i <= m; i++){
scanf("%d%d", &q[i].l, &q[i].r);
q[i].id = i;
}
sort(q+, q++m);
int R = ;
for(int i = ; i <= n; i++){
while(R < q[i].r){
if(last[a[R]] == R){
Add(first[a[R]]);
tot--;
}
R++;
}
ans[q[i].id] = tot + Query(q[i].l);
}
for(int i = ; i <= m; i++)
printf("%d\n", ans[i]);
}
return ;
}

还有蔡队的写法, 直接复制一份原来的数组在后面, 将2个区间的问题转化成一个区间求数字的问题,然后就可以套主席树的板子了。

牛客暑假多校第一场 J Different Integers的更多相关文章

  1. 牛客暑假多校第一场J-Different Integers

    一.题目描述: 链接:https://www.nowcoder.com/acm/contest/139/JGiven a sequence of integers a1, a2, ..., an an ...

  2. 2019牛客暑期多校第一场题解ABCEFHJ

    A.Equivalent Prefixes 传送门 题意:给你两个数组,求从第一个元素开始到第p个元素 满足任意区间值最小的元素下标相同的 p的最大值. 题解:我们可以从左往右记录到i为止每个区间的最 ...

  3. 牛客暑假多校第二场J-farm

    一.题意 White Rabbit has a rectangular farmland of n*m. In each of the grid there is a kind of plant. T ...

  4. 牛客暑假多校第二场 F trade

    题意: 白兔有n个仓库,每个仓库有啊ai个货物,在每个仓库白兔可以装上任意数量的货物,也可以卸下任意数量的货物,现在有k个圆形信号阻隔器,然后有m个顾客下个一个订单,每个顾客的收货量有一个上限, 在每 ...

  5. 牛客暑假多校第二场 K carpet

    题意:给你一个n*m的矩阵 ,每个位置都有一个字符并且都有一个值,现在需要找到一个p*q的子矩阵, 原来的矩阵可以由现在这个矩阵无限复制然后截取其中的一部分得到,并且要求 子矩阵里最大的值 * (p+ ...

  6. 2019 牛客暑期多校 第一场 H XOR (线性基)

    题目:https://ac.nowcoder.com/acm/contest/881/H 题意:求一个集合内所有子集异或和为0的长度之和 思路:首先集合内异或和,这是线性基的一个明显标志,然后我们不管 ...

  7. LGV定理 (CodeForces 348 D Turtles)/(牛客暑期多校第一场A Monotonic Matrix)

    又是一个看起来神奇无比的东东,证明是不可能证明的,这辈子不可能看懂的,知道怎么用就行了,具体看wikihttps://en.wikipedia.org/wiki/Lindstr%C3%B6m%E2%8 ...

  8. 【牛客网多校第一场】A

    题目链接:https://www.nowcoder.com/acm/contest/139/A 题意:大概就是给你0,1,2让你填矩阵问有多少种填法满足 a(i,j)<=a(i+1,j)以及a( ...

  9. 【2019牛客暑期多校第一场】E题ABBA

    题目链接 大致题意 有(n+m)(n + m)(n+m)个字母A和(n+m)(n + m)(n+m)个字母B,组成一个长度为 2∗(n+m)2*(n + m)2∗(n+m)的字符串,并且使得字符串中有 ...

随机推荐

  1. js函数柯理化

    所谓的函数柯理化,简单来说就是,一个需要接收多个参数的函数,进行分开一个个的传递参数,当函数执行的时候,传递剩余的参数. 主要作用在于增强函数的通用性. 如下举个例子: function custom ...

  2. 9-2、大型项目的接口自动化实践记录----递归判断两个json串是否相等

    1.已知json串构成的情况下判断 先构造一下场景,假设已经把各个数据都移除掉不对比的字段 图1 预期.实际结果,复杂接口返回多层嵌套json时,同下 图2 预期.实际结果值为:{child_json ...

  3. ns3 802.11b PHY model

    I use the ubuntu and do not install the chinse input. The Code: c file requires gnu gsl library, it ...

  4. IDEA运行报错: Maven编译错误:不再支持源选项 5。请使用 6 或更高版本

    这里 记录下 这个问题的解决方案: 1:修改maven settings.xml 中的数据 这里的版本要对应现在使用的jdk版本 2:检查idea 配置 图中2块区域要一致 检查这块地方对应了自己的j ...

  5. linux装OpenOffice后传---中文乱码的解决

    上一篇的博客已经详细的介绍了linux系统上如何安装OpenOffice,安装之后使用发现转换的pdf出现中文乱码.后来发现是linux上没有中文对应的那个字体. 字体准备 在windows上的位置 ...

  6. DC-2靶机

    DC-2 靶机获取:http://www.five86.com/ 靶机IP:192.168.43.197(arp-scan l) 攻击机器IP:192.168.43.199 在hosts文件里添加:1 ...

  7. vi编辑器基础指令

    vi -- 终端中的编辑器 visual interface ssh-- secure shell vim vi improved-----------------------------打开和新建文 ...

  8. javaScript今日总结

    javascript简单介绍ECMAScript 1.语法 2.变量:只能使用var定义,如果在函数的内容使用var定义,那么它是一个局部变量,如果没有使用var它是一个全局的.弱类型! 3.数据类型 ...

  9. IntelliJ IDEA 激活(最新)

    注:此文以 Mac 为例,Windows 的激活方法也大同小异.如果不差钱的话,建议购买正版! 1.下载安装 直接通过下面的链接到官网下载最新的 Ultimate 版本即可: https://www. ...

  10. javaScript基础-0 javascript概述

    一.简介 javaScript一种面向web的编程语言,是一种动态类型.弱类型.基于原型的语言,内置支持类型.它的解释器被称为JavaScript引擎,为浏览器的一部分,广泛用于客户端的脚本语言,最早 ...