题解:解法一:莫队

解法二:按区间左端点排序,让区间内最左边的贝壳对答案产生贡献,树状数组维护,转移对答案产生贡献的贝壳位置

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm> using namespace std;
const int maxn=1000009; int n,m;
int a[maxn];
int nex[maxn];
int ans[maxn]; int tong[maxn]; inline int lowbit(int x){
return x&(-x);
}
int c[maxn];
int add(int x,int val){
while(x<=1000001){
c[x]+=val;
x+=lowbit(x);
}
}
int query(int x){
int ret=0;
while(x){
ret+=c[x];
x-=lowbit(x);
}
return ret;
} struct Section{
int l,r,id;
}sec[maxn];
int cmp(const Section &t1,const Section &t2){
if(t1.l==t2.l)return t1.r<t2.r;
else return t1.l<t2.l;
} int main(){
scanf("%d",&n);
for(int i=1;i<=n;++i){
scanf("%d",&a[i]);
if(a[i]==0)a[i]=1000001;
}
for(int i=n;i>=1;--i){
if(!tong[a[i]]){
nex[i]=n+1;
}else{
nex[i]=tong[a[i]];
}
tong[a[i]]=i;
}
scanf("%d",&m);
for(int i=1;i<=m;++i){
scanf("%d%d",&sec[i].l,&sec[i].r);
sec[i].id=i;
}
sort(sec+1,sec+1+m,cmp); memset(tong,0,sizeof(tong));
for(int i=1;i<=n;++i){
if(!tong[a[i]])add(i,1);
tong[a[i]]=1;
} int head=1;
for(int i=1;i<=m;++i){
while(head<sec[i].l){
add(head,-1);
add(nex[head],1);
++head;
}
ans[sec[i].id]=query(sec[i].r)-query(sec[i].l-1);
}
for(int i=1;i<=m;++i)printf("%d\n",ans[i]);
return 0;
}

  

最新文章

  1. 随机函数的代码(srand、rand)
  2. The conversion of a varchar data type to a datetime data type resulted in an out-of-range value
  3. []with[[]]
  4. 阿里云oss上传图片
  5. 唉,还是Windows好
  6. outlook圆角table
  7. Python的类变量和对象变量声明解析
  8. 关于使用视图进行分页时出现当前记录集不支持书签的错误解决方法及原因(asp)
  9. Java和.NET的GZIP压缩功能对比
  10. linux命令之ls命令的简明讲解
  11. redis安装方法
  12. 基于FPGA的key button等开关消抖,按键消抖电路设计
  13. [译]Stairway to Integration Services Level 8 - SSIS 工作流管理高级
  14. .NET C#到Java没那么难,DB篇
  15. Docker最全教程之使用 Visual Studio Code玩转Docker(二十)
  16. css3实现旋转表
  17. Vue列表组件与弹窗组件示例
  18. Getting started - RN1
  19. springmvc+ajax文件上传
  20. 【Luogu1937】仓配置(贪心,线段树)

热门文章

  1. 本地Git仓库与GitHub/GitLab仓库同步
  2. sqlserver查找断号,回收单据号
  3. ThinkPHP 3.2 生成静态页面
  4. PAT (Advanced Level) 1136~1139:1136模拟 1137模拟 1138 前序中序求后序 1139模拟
  5. css滚动
  6. 微信小程序是什么
  7. 二进制枚举之被RE完虐的我的一天
  8. 007、Java中定义int型变量
  9. 数据结构——Java Stack 类
  10. 树莓派—raspbian软件源