牛客网暑期ACM多校训练营(第一场):J-Different Integers(分开区间不同数+树状数组)
题意:给出序列a1, a2, ..., an和区间(l1, r1), (l2, r2), ..., (lq, rq),对每个区间求集合{a1, a2, ..., ai, aj, aj + 1, ..., an}中不同的数的个数。
题解:
法一:将序列数组在后面复制一遍,就形成了一个环了。这就相当于求连续区间的不同数了。
法二:离线+树状数组
处理出每个数第一次出现的位置first、最后一次出现的位置last和所有的数的种类数tot。将区间(l, r)根据r从小到大排序。从1-n遍历离线数组,如果发现某个数a[i]最后一次出现的位置等于当前i(last[a[i]] == i),就将a[i]第一次出现的位置在树状数组中标记(add(first[a[i]])),当发现r == i时,就保存答案(tot - (sum(maxn - 1) - sum(p[k].l)))。
原理:求出中间丢失的种类数,用总数减去即是答案。当first[a[i]] > l && last[a[i]] < r时,a[i]就不在所求集合里出现。
#include<bits/stdc++.h>
using namespace std; const int maxn = 1e5 + ;
int n, q;
int a[maxn];
int bit[maxn];
struct Node{
int l, r, id;
bool operator < (const Node& A) const
{
return r < A.r;
}
}p[maxn];
int first[maxn], last[maxn];
int ans[maxn]; void add(int i)
{
while(i > && i < maxn){
bit[i]++;
i += i & -i;
}
} int sum(int i)
{
int ans = ;
while(i){
ans += bit[i];
i -= i & -i;
}
return ans;
} int main()
{
while(scanf("%d%d", &n, &q) != EOF){ memset(bit, , sizeof(bit)); memset(first, -, sizeof(first));
memset(last, -, sizeof(last));
int tot = ;
for(int i = ; i <= n; i++){
scanf("%d", &a[i]);
last[a[i]] = i;
if(first[a[i]] == -){
tot++;
first[a[i]] = i;
}
} for(int i = ; i < q; i++){
scanf("%d%d", &p[i].l, &p[i].r);
p[i].id = i;
}
sort(p, p + q); memset(ans, , sizeof(ans));
for(int i = , k = ; i <= n; i++){
while(k < q && p[k].r == i){
ans[p[k].id] = tot - (sum(maxn - ) - sum(p[k].l));
k++;
}
if(last[a[i]] == i){
add(first[a[i]]);
}
} for(int i = ; i < q; i++) printf("%d\n", ans[i]);
}
}
牛客网暑期ACM多校训练营(第一场):J-Different Integers(分开区间不同数+树状数组)的更多相关文章
- 牛客网暑期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秒 ...
- Different Integers 牛客网暑期ACM多校训练营(第一场) J 离线+线状数组或者主席树
Given a sequence of integers a1, a2, ..., an and q pairs of integers (l 1, r1), (l2, r2), ..., (lq, ...
- 牛客网暑期ACM多校训练营(第七场)Bit Compression
链接:https://www.nowcoder.com/acm/contest/145/C 来源:牛客网 题目描述 A binary string s of length N = 2n is give ...
- 牛客网暑期ACM多校训练营(第一场) - J Different Integers(线段数组or莫队)
链接:https://www.nowcoder.com/acm/contest/139/J来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 524288K,其他语言1048 ...
随机推荐
- cocos2d-x 3.0 创建项目
cocos2d-x 3.0 创建项目 点击打开链接
- 简单的Nodejs模块
说千遍,道万遍,不如动手做一遍,我们实现一个node所谓的模块 看下上面的图,了解一下模块自始至终的一个流程,首先是创建模块,也就是一个入口的js文件,里面加了一些特定的功能,然后导出这个模块, ex ...
- bootstrap中模态框、模态框的属性
工作中有需要用到模态框的可以看看 <div class="modal fade" id="userModal" tabindex="-1&quo ...
- C#接口定义
C#接口定义 C#不支持多重继承,但是客观世界出现多重继承的情况又比较多.为了避免传统的多重继承给程序带来的复杂性等问题,C# 提出了接口的概念.通过接口可以实现多重继承的功能. 继承该接口的类或结 ...
- link链接外部样式表
一个完整的link标签: <link href="[css adress]" rel="stylesheet" type="text/css&q ...
- js对象动态添加属性,方法
1. 动态添加属性,方法 var object = new Object(); object.name = "name"; object.age = 19; >>> ...
- Target runtime Apache Tomcat v8.5 is not defined.
Target runtime Apache Tomcat v8.5(或者其它版本) is not defined. 这个错误通常是在从文件夹中导入别人的项目的时候发生,因为 在 .setting 中有 ...
- php 删除指定扩展名文件
<?php /** *@param $path文件夹绝对路径 $file_type待删除文件的后缀名 *return void */ function clearn_file($path, $f ...
- python__基础 : 异常处理与自定义异常
异常处理方法一般为: try: ------code----- except Exception as e: # 抛出异常之后将会执行 print(e) else: # 没有异常将会执行 print( ...
- Gson杂记录
//Integer userId = getUserId(); //System.out.println("userId:"+userId); /*for(int i=0;i< ...