链接: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(分开区间不同数+树状数组)的更多相关文章

  1. 牛客网暑期ACM多校训练营 第九场

    HPrefix Sum study from : https://blog.csdn.net/mitsuha_/article/details/81774727 k较小.分离x和k. 另外的可能:求a ...

  2. 牛客网暑期ACM多校训练营(第四场):A Ternary String(欧拉降幂)

    链接:牛客网暑期ACM多校训练营(第四场):A Ternary String 题意:给出一段数列 s,只包含 0.1.2 三种数.每秒在每个 2 后面会插入一个 1 ,每个 1 后面会插入一个 0,之 ...

  3. 牛客网暑期ACM多校训练营(第五场):F - take

    链接:牛客网暑期ACM多校训练营(第五场):F - take 题意: Kanade有n个盒子,第i个盒子有p [i]概率有一个d [i]大小的钻石. 起初,Kanade有一颗0号钻石.她将从第1到第n ...

  4. 牛客网 暑期ACM多校训练营(第二场)A.run-动态规划 or 递推?

    牛客网暑期ACM多校训练营(第二场) 水博客. A.run 题意就是一个人一秒可以走1步或者跑K步,不能连续跑2秒,他从0开始移动,移动到[L,R]的某一点就可以结束.问一共有多少种移动的方式. 个人 ...

  5. 牛客网 暑期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 ,问你 ...

  6. 牛客网暑期ACM多校训练营(第三场)H Diff-prime Pairs (贡献)

    牛客网暑期ACM多校训练营(第三场)H Diff-prime Pairs (贡献) 链接:https://ac.nowcoder.com/acm/contest/141/H来源:牛客网 Eddy ha ...

  7. 2018牛客网暑期ACM多校训练营(第二场)I- car ( 思维)

    2018牛客网暑期ACM多校训练营(第二场)I- car 链接:https://ac.nowcoder.com/acm/contest/140/I来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 ...

  8. Different Integers 牛客网暑期ACM多校训练营(第一场) J 离线+线状数组或者主席树

    Given a sequence of integers a1, a2, ..., an and q pairs of integers (l 1, r1), (l2, r2), ..., (lq, ...

  9. 牛客网暑期ACM多校训练营(第七场)Bit Compression

    链接:https://www.nowcoder.com/acm/contest/145/C 来源:牛客网 题目描述 A binary string s of length N = 2n is give ...

  10. 牛客网暑期ACM多校训练营(第一场) - J Different Integers(线段数组or莫队)

    链接:https://www.nowcoder.com/acm/contest/139/J来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 524288K,其他语言1048 ...

随机推荐

  1. lasagne保存网络参数

    # Optionally, you could now dump the network weights to a file like this: # np.savez('model.npz', *l ...

  2. idea中使用maven方式使用jetty+cmd中使用Jetty运行(maven)Web项目

    进度条件:必须是web项目 一.使用idea 导入项目或者打开(如果有可以忽略) 导入项目 . 全部next 导入成功,进行打开pom文件加入插件 <plugins> <!-- je ...

  3. HTML5 input date 移动端 IOS 不支持问题

    1.placeholder 问题解决方法 对 input type date 使用 placeholder 的目的是为了让用户更准确的输入日期格式,iOS 上会有 date 不会显示 placehol ...

  4. SpringBoot中使用配置文件

    一般都是把xml配置文件转换为@Bean的模式,如果非要使用xml配置文件,方式如下: /** * 将配置文件引入springboot */ @Configuration @ImportResourc ...

  5. C#使用ref和out传递数组

    C#使用ref和out传递数组 一.使用ref参数传递数组 数组类型的ref参数必须由调用方明确赋值.因此,接受方不需要明确赋值.接受方数组类型的ref参数能够修改调用方数组类型的结果.可以将接受方的 ...

  6. python显示灰度图

    import matplotlib import matplotlib.pyplot as plt %matplotlib inline im=plt.imread('../lena.jpg', py ...

  7. Rxjava+retrofit+mvp整合

    转载请标明出处: http://blog.csdn.net/forezp/article/details/52621898 本文出自方志朋的博客 最近在看Rxjava,写了一个简单的 demo整合了R ...

  8. 【学时总结】◆学时·VII◆ 高维DP

    ◆学时·VII◆ 高维DP 自学之余,偶遇DP…… ◇ 算法概述 顾名思义——一种处理多方面状态的DP,这种DP特点是……每一维的大小都不算太大(不然用dp数组存储下来内存会炸),而且枚举时容易超时… ...

  9. linux 基本命令笔记

    nohup [process]  & 后台挂起命令nohup 挂起& 后台运行 python3 manage.py runserver 0.0.0.0:8080 python -r 递 ...

  10. 【shopex】真正可用的app开发机制

    shopex的app开发机制详解   网上流传的shopex4.8.5的app开发教程,不仅说得不明不白,而且由于版本问题,照着做根本是做不成的. 知其然,亦要知其所以然. shopex提供了的一个干 ...