Given a sequence of integers a1, a2, ..., an and q pairs of integers (l 1, r1), (l2, r2), ..., (lq, rq), find count(l1, r1),  count(l2, r2), ..., count(lq, rq) where count(i, j) is the number of different integers among a 1, a2, ..., ai  , aj  , aj + 1,  ..., an.

输入描述:

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, a2, ..., an.  The i-th of the following q lines contains two integers l  i and ri  .

输出描述:  For each test case, print q integers which denote the result.

备注  * 1 ≤ n, q ≤ 10  5  * 1 ≤ ai ≤ n  * 1 ≤ l  i  , ri ≤ n  * The number of test cases does not exceed 10.

示例1:

输入

3 2  1 2 1  1 2  1 3  4 1  1 2 3 4  1 3

输出

2  1  3

题意就是求去掉(i-1,j-1)区间得数以后剩余的不同数的个数

也就是求(1,l)和(r,n)中不同数的个数,因为这两个区间不能直接合并在一起,所以比赛的时候我是通过倍增区间使两个区间合并在一起

倍增区间后,我们所要求的区间就是(r,n+l),然后就是一个求区间不同数的问题了

比赛的时候我用的是主席树,比赛结束后叉姐直播说用的树状数组,于是看了别人的博客用树状数组写了遍

参考博客:https://www.cnblogs.com/kkrisen/p/3879517.html

下面是树状数组的用法:

我的做法是把区间排好序,针对某个区间在树状数组上更新以及查询相应值,这样能准确查出结果,但又不影响之后的查询

具体来说,先把区间按右端点进行排序,然后从小区间开始,树状数组的含义就是指以当前r为结尾的前缀区间的元素种类数,简单点说,就是我当前扫到l _ r区间,把l - r区间还没在树状数组上更新的值,更新一遍,在之前已经存在了的值先删掉再更新一遍,确保我确定的元素都是往r靠的,这样才能保证求取区间正确

比如我 1 2 2 1 3,当我r移到3的时候,加入前面的1还没在树状数组里更新过(但其实之前已经有读过1了)那就把之前的1的影响删掉,重新在这个3左边这个下标为4的位置给树状数组 add 1.这样确保之后不管我是查询 4 5 或者 1 5,元素1都只算了一次,但都没遗落(想想如果元素1一直在下标1那里,我查询4 5,就不会有1了)

总之:

所以这就是这个离线用法的妙处,尤其要理解树状数组在这个题目代表的含义是什么,即当前 以r结尾的区间的元素种类个数,为了维护这个值的准确性,必须把没出现过的,加入到树状数组中,之前已经出现过了并且再次出现的,以再次出现的位置为准。

每次对区间不用一开始就扫,每个就只要扫一次就可以了,用个vis数组记录哪个出现了,出现在什么位置,就不用重新扫了,否则会超时,这样,总共也就扫了n下而不是n*q(这里数字小用vis更快,用map会tle)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
const int N = 300010*2;
int n, vis[N];
struct BIT {
int c[N];
void init(int n) {
for (int i=0;i<=n;i++) c[i]=0;
}
void add(int loc,int v) {
while (loc<=2*n) {
c[loc]+=v;
loc+=loc&(-loc);
}
}
int sum(int loc) {
int ret=0;
while (loc) {
ret+=c[loc];
loc-=loc&(-loc);
}
return ret;
}
}T;
struct node {
int l,r,id;
bool operator <(const node&rhs) const {
//if (l==rhs.l) return r<rhs.r;
return r<rhs.r;
}
}query[2000000+10];
int ans[2000000+10];
int A[N];
int main() {
while (scanf("%d",&n)!=EOF) {
int q;
scanf("%d",&q);
memset( vis, 0, sizeof(vis) );
T.init(2*n);
for (int i=1;i<=n;i++) scanf("%d",&A[i]), A[n+i] = A[i];
for (int i=0;i<q;i++) {
int le, ri;
scanf("%d%d",&le,&ri);
query[i].l = ri, query[i].r = n+le;
//scanf("%d%d",&query[i].l,&query[i].r);
query[i].id=i;
}
sort(query,query+q);
int cur=1;
for (int i=0;i<q;i++) {
for (int j=cur;j<=query[i].r;j++) {
if (vis[A[j]]) {
T.add(vis[A[j]],-1);
}
T.add(j,1);
vis[A[j]] = j;
}
cur=query[i].r+1;
ans[query[i].id]=T.sum(query[i].r)-T.sum(query[i].l-1);
}
for (int i=0;i<q;i++) {
printf("%d\n",ans[i]);
}
}
return 0;
}

  

在贴一份主席树的代码,比赛时候A的代码,他优化数据后还是过了

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = 1e6 + 10; int n,q;
int cnt = 0;
struct Node
{
int l,r,sum;
} p[maxn << 3]; int la[maxn << 3]; int a[maxn << 3];
int root[maxn << 3]; int build(int l,int r)
{
int nc = ++cnt;
p[nc].sum = 0;
p[nc].l = p[nc].r = 0;
if (l == r) return nc;
int m = l + r >> 1;
p[nc].l = build(l,m);
p[nc].r = build(m+1,r);
return nc;
} int update(int pos,int c,int v,int l,int r)
{
int nc = ++cnt;
p[nc] = p[c];
p[nc].sum += v;
if (l == r) return nc;
int m = l+r>>1;
if (m >= pos)
{
p[nc].l = update(pos,p[c].l,v,l,m);
}
else
{
p[nc].r = update(pos,p[c].r,v,m+1,r);
}
return nc;
} int query(int pos,int c,int l,int r)
{
if (l == r) return p[c].sum;
int m = l + r >> 1;
if (m >= pos)
{
return p[p[c].r ].sum + query(pos,p[c].l,l,m); }
else return query(pos,p[c].r,m+1,r);
} int main()
{
while(scanf("%d %d",&n,&q) != EOF)
{
cnt=0;
memset(la,-1,sizeof la);
for (int i = 1; i <= n; ++i)
{
scanf("%d",a+i);
a[n+i]=a[i];
}
root[0] = build(1,2*n); for (int i = 1 ; i <= 2*n; ++i)
{
int v = a[i];
if (la[v] == -1)
{
root[i] = update(i,root[i-1],1,1,2*n);
}
else
{
int t = update(la[v],root[i-1],-1,1,2*n);
root[i] = update(i,t,1,1,2*n);
}
la[v] = i;
} while(q--)
{
int x,y;
scanf("%d %d",&x, &y);
printf("%d\n",query(y,root[n + x],1,2*n));
///printf("%d\n",query(1,root[x],1,2*n)+query(y,root[n],1,2*n));
}
}
return 0;
}

  

Different Integers 牛客网暑期ACM多校训练营(第一场) J 离线+线状数组或者主席树的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  9. 牛客网暑期ACM多校训练营(第九场) A题 FWT

    链接:https://www.nowcoder.com/acm/contest/147/A来源:牛客网 Niuniu has recently learned how to use Gaussian ...

随机推荐

  1. 8天入门docker系列 —— 第八天 让程序跑在swarm集群上

    真正的落地部署都是希望程序跑在集群下,而不是单机版下测测玩玩,所以这篇就来聊一下怎么使用docker swarm进行部署,因为是swarm是docker自带的, 所以部署起来还是非常简单的. 一:前置 ...

  2. SQLServer2000同步复制技术实现步骤

    SQLServer2000同步复制技术实现步骤 一. 预备工作 1.发布服务器,订阅服务器都创建一个同名的windows用户,并设置相同的密码,做为发布快照文件夹的有效访问用户 --管理工具 --计算 ...

  3. Linux常用命令之ftp

    FTP是Internet用户使用最频繁的文件上传.下载的命令之一.linux ftp用命令的方式来控制在本机和远程ftp服务器之间传送文件.ftp中的命令包括上传文件(单个.多个),下载文件(单个.多 ...

  4. java之面向对象详解

    #############java面向对象详解#############1.面向对象基本概念2.类与对象3.类和对象的定义格式4.对象与内存分析5.封装性6.构造方法7.this关键字8.值传递与引用 ...

  5. 100天搞定机器学习|day38 反向传播算法推导

    往期回顾 100天搞定机器学习|(Day1-36) 100天搞定机器学习|Day37无公式理解反向传播算法之精髓 上集我们学习了反向传播算法的原理,今天我们深入讲解其中的微积分理论,展示在机器学习中, ...

  6. MySQL-InnoDB锁(二)

    上篇文章中对InnoDB存储引擎中的锁进行学习,本文是实践部分,根据索引和查询范围,探究加锁范围的情况. 在本实例中,创建简单表如下: mysql> select * from t; +---- ...

  7. ajax提交的问题点记录

    原始方式是这样的: var prId = $("#prId").val(); var prNumber = $("#prNumber").val(); var ...

  8. C# - 协变、逆变 看完这篇就懂了

    1. 基本概念 官方:协变和逆变都是术语,前者指能够使用比原始指定的派生类型的派生程度更大(更具体的)的类型,后者指能够使用比原始指定的派生类型的派生程度更小(不太具体的)的类型.[MSDN] 公式: ...

  9. MySQL之备份和还原

    在实际项目中对于数据库的安全是重中之重,为防万一我们需要做好备份工作.备份分为全量备份和增量备份,今天我们就来实践下备份和还原操作. 一.为什么需要备份 在生产环境中数据库可能会遭遇到各种各样的不测从 ...

  10. 百度地图获取定位,实现拖动marker定位,返回具体的位置名

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...